TsWKBReader.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 "TsWKBReader.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  /*
51 
52  WKTreader r;
53  te::gm::Geometry* g1 = r.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))");
54 
55 // testando o wkb
56  std::size_t wkb1size = 0;
57  wkb1size = g1->getWKBSize();
58  char* wkb1 = new char[wkb1size];
59  g1->getWKB(wkb1);
60 
61  te::gm::Geometry* g2 = te::gm::Geometry::getGeomFromWKB(wkb1);
62 
63  ASSERT(g1->equals(g2)); // confirma se a escrita/leitura est� ok
64 
65  //////// posso desprezar
66  wkb2size = g2->getWKBSize();
67 
68  assert(wkb1size == wkb2size);
69 
70  char* wkb2 = new char[wkb2size];
71  g2->getWKB(wkb2);
72 
73  assert(memcmp(wkb1, wkb2) == 0);
74 
75 
76  */
77 //#ifdef TE_COMPILE_ALL
78 
79 // load geometries from a file
80  std::vector<WKBEntry> hwkbVec;
81  loadGeometry(hwkbVec);
82 
83  for(size_t i = 0; i < hwkbVec.size(); ++i)
84  {
85  const WKBEntry& hwkb = hwkbVec[i];
86 
87 // convert WKT to a geometry
89  g1->setSRID(hwkb.m_srid);
90 
91  std::size_t wkb1size = 0;
92  wkb1size = g1->getWkbSize();
93  char* wkb1 = new char[wkb1size];
95 
96 // create a new geometry g2 using wkb1 from g1 => g2 must be equal g1
98  CPPUNIT_ASSERT(g2->equals(g1));
99 
100 // checking hex and binary
101  char* hwkb1 = te::core::Binary2Hex(wkb1, wkb1size);
102  std::size_t wkb2size = g2->getWkbSize();
103  CPPUNIT_ASSERT(wkb1size == wkb2size);
104 
105  char* wkb2 = new char[wkb2size];
107  char* hwkb2 = te::core::Binary2Hex(wkb2, wkb2size);
108 
109  CPPUNIT_ASSERT(strcmp(hwkb1, hwkb2) == 0);
110  CPPUNIT_ASSERT(memcmp(wkb1, wkb2, wkb2size) == 0);
111 
112 // Testing WKBReader
113  te::gm::Geometry* geomRead;
114  CPPUNIT_ASSERT_NO_THROW(geomRead = te::gm::WKBReader::read(wkb1));
115  CPPUNIT_ASSERT(geomRead);
116  CPPUNIT_ASSERT(geomRead->equals(g1));
117  size_t size1 = geomRead->getWkbSize();
118  char* wkb1Read = new char [size1];
120  CPPUNIT_ASSERT(memcmp(wkb1, wkb1Read, size1) == 0);
121 
122  const char* validHWKB = hwkb.m_hwkb.c_str();
123  CPPUNIT_ASSERT(strcmp(hwkb1, validHWKB) == 0);
124 
125  delete [] wkb1;
126  delete [] wkb2;
127  delete [] hwkb1;
128  delete [] hwkb2;
129  delete [] wkb1Read;
130  delete g1; delete g2; delete geomRead;
131  }
132 
133  //#endif
134 }
135 
137 {
138 //#ifdef TE_COMPILE_ALL
139 
140 // load geometries from a file
141  std::vector<std::string> wktGeom;
142 
143  loadWKT("/data/wkt_geom.txt", wktGeom);
144  for(size_t i = 0; i < wktGeom.size(); ++i)
145  {
146  std::string rr_wkt = wktGeom[i];
147 
148 // Suppose WKTReader is ok...
149  te::gm::Geometry* g1;
150  CPPUNIT_ASSERT_NO_THROW(g1 = te::gm::WKTReader::read(rr_wkt.c_str()));
151 
152  std::size_t wkb1size = 0;
153  wkb1size = g1->getWkbSize();
154  char* wkb1 = new char[wkb1size];
156 
157 // Testing GeoWriter and GeosReader -------
158  geos::geom::Geometry* geos_geom = te::gm::GEOSWriter::write(g1);
159  te::gm::Geometry* tl_geom_from_geos = te::gm::GEOSReader::read(geos_geom);
160  CPPUNIT_ASSERT(g1->equals(tl_geom_from_geos));
161 // End of test GeoWriter and GeosReader ---
162 
163 
164 // Testing WKBReader based on g1 and wkb1
165  te::gm::Geometry* geomRead;
166  CPPUNIT_ASSERT_NO_THROW(geomRead = te::gm::WKBReader::read(wkb1));
167  CPPUNIT_ASSERT(geomRead->equals(g1));
169  CPPUNIT_ASSERT(g2->equals(g1));
170 
171  size_t size1 = geomRead->getWkbSize();
172  char* wkb1Read = new char [size1];
173  geomRead->getWkb(wkb1Read, te::common::Globals::sm_machineByteOrder);
174  CPPUNIT_ASSERT(memcmp(wkb1Read, wkb1, size1) == 0);
175 
176  std::size_t wkb2size = g2->getWkbSize();
177  char* wkb2 = new char[wkb2size];
179  CPPUNIT_ASSERT(wkb1size == wkb2size);
180  CPPUNIT_ASSERT(memcmp(wkb1, wkb2, wkb2size) == 0);
181 
182 //Testing Binary2Hex
183  char* hwkb1 = te::core::Binary2Hex(wkb1, wkb1size);
184  char* hwkb2 = te::core::Binary2Hex(wkb2, wkb2size);
185  CPPUNIT_ASSERT(strcmp(hwkb1, hwkb2) == 0);
186 
187  delete [] wkb1;
188  delete [] wkb2;
189  delete [] hwkb1;
190  delete [] hwkb2;
191  delete [] wkb1Read;
192  delete g1; delete g2; delete geomRead;
193  //delete geos_geom; //TODO: deletion of pointer to incomplete type geos::geom::Geometry - no destructor called
194  }
195 
196  //#endif
197 }
198 
200 {
201 //#ifdef TE_COMPILE_ALL
202 // load test data
203  std::vector<WKBEntry> hwkbVec;
204  loadGeometry(hwkbVec);
205 
206 // for each entry, test geometry basic methods
207  for(size_t i = 0; i < hwkbVec.size(); ++i)
208  {
209  const WKBEntry& hwkb = hwkbVec[i];
210 
211 // convert HWKB to WKB and read the WKB into a TL geometry
212  std::unique_ptr<te::gm::Geometry> g(te::gm::WKBReader::read(te::core::Hex2Binary(hwkb.m_hwkb.c_str())));
213  CPPUNIT_ASSERT(g.get());
214  g->setSRID(hwkb.m_srid);
215 
216 // test geometry basic methods
217  CPPUNIT_ASSERT(g->getDimension() == static_cast<te::gm::Dimensionality>(hwkb.m_dimensonality));
218  CPPUNIT_ASSERT(g->getCoordinateDimension() == hwkb.m_ndims);
219  CPPUNIT_ASSERT(te::common::Convert2UCase(g->getGeometryType()) == hwkb.m_geomType);
220  CPPUNIT_ASSERT(g->getGeomTypeId() == static_cast<te::gm::GeomType>(hwkb.m_geomTypeId));
221  CPPUNIT_ASSERT(g->getSRID() == hwkb.m_srid);
222  //CPPUNIT_ASSERT_NO_THROW(g->transform(4128));
223  //CPPUNIT_ASSERT(g->getSRID() == 4128);
224  std::unique_ptr<te::gm::Geometry> envelope(g->getEnvelope());
225  CPPUNIT_ASSERT(envelope->getGeomTypeId() == te::gm::PolygonType);
226  CPPUNIT_ASSERT(g->getMBR());
227  std::cout << hwkb.m_wkt << std::endl;
228  std::string ustring = g->asText();
229  //CPPUNIT_ASSERT_MESSAGE("multipolygon->asText() in not working", te::common::Convert2UCase(g->asText()) == hwkb.m_wkt);
230  size_t wkbSize;
231  char* myWkb = g->asBinary(wkbSize);
232  char* myHwkb = te::core::Binary2Hex(myWkb, wkbSize);
233  delete [] myWkb;
234  CPPUNIT_ASSERT(myHwkb == hwkb.m_hwkb);
235  delete [] myHwkb;
236 
237  CPPUNIT_ASSERT(g->isEmpty() == false);
238 
239  if(g->getGeomTypeId() != te::gm::GeometryCollectionType)
240  {
241  CPPUNIT_ASSERT(g->isSimple() == true);
242  CPPUNIT_ASSERT(g->isValid() == true);
243  std::unique_ptr<te::gm::Geometry> boundary;
244  CPPUNIT_ASSERT_NO_THROW(boundary.reset(g->getBoundary()));
245  CPPUNIT_ASSERT(boundary.get());
246  }
247  CPPUNIT_ASSERT(g->is3D() == hwkb.m_is3D);
248  CPPUNIT_ASSERT(g->isMeasured() == hwkb.m_isMeasured);
249  }
250 
251 //#endif
252 }
253 
254 void TsWKBReader::loadGeometry(std::vector<WKBEntry>& hwkbVec) const
255 {
256  hwkbVec.clear();
257 
258  FILE* f = fopen( TERRALIB_DATA_DIR "/hwkb.csv", "r");
259 
260  CPPUNIT_ASSERT(f != NULL);
261 
262  while(!feof(f))
263  {
264  std::vector<std::string> values;
265  std::string value("");
266 
267  while(!feof(f))
268  {
269  char c = (char)(fgetc(f));
270 
271  if(c == '\n')
272  {
273  values.push_back(value);
274  break;
275  }
276 
277  if(c == ';')
278  {
279  values.push_back(value);
280  value = "";
281  }
282  else
283  value += c;
284  }
285 
286  if(values.size() != 9)
287  break;
288 
289  WKBEntry fe;
290 
291  fe.m_hwkb = values[0];
292  fe.m_srid = atoi(values[1].c_str());
293  fe.m_geomType = values[2];
294  fe.m_dimensonality = atoi(values[3].c_str());
295  fe.m_ndims = atoi(values[4].c_str());
296  fe.m_wkt = values[5];
297  fe.m_geomTypeId = atoi(values[6].c_str());
298  fe.m_is3D = values[7] == "t";
299  fe.m_isMeasured = values[8] == "t";
300 
301  hwkbVec.push_back(fe);
302  }
303 
304  fclose(f);
305 }
306 
307 void TsWKBReader::loadWKT(std::vector<std::string>& geom_wkt) const
308 {
309  geom_wkt.clear();
310 
311  FILE* f = fopen( te::core::CharEncoding::fromUTF8(TERRALIB_DATA_DIR "/wkt_line.txt").c_str() , "r");
312 
313  CPPUNIT_ASSERT(f != NULL);
314 
315  while(!feof(f))
316  {
317  std::vector<std::string> values;
318  std::string value("");
319 
320  while(!feof(f))
321  {
322  char c = (char)(fgetc(f));
323 
324  if(c == '\n')
325  {
326  geom_wkt.push_back(value);
327  break;
328  }
329  else
330  value += c;
331  }
332  }
333  fclose(f);
334 }
335 
336 void TsWKBReader::loadWKT(std::string filewkt, std::vector<std::string>& geom_wkt) const
337 {
338  geom_wkt.clear();
339 
340  std::string filename = TERRALIB_DATA_DIR + filewkt;
341  FILE* f = fopen( te::core::CharEncoding::fromUTF8(filename).c_str() , "r");
342 
343  CPPUNIT_ASSERT(f != NULL);
344 
345  while(!feof(f))
346  {
347  std::vector<std::string> values;
348  std::string value("");
349 
350  while(!feof(f))
351  {
352  char c = (char)(fgetc(f));
353 
354  if(c == '\n')
355  {
356  geom_wkt.push_back(value);
357  break;
358  }
359  else
360  value += c;
361  }
362  }
363  fclose(f);
364 }
void loadWKT(std::vector< std::string > &geom_wkt) const
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
char * Hex2Binary(const char *hex)
It converts each pair of hex characters from a NULL-terminated string of hex characters into a binary...
Definition: HexUtils.h:469
static const MachineByteOrder sm_machineByteOrder
A flag that indicates the machine byte order (Big Endian or Little Endian).
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:168
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 tcBasicGeometryMethods()
Test Case: testing the basic geometry methods.
void setUp()
Definition: TsWKBReader.cpp:40
Test suite for the geometry WKB Reader.
Test suite for the WKBReader class.
Definition: TsWKBReader.h:49
Dimensionality
From Wikipedia: "in mathematics, the dimension of an object is an intrinsic property, independent of the space in which the object may happen to be embedded".
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.
void loadGeometry(std::vector< WKBEntry > &hwkbVec) const
Auxiliary method for reading geometry information from an ASCII file.
A class that converts a TerraLib geometry to a GEOS geometry.
void tcCreateGeomFromWKTAndApplyReadTests()
Test Case: Creating basic Geom from WKT and apply read test.
void tcCreateGeometriesAndApplyReadTests()
Test Case: Creating basic Geom and apply read test.
Definition: TsWKBReader.cpp:48
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 tearDown()
Definition: TsWKBReader.cpp:44
This file contains include headers for the TerraLib Common Runtime module.
This file contains include headers for the Vector Geometry model of TerraLib.
std::string m_hwkb
Definition: TsWKBReader.h:87
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
std::string m_geomType
Definition: TsWKBReader.h:89
CPPUNIT_TEST_SUITE_REGISTRATION(TsWKBReader)
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.