1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    GeoConvHelper.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @date    2006-08-01
15 /// @version $Id$
16 ///
17 // static methods for processing the coordinates conversion for the current net
18 /****************************************************************************/
19 #ifndef GeoConvHelper_h
20 #define GeoConvHelper_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <map>
29 #include <string>
30 #include <utils/geom/Position.h>
31 #include <utils/geom/Boundary.h>
32 
33 #ifdef PROJ_API_FILE
34 #include PROJ_API_FILE
35 #ifdef PROJ_VERSION_MAJOR
36 typedef PJ* projPJ;
37 #endif
38 #endif
39 
40 
41 // ===========================================================================
42 // class declarations
43 // ===========================================================================
44 
45 class OptionsCont;
46 class PositionVector;
47 class OutputDevice;
48 
49 // ===========================================================================
50 // class definitions
51 // ===========================================================================
52 /**
53  * @class GeoConvHelper
54  * @brief static methods for processing the coordinates conversion for the current net
55  */
56 class GeoConvHelper {
57 public:
58 
59     /**@brief Constructor based on the stored options
60      * @param[in] oc The OptionsCont from which to read options
61      */
62     GeoConvHelper(OptionsCont& oc);
63 
64     /// @brief Constructor
65     GeoConvHelper(const std::string& proj, const Position& offset,
66                   const Boundary& orig, const Boundary& conv, double scale = 1.0, double rot = 0.0, bool inverse = false, bool flatten = false);
67 
68     /// @brief Destructor
69     ~GeoConvHelper();
70 
71     /**@brief Adds projection options to the given container
72      * @param[in] oc The options container to add the options to
73      * @todo let the container be retrieved
74      */
75     static void addProjectionOptions(OptionsCont& oc);
76 
77     /// @brief Initialises the processing and the final instance using the given options
78     static bool init(OptionsCont& oc);
79 
80     /// @brief Initialises the processing and the final instance using the given proj.4-definition and complete network parameter
81     static void init(const std::string& proj, const Position& offset, const Boundary& orig,
82                      const Boundary& conv, double scale = 1.0);
83 
84     /**@brief the coordinate transformation to use for input conversion and processing
85      * @note instance is modified during use: boundary may adapt to new coordinates
86      */
getProcessing()87     static GeoConvHelper& getProcessing() {
88         return myProcessing;
89     }
90 
91     /// @brief the coordinate transformation that was loaded fron an input file
getLoaded()92     static GeoConvHelper& getLoaded() {
93         return myLoaded;
94     }
95 
getNumLoaded()96     static int getNumLoaded() {
97         return myNumLoaded;
98     }
99 
100     /**@brief compute the location attributes which will be used for output
101      * based on the loaded location data, the given options and the transformations applied during processing
102      */
103     static void computeFinal(bool lefthand = false);
104 
105     /// @brief the coordinate transformation for writing the location element and for tracking the original coordinate system
getFinal()106     static const GeoConvHelper& getFinal() {
107         return myFinal;
108     }
109 
110     /// @brief sets the coordinate transformation loaded from a location element
111     static void setLoaded(const GeoConvHelper& loaded);
112 
113     /// @brief @brief resets loaded location elements
114     static void resetLoaded();
115 
116     /// @brief Converts the given cartesian (shifted) position to its geo (lat/long) representation
117     void cartesian2geo(Position& cartesian) const;
118 
119     /**@brief Converts the given coordinate into a cartesian and optionally update myConvBoundary
120      * @note: initializes UTM / DHDN projection on first use (select zone)
121      */
122     bool x2cartesian(Position& from, bool includeInBoundary = true);
123 
124     /// @brief Converts the given coordinate into a cartesian using the previous initialisation
125     bool x2cartesian_const(Position& from) const;
126 
127     /// @brief Returns whether a transformation from geo to metric coordinates will be performed
128     bool usingGeoProjection() const;
129 
130     /// @brief Returns the information whether an inverse transformation will happen
131     bool usingInverseGeoProjection() const;
132 
133     /// @brief Shifts the converted boundary by the given amounts
134     void moveConvertedBy(double x, double y);
135 
136     /// @brief Returns the original boundary
137     const Boundary& getOrigBoundary() const;
138 
139     /// @brief Returns the converted boundary
140     const Boundary& getConvBoundary() const;
141 
142     /// @brief sets the converted boundary
setConvBoundary(const Boundary & boundary)143     void setConvBoundary(const Boundary& boundary) {
144         myConvBoundary = boundary;
145     }
146 
147     /// @brief Returns the network offset
148     const Position getOffset() const;
149 
150     /// @brief Returns the network base
151     const Position getOffsetBase() const;
152 
153     /// @brief Returns the original projection definition
154     const std::string& getProjString() const;
155 
156     /// @brief @brief writes the location element
157     static void writeLocation(OutputDevice& into);
158 
159     bool operator==(const GeoConvHelper& o) const;
160 
161     bool operator!=(const GeoConvHelper& o) const {
162         return !(*this == o);
163     }
164 
165 private:
166     /// @brief projection method
167     enum ProjectionMethod {
168         NONE,
169         SIMPLE,
170         UTM,
171         DHDN,
172         DHDN_UTM,
173         PROJ
174     };
175 
176     /// @brief A proj options string describing the proj.4-projection to use
177     std::string myProjString;
178 
179 #ifdef PROJ_API_FILE
180     /// @brief The proj.4-projection to use
181     projPJ myProjection;
182 
183     /// @brief The inverse proj.4-projection to use first
184     projPJ myInverseProjection;
185 
186     /// @brief The geo proj.4-projection which is the target of the inverse projection
187     projPJ myGeoProjection;
188 #endif
189 
190     /// @brief The offset to apply
191     Position myOffset;
192 
193     /// @brief The scaling to apply to geo-coordinates
194     double myGeoScale;
195 
196     /// @brief The rotation to apply to geo-coordinates
197     double mySin;
198     double myCos;
199 
200     /// @brief Information whether no projection shall be done
201     ProjectionMethod myProjectionMethod;
202 
203     /// @brief Information whether inverse projection shall be used
204     bool myUseInverseProjection;
205 
206     /// @brief whether to discard z-data
207     bool myFlatten;
208 
209     /// @brief The boundary before conversion (x2cartesian)
210     Boundary myOrigBoundary;
211 
212     /// @brief The boundary after conversion (x2cartesian)
213     Boundary myConvBoundary;
214 
215     /// @brief coordinate transformation to use for input conversion and processing
216     static GeoConvHelper myProcessing;
217 
218     /// @brief coordinate transformation loaded from a location element
219     static GeoConvHelper myLoaded;
220 
221     /// @brief coordinate transformation to use for writing the location element and for tracking the original coordinate system
222     static GeoConvHelper myFinal;
223 
224     /// @brief the numer of coordinate transformations loaded from location elements
225     static int myNumLoaded;
226 
227     /// @brief make assignment operator private
228     GeoConvHelper& operator=(const GeoConvHelper&);
229 
230     /// @brief invalidated copy constructor.
231     GeoConvHelper(const GeoConvHelper&) = delete;
232 };
233 
234 
235 #endif
236 
237 /****************************************************************************/
238 
239