"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/src/coordSys/TransverseMercatorProjection.cc" (30 Oct 2019, 5219 Bytes) of package /linux/privat/fimex-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TransverseMercatorProjection.cc" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.3.3_vs_1.4.0.

    1 /*
    2  * Fimex, TransverseMercatorProjection.cc
    3  *
    4  * (C) Copyright 2010-2019, met.no
    5  *
    6  * Project Info:  https://wiki.met.no/fimex/start
    7  *
    8  * This library is free software; you can redistribute it and/or modify it
    9  * under the terms of the GNU Lesser General Public License as published by
   10  * the Free Software Foundation; either version 2.1 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This library is distributed in the hope that it will be useful, but
   14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   16  * License for more details.
   17  *
   18  * You should have received a copy of the GNU Lesser General Public
   19  * License along with this library; if not, write to the Free Software
   20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   21  * USA.
   22  *
   23  *  Created on: Jun 8, 2010
   24  *      Author: Heiko Klein
   25  */
   26 
   27 #include "fimex/coordSys/TransverseMercatorProjection.h"
   28 
   29 #include "fimex/Logger.h"
   30 #include "fimex/String2Type.h"
   31 
   32 #include <regex>
   33 
   34 namespace MetNoFimex {
   35 using namespace std;
   36 
   37 static Logger_p logger = getLogger("fimex.TransverseMercatorProjection");
   38 
   39 TransverseMercatorProjection::TransverseMercatorProjection()
   40     : ProjectionImpl("transverse_mercator", false)
   41 {
   42 }
   43 
   44 TransverseMercatorProjection::~TransverseMercatorProjection() {}
   45 
   46 bool TransverseMercatorProjection::acceptsProj4(const std::string& proj4Str)
   47 {
   48     return proj4ProjectionMatchesName(proj4Str, "tmerc") || proj4ProjectionMatchesName(proj4Str, "utm") || proj4ProjectionMatchesName(proj4Str, "gstmerc") ||
   49            proj4ProjectionMatchesName(proj4Str, "etmerc");
   50 }
   51 
   52 std::vector<CDMAttribute> TransverseMercatorProjection::parametersFromProj4(const std::string& proj4Str)
   53 {
   54     vector<CDMAttribute> attrs;
   55     if (!acceptsProj4(proj4Str)) return attrs;
   56 
   57     attrs.push_back(CDMAttribute("grid_mapping_name", "transverse_mercator"));
   58 
   59     if (proj4ProjectionMatchesName(proj4Str, "utm")) {
   60         attrs.push_back(CDMAttribute("scale_factor_at_central_meridian", 0.9996));
   61         attrs.push_back(CDMAttribute("latitude_of_projection_origin", 0));
   62         attrs.push_back(CDMAttribute("false_easting", 500000));
   63         std::smatch what;
   64         double longOfProjOrigin = 0;
   65         if (std::regex_search(proj4Str, what, std::regex("\\+zone=(\\d+)"))) {
   66             short zone = string2type<short>(what[1].str());
   67             if (zone <= 0) zone = 1;
   68             else if (zone > 60) zone = 60;
   69             longOfProjOrigin = (zone-1)*6 - 180 + 3;
   70             attrs.push_back(CDMAttribute("longitude_of_central_meridian", longOfProjOrigin));
   71         }
   72         // some people use utm with lon_0 instead of zone
   73         if (std::regex_search(proj4Str, what, std::regex("\\+lon_0=(\\d+|\\d+\\.\\d+)"))) {
   74             longOfProjOrigin = string2type<double>(what[1].str());
   75             attrs.push_back(CDMAttribute("longitude_of_central_meridian", longOfProjOrigin));
   76         }
   77         if (std::regex_search(proj4Str, what, std::regex("\\+south"))) {
   78             attrs.push_back(CDMAttribute("false_northing", 10000000));
   79         }
   80         LOG4FIMEX(logger, Logger::WARN, "proj4 utm projection is only valid 6 deg. from center-longitude. Consider using: +proj=gstmerc (or etmerc proj4.8, NGA rec.) +k=0.9996 +lon_0="<< longOfProjOrigin << "+x_0=500000");
   81     } else { // projection given as tmerc or gstmerc
   82         if (proj4ProjectionMatchesName(proj4Str, "tmerc")) {
   83             LOG4FIMEX(logger, Logger::WARN, "proj4 tmerc projection is only valid 6 deg. from center-longitude. Consider using: +proj=etmerc (proj4.8, NGA recommendation) or +proj=gstmerc (proj 4.7) or etmerc (proj >=4.8)");
   84         }
   85         // longitude at origin
   86         double longOfProjOrigin = 0.;
   87         std::smatch what;
   88         if (std::regex_search(proj4Str, what, std::regex("\\+lon_0=(\\S+)"))) {
   89             longOfProjOrigin = string2type<double>(what[1].str());
   90         }
   91         attrs.push_back(CDMAttribute("longitude_of_central_meridian", longOfProjOrigin));
   92 
   93         // standard_parallel or scale_factor
   94         if (std::regex_search(proj4Str, what, std::regex("\\+lat_0=(\\S+)"))) {
   95             double latOfProjOrigin = string2type<double>(what[1].str());
   96             attrs.push_back(CDMAttribute("latitude_of_projection_origin", latOfProjOrigin));
   97         }
   98 
   99         if (std::regex_search(proj4Str, what, std::regex("\\+k=(\\S+)"))) {
  100             attrs.push_back(CDMAttribute("scale_factor_at_central_meridian", string2type<double>(what[1].str())));
  101         }
  102     }
  103     proj4GetEarthAttributes(proj4Str, attrs);
  104     attrs.push_back(CDMAttribute("proj4", proj4Str));
  105     return attrs;
  106 }
  107 
  108 std::ostream& TransverseMercatorProjection::getProj4ProjectionPart(std::ostream& oproj) const
  109 {
  110 #if PJ_VERSION > 480
  111     oproj << "+proj=etmerc";
  112 #else
  113 #if PJ_VERSION > 470
  114     oproj << "+proj=gstmerc";
  115 #else
  116     oproj << "+proj=tmerc";
  117 #endif
  118 #endif
  119     addParameterToStream(oproj, "longitude_of_central_meridian", " +lon_0=");
  120     addParameterToStream(oproj, "latitude_of_projection_origin", " +lat_0=");
  121     addParameterToStream(oproj, "scale_factor_at_central_meridian", " +k_0=");
  122     return oproj;
  123 }
  124 
  125 } // namespace MetNoFimex