SRSExamples.cpp
Go to the documentation of this file.
1 #include "SRSExamples.h"
2 
3 #include <terralib/common.h>
4 #include <terralib/geometry.h>
5 #include <terralib/srs.h>
6 
7 #include <iostream>
8 
10 {
11  std::vector<unsigned int> epsgs;
12 
13  // test codes to the main CS used in Brazil:
14 
15  //Geographic CS: Corrego Alegre, WGS84, SIRGAS 2000 and SAD69
16  epsgs.push_back(TE_SRS_CORREGO_ALEGRE); epsgs.push_back(TE_SRS_WGS84); epsgs.push_back(TE_SRS_SIRGAS2000); epsgs.push_back(TE_SRS_SAD69);
17 
18  // UTM SAD69 North
19  for (size_t i=TE_SRS_SAD69_UTM_ZONE_18N; i<=TE_SRS_SAD69_UTM_ZONE_22N; epsgs.push_back(i), ++i);
20 
21  // UTM SAD69 South
22  for (size_t i=TE_SRS_SAD69_UTM_ZONE_17S; i<=TE_SRS_SAD69_UTM_ZONE_25S; epsgs.push_back(i), ++i);
23 
24  // UTM Corrego Alegre South
25  for (size_t i=TE_SRS_CORREGO_ALEGRE_UTM_ZONE_21S; i<=TE_SRS_CORREGO_ALEGRE_UTM_ZONE_25S; epsgs.push_back(i), ++i);
26 
27  // UTM WGS84 North
28  for (size_t i=TE_SRS_WGS84_UTM_ZONE_18N; i<=TE_SRS_WGS84_UTM_ZONE_22N; epsgs.push_back(i), ++i);
29 
30  // UTM WGS84 South
31  for (size_t i=TE_SRS_WGS84_UTM_ZONE_17S; i<=TE_SRS_WGS84_UTM_ZONE_25S; epsgs.push_back(i), ++i);
32 
33  // UTM SIRGAS North
34  for (size_t i=TE_SRS_SIRGAS2000_UTM_ZONE_18N; i<=TE_SRS_SIRGAS2000_UTM_ZONE_22N; epsgs.push_back(i), ++i);
35 
36  // Polyconic SAD69
37  epsgs.push_back(TE_SRS_SAD69_POLYCONIC);
38 
39  // Antartic Polar Stereographic WGS84
41 
42  std::cout << "Describing coordinate systems by ID - begin" << std::endl;
43  std::string name;
44  std::string wkt;
45  std::string p4txt;
46  std::pair<std::string,unsigned int> srid;
47  for (size_t i=0; i<epsgs.size();++i)
48  {
49  std::cout << "\n -- " << epsgs[i] << " -- \n";
50  name = te::srs::SpatialReferenceSystemManager::getInstance().getName(epsgs[i]);
51  std::cout << " Name: " << name;
52  srid = te::srs::SpatialReferenceSystemManager::getInstance().getIdFromName(name);
53  if (srid.second == epsgs[i])
54  std::cout << ". Id from name OK" << std::endl;
55  else
56  std::cout << ". Id from name NOT OK" << std::endl;
57 
59  std::cout << " WKT: " << wkt.substr(0,60) << "... ";
60  srid = te::srs::SpatialReferenceSystemManager::getInstance().getIdFromWkt(wkt);
61  if (srid.second == epsgs[i])
62  std::cout << ". Id from wkt OK" << std::endl;
63  else
64  std::cout << " Id from wkt NOT OK" << std::endl;
65 
66  p4txt = te::srs::SpatialReferenceSystemManager::getInstance().getP4Txt(epsgs[i]);
67  std::cout << " PROJ4: " << p4txt.substr(0,60) << "... ";
68  srid = te::srs::SpatialReferenceSystemManager::getInstance().getIdFromP4Txt(p4txt);
69  if (srid.second == epsgs[i])
70  std::cout << ". Id from PROJ4 txt OK" << std::endl;
71  else
72  std::cout << ". Id from PROJ4 txt NOT OK" << std::endl;
73  }
74  std::cout << "\nDescribing coordinate systems by ID - end " << std::endl << std::endl;
75 
76 }
77 
78 
80 {
81  std::cout << "Showing how SRID's are recognized by the converter - begin" << std::endl;
82 
83  std::vector<int> epsgs;
84 
85  // test codes to the main CS used in Brazil:
86 
87  // Geographic CS: Corrego Alegre, WGS84, SIRGAS 2000 and SAD69
88  epsgs.push_back(TE_SRS_CORREGO_ALEGRE); epsgs.push_back(TE_SRS_WGS84); epsgs.push_back(TE_SRS_SIRGAS2000); epsgs.push_back(TE_SRS_SAD69);
89 
90  // UTM SAD69 North
91  for (size_t i=TE_SRS_SAD69_UTM_ZONE_18N; i<=TE_SRS_SAD69_UTM_ZONE_22N; epsgs.push_back(i), ++i);
92 
93  // UTM SAD69 South
94  for (size_t i=TE_SRS_SAD69_UTM_ZONE_17S; i<=TE_SRS_SAD69_UTM_ZONE_25S; epsgs.push_back(i), ++i);
95 
96  // UTM Corrego Alegre South
97  for (size_t i=TE_SRS_CORREGO_ALEGRE_UTM_ZONE_21S; i<=TE_SRS_CORREGO_ALEGRE_UTM_ZONE_25S; epsgs.push_back(i), ++i);
98 
99  // UTM WGS84 North
100  for (size_t i=TE_SRS_WGS84_UTM_ZONE_18N; i<=TE_SRS_WGS84_UTM_ZONE_22N; epsgs.push_back(i), ++i);
101 
102  // UTM WGS84 South
103  for (size_t i=TE_SRS_WGS84_UTM_ZONE_17S; i<=TE_SRS_WGS84_UTM_ZONE_25S; epsgs.push_back(i), ++i);
104 
105  // UTM SIRGAS North
106  for (size_t i=TE_SRS_SIRGAS2000_UTM_ZONE_18N; i<=TE_SRS_SIRGAS2000_UTM_ZONE_22N; epsgs.push_back(i), ++i);
107 
108  // Polyconic SAD69
109  epsgs.push_back(TE_SRS_SAD69_POLYCONIC);
110 
111  // Antartic Polar Stereographic WGS84
113 
114  std::unique_ptr<te::srs::Converter> converter(new te::srs::Converter());
115  for (size_t i=0; i<epsgs.size();++i)
116  {
117  try
118  {
119  converter->setSourceSRID(epsgs[i]);
120  std::cout << "SRS identifier " << epsgs[i] << " recognized\n";
121  }
122  catch (...) {
123  std::cout << "SRS identifier " << epsgs[i] << " NOT recognized\n";
124  }
125  }
126  std::cout << "Showing how SRID's are recognized by the converter - end " << std::endl << std::endl;
127 }
128 
129 
131 {
132  std::cout << "Showing how Convert coordinates - end " << std::endl << std::endl;
133 
134  std::cout << "Converting a geometry: " << std::endl;
135  te::gm::Geometry* geom = new te::gm::Point(-45.0,-23.0,TE_SRS_WGS84);
136  std::cout << geom->asText() << std::endl;
137 
138  std::cout << std::endl;
139 
141  std::cout << geom->asText() << std::endl;
142 
143  delete geom;
144 
145  std::cout << std::endl;
146  std::cout.setf(std::ios::fixed,std::ios::floatfield);
147  std::cout.precision(5);
148 
149  std::cout << "Converting one Coordinate: " << std::endl;
150  std::unique_ptr<te::srs::Converter> converter(new te::srs::Converter());
151  converter->setSourceSRID(TE_SRS_WGS84);
152  converter->setTargetSRID(TE_SRS_WGS84_UTM_ZONE_23S);
153 
154  double llX = -45.0;
155  double llY = 0.0;
156  double xyX;
157  double xyY;
158 
159  converter->convert(llX,llY,xyX,xyY);
160  std::cout << llX << " " << llY << " >>>> " << xyX << " " << xyY << std::endl;
161 
162  llX = 0;
163  llY = 0;
164  converter->invert(xyX,xyY,llX,llY);
165 
166  std::cout << llX << " " << llY << " <<<< " << xyX << " " << xyY << std::endl;
167 
168  llX = -45.0;
169  llY = -23.0;
170 
171  std::cout << std::endl;
172  std::cout << "Converting a list of coordinates: " << std::endl;
173 
174  double* xs = new double[3];
175  double* ys = new double[3];
176 
177  xs[0] = -45.5;
178  xs[1] = -45.6;
179  xs[2] = -45.7;
180 
181  ys[0] = -23.0;
182  ys[1] = -23.1;
183  ys[2] = -23.2;
184 
185  converter->convert(xs,ys,3,1);
186 
187  delete [] xs;
188  delete [] ys;
189 
190  std::cout << std::endl;
191  std::cout << "Converting a coordinate after include a CS to the Manager: " << std::endl;
192 
193  std::string p4txt = "+proj=utm +zone=23 +south +ellps=WGS84 +datum=WGS84 +units=km +no_defs";
194  converter->setSourcePJ4txt(p4txt);
195  converter->setTargetSRID(TE_SRS_WGS84);
196 
197  xyX = 500000 * 0.001; // meter to kilometer
198  xyY = 10000000 * 0.001; // meter to kilometer
199 
200  converter->convert(xyX,xyY,llX,llY);
201 
202  std::cout << xyX << " " << xyY << " >>>> " << llX << " " << llY << std::endl;
203 
204  std::cout << "Showing how Convert coordinates - end " << std::endl << std::endl;
205 }
void ConvertCoordinates()
This example shows how to transform a geometry from an SRS to another.
This file contains include headers for TerraLib Spatial Reference System module.
Several examples on how to use Spatial Reference Systens handling in TerraLib.
#define TE_SRS_SAD69_UTM_ZONE_22N
#define TE_SRS_CORREGO_ALEGRE_UTM_ZONE_21S
#define TE_SRS_SAD69
#define TE_SRS_SAD69_POLYCONIC
#define TE_SRS_WGS84_UTM_ZONE_25S
#define TE_SRS_WGS84_ANTARTIC_POLAR_STEREOGRAPHIC
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
#define TE_SRS_SIRGAS2000_UTM_ZONE_18N
void SpatialReferenceSystemManager()
This example shows how to use the Spatial Reference Systems Manager.
Definition: SRSExamples.cpp:9
std::string asText() const _NOEXCEPT_OP(true)
It returns an string with the Well-Known Text Representation for the geometry.
void RecognizeSRIDs()
This example shows how Coordinate Systems from identifiers are recognized by the converter.
Definition: SRSExamples.cpp:79
#define TE_SRS_CORREGO_ALEGRE_UTM_ZONE_25S
#define TE_SRS_SAD69_UTM_ZONE_25S
#define TE_SRS_WGS84_UTM_ZONE_23S
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Definition: Converter.h:53
#define TE_SRS_SIRGAS2000
#define TE_SRS_WGS84_UTM_ZONE_17S
This file contains include headers for the TerraLib Common Runtime module.
#define TE_SRS_WGS84_UTM_ZONE_18N
This file contains include headers for the Vector Geometry model of TerraLib.
virtual void transform(int srid) _NOEXCEPT_OP(false)=0
It converts the coordinate values of the geometry to the new spatial reference system.
#define TE_SRS_CORREGO_ALEGRE
#define TE_SRS_WGS84_UTM_ZONE_22N
#define TE_SRS_SAD69_UTM_ZONE_17S
#define TE_SRS_SAD69_UTM_ZONE_18N
#define TE_SRS_WGS84
#define TE_SRS_SIRGAS2000_UTM_ZONE_22N