TsGEOSWriterReader.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 // Unit-Test TerraLib
21 #include "TsGEOSWriterReader.h"
22 #include "../Config.h"
23 
24 // TerraLib
25 #include <terralib/common.h>
28 #include <terralib/geometry.h>
32 
33 // STL
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cstring>
37 
39 
41 {
42 }
43 
45 {
46 }
47 
49 {
50 //#ifdef TE_COMPILE_ALL
51 
52 // load geometries from a file
53  std::vector<WKBEntry> hwkbVec;
54  loadGeometry(hwkbVec);
55 
56  for(size_t i = 0; i < hwkbVec.size(); ++i)
57  {
58  const WKBEntry& hwkb = hwkbVec[i];
59 
60 // convert WKT to a geometry
62  g1->setSRID(hwkb.m_srid);
63 
64 // Suppose WKTReader is ok...
65  //te::gm::WKTReader rt;
66  //te::gm::Geometry* g1 = rt.read("POLYGON((10 18,15 18,15 22,10 22,10 18),(11 20,12 20,12 21,11 21,11 20),(13 19,14 19,14 20,13 20,13 19))");
67 
68  std::size_t wkb1size = 0;
69  wkb1size = g1->getWkbSize();
70  char* wkb1 = new char[wkb1size];
72 
73 // create a new geometry g2 using wkb1 from g1 => g2 must be equal g1
75  CPPUNIT_ASSERT(g2->equals(g1));
76 
77 // checking hex and binary
78  char* hwkb1 = te::core::Binary2Hex(wkb1, wkb1size);
79  std::size_t wkb2size = g2->getWkbSize();
80  CPPUNIT_ASSERT(wkb1size == wkb2size);
81 
82  char* wkb2 = new char[wkb2size];
84  char* hwkb2 = te::core::Binary2Hex(wkb2, wkb2size);
85 
86  CPPUNIT_ASSERT(strcmp(hwkb1, hwkb2) == 0);
87  CPPUNIT_ASSERT(memcmp(wkb1, wkb2, wkb2size) == 0);
88 
89 // Testing WKBReader
90  te::gm::Geometry* geomRead;
91  CPPUNIT_ASSERT_NO_THROW(geomRead = te::gm::WKBReader::read(wkb1));
92  CPPUNIT_ASSERT(geomRead);
93  CPPUNIT_ASSERT(geomRead->equals(g1));
94  size_t size1 = geomRead->getWkbSize();
95  char* wkb1Read = new char [size1];
97  CPPUNIT_ASSERT(memcmp(wkb1, wkb1Read, size1) == 0);
98 
99  const char* validHWKB = hwkb.m_hwkb.c_str();
100  CPPUNIT_ASSERT(strcmp(hwkb1, validHWKB) == 0);
101 
102  delete [] wkb1;
103  delete [] wkb2;
104  delete [] hwkb1;
105  delete [] hwkb2;
106  delete [] wkb1Read;
107  }
108 
109  //#endif
110 }
111 
113 {
114 //#ifdef TE_COMPILE_ALL
115 
116 // load geometries from a file
117  std::vector<std::string> wktGeom;
118 
119  loadWKT("/data/wkt_point.txt", wktGeom);
120  for(size_t i = 0; i < wktGeom.size(); ++i)
121  {
122  std::string rr_wkt = wktGeom[i];
123 
124 // Suppose WKTReader is ok...
125  te::gm::Geometry* g1;
126  CPPUNIT_ASSERT_NO_THROW(g1 = te::gm::WKTReader::read(rr_wkt.c_str()));
127 
128 // Testing GeosWriter and GeosReader -------
129  geos::geom::Geometry* geos_geom = te::gm::GEOSWriter::write(g1);
130  te::gm::Geometry* tl_geom_from_geos = te::gm::GEOSReader::read(geos_geom);
131  CPPUNIT_ASSERT(g1->equals(tl_geom_from_geos));
132  }
133 
134 //#endif
135 }
136 
138 {
139 //#ifdef TE_COMPILE_ALL
140 
141 // load geometries from a file
142  std::vector<std::string> wktGeom;
143 
144  loadWKT("/data/wkt_line.txt", wktGeom);
145  for(size_t i = 0; i < wktGeom.size(); ++i)
146  {
147  std::string rr_wkt = wktGeom[i];
148 
149 // Suppose WKTReader is ok...
150  te::gm::Geometry* g1;
151  CPPUNIT_ASSERT_NO_THROW(g1 = te::gm::WKTReader::read(rr_wkt.c_str()));
152 
153 // Testing GeoWriter and GeosReader -------
154  geos::geom::Geometry* geos_geom = te::gm::GEOSWriter::write(g1);
155  te::gm::Geometry* tl_geom_from_geos = te::gm::GEOSReader::read(geos_geom);
156  CPPUNIT_ASSERT(g1->equals(tl_geom_from_geos));
157 
158  }
159 
160  //#endif
161 }
162 
164 {
165 //#ifdef TE_COMPILE_ALL
166 
167 // load geometries from a file
168  std::vector<std::string> wktGeom;
169 
170  loadWKT("/data/wkt_polygon.txt", wktGeom);
171  for(size_t i = 0; i < wktGeom.size(); ++i)
172  {
173  std::string rr_wkt = wktGeom[i];
174 
175 // Suppose WKTReader is ok...
176  te::gm::Geometry* g1;
177  CPPUNIT_ASSERT_NO_THROW(g1 = te::gm::WKTReader::read(rr_wkt.c_str()));
178 
179 // Testing GeoWriter and GeosReader -------
180  geos::geom::Geometry* geos_geom = te::gm::GEOSWriter::write(g1);
181  te::gm::Geometry* tl_geom_from_geos = te::gm::GEOSReader::read(geos_geom);
182  CPPUNIT_ASSERT(g1->equals(tl_geom_from_geos));
183  }
184 //#endif
185 }
186 
188 {
189 //#ifdef TE_COMPILE_ALL
190 
191 // load geometries from a file
192  std::vector<std::string> wktGeom;
193 
194  loadWKT("/data/wkt_geomcollection.txt", wktGeom);
195  for(size_t i = 0; i < wktGeom.size(); ++i)
196  {
197  std::string rr_wkt = wktGeom[i];
198 
199 // Suppose WKTReader is ok...
200  te::gm::Geometry* g1;
201  CPPUNIT_ASSERT_NO_THROW(g1 = te::gm::WKTReader::read(rr_wkt.c_str()));
202 
203 // Testing GeoWriter and GeosReader -------
204  geos::geom::Geometry* geos_geom = te::gm::GEOSWriter::write(g1);
205  te::gm::Geometry* tl_geom_from_geos = te::gm::GEOSReader::read(geos_geom);
206  CPPUNIT_ASSERT(g1->equals(tl_geom_from_geos));
207  }
208 //#endif
209 }
210 
211 void TsGEOSWriterReader::loadGeometry(std::vector<WKBEntry>& hwkbVec) const
212 {
213  hwkbVec.clear();
214 
215  FILE* f = fopen( te::core::CharEncoding::fromUTF8(TERRALIB_DATA_DIR "/hwkb.csv").c_str(), "r");
216 
217  CPPUNIT_ASSERT(f != NULL);
218 
219  while(!feof(f))
220  {
221  std::vector<std::string> values;
222  std::string value("");
223 
224  while(!feof(f))
225  {
226  char c = (char)(fgetc(f));
227 
228  if(c == '\n')
229  {
230  values.push_back(value);
231  break;
232  }
233 
234  if(c == ';')
235  {
236  values.push_back(value);
237  value = "";
238  }
239  else
240  value += c;
241  }
242 
243  if(values.size() != 9)
244  break;
245 
246  WKBEntry fe;
247 
248  fe.m_hwkb = values[0];
249  fe.m_srid = atoi(values[1].c_str());
250  fe.m_geomType = values[2];
251  fe.m_dimensonality = atoi(values[3].c_str());
252  fe.m_ndims = atoi(values[4].c_str());
253  fe.m_wkt = values[5];
254  fe.m_geomTypeId = atoi(values[6].c_str());
255  fe.m_is3D = values[7] == "t";
256  fe.m_isMeasured = values[8] == "t";
257 
258  hwkbVec.push_back(fe);
259  }
260 
261  fclose(f);
262 }
263 
264 void TsGEOSWriterReader::loadWKT(std::vector<std::string>& geom_wkt) const
265 {
266  geom_wkt.clear();
267 
268  FILE* f = fopen( te::core::CharEncoding::fromUTF8(TERRALIB_DATA_DIR "/wkt_line.txt").c_str(), "r");
269 
270  CPPUNIT_ASSERT(f != NULL);
271 
272  while(!feof(f))
273  {
274  std::vector<std::string> values;
275  std::string value("");
276 
277  while(!feof(f))
278  {
279  char c = (char)(fgetc(f));
280 
281  if(c == '\n')
282  {
283  geom_wkt.push_back(value);
284  break;
285  }
286  else
287  value += c;
288  }
289  }
290  fclose(f);
291 }
292 
293 void TsGEOSWriterReader::loadWKT(std::string filewkt, std::vector<std::string>& geom_wkt) const
294 {
295  geom_wkt.clear();
296 
297  std::string filename = TERRALIB_DATA_DIR + filewkt;
298  FILE* f = fopen( te::core::CharEncoding::fromUTF8(filename).c_str() , "r");
299 
300  CPPUNIT_ASSERT(f != NULL);
301 
302  while(!feof(f))
303  {
304  std::vector<std::string> values;
305  std::string value("");
306 
307  while(!feof(f))
308  {
309  char c = (char)(fgetc(f));
310 
311  if(c == '\n')
312  {
313  geom_wkt.push_back(value);
314  break;
315  }
316  else
317  value += c;
318  }
319  }
320  fclose(f);
321 }
void tcCreateGeomCollectionFromWKTAndApplyGEOSTests()
Test Case: Creating PPLYGONS Geom from WKT and apply GEOS Write and Read test.
void loadWKT(std::vector< std::string > &geom_wkt) const
Test suite for the GEOSWriter and GEOSReader class.
void tcCreateGeometriesAndApplyReadTests()
Test Case: Creating basic Geom and apply read test.
Test suite for the GEOSWriter and GEOSReader class.
static const MachineByteOrder sm_machineByteOrder
A flag that indicates the machine byte order (Big Endian or Little Endian).
void tcCreateLinesFromWKTAndApplyGEOSTests()
Test Case: Creating LINES Geom from WKT and apply GEOS Write and Read test.
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
std::size_t getWkbSize() const _NOEXCEPT_OP(true)
It returns the size required by a WKB representation for this geometric object.
An static class with global definitions.
void loadGeometry(std::vector< WKBEntry > &hwkbVec) const
Auxiliary method for reading geometry information from an ASCII file.
This file contains several utilities functions for dealing with HEX strings.
A class for handling character enconding/decoding.
static geos::geom::Geometry * write(const Geometry *teGeom)
It reads a TerraLib geometry and make a GEOS geometry.
A class that converts a GEOS geometry to a TerraLib geometry.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A class that converts a TerraLib geometry to a GEOS geometry.
virtual void setSRID(int srid) _NOEXCEPT_OP(true)=0
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
void tcCreatePolygonFromWKTAndApplyGEOSTests()
Test Case: Creating POLYGONS Geom from WKT and apply GEOS Write and Read test.
This file contains include headers for the TerraLib Common Runtime module.
This file contains include headers for the Vector Geometry model of TerraLib.
char * Binary2Hex(const char *s, std::size_t size)
Each char from the array of characters in binary format is converted into a pair of hex characters...
Definition: HexUtils.h:602
void tcCreatePointsFromWKTAndApplyGEOSTests()
Test Case: Creating POINTS Geom from WKT and apply GEOS Write and Read test.
CPPUNIT_TEST_SUITE_REGISTRATION(TsGEOSWriterReader)
static Geometry * read(const char *wkb)
It returns a valid geometry from a given WKB.
Definition: WKBReader.cpp:48
virtual bool equals(const Geometry *const rhs, const bool exact=false) const _NOEXCEPT_OP(false)
It returns true if the geometry object is spatially equal to rhs geometry.
void getWkb(char *wkb, te::common::MachineByteOrder byteOrder) const _NOEXCEPT_OP(false)
It serializes the geometry to a WKB representation into the specified buffer.
static Geometry * read(const char *wkt)
It returns a valid Geometry from a given WKT.