27 #include "../common/ByteSwapUtils.h"
28 #include "../common/Enums.h"
29 #include "../common/Globals.h"
30 #include "../common/HexUtils.h"
31 #include "../common/Translator.h"
32 #include "../dataaccess/Exception.h"
33 #include "../geometry.h"
44 #ifdef TE_ENABLE_SPATIALITE
46 #include <spatialite/gaiageo.h>
53 const unsigned char** endptr,
55 unsigned char ewkbByteOrder,
59 assert(ewkb && endptr);
61 unsigned int nPts = 0;
62 memcpy(&nPts, ewkb, 4);
68 T* cs =
new T(nPts, static_cast<te::gm::GeomType>(gtype), srid, mbr);
72 case GAIA_LINESTRING :
73 memcpy(cs->getCoordinates(), ewkb, 16 * nPts);
77 case GAIA_LINESTRINGZ :
78 for(
unsigned int i = 0; i < nPts; ++i)
80 memcpy(&(cs->getCoordinates()[i]), ewkb, 16);
81 memcpy(&(cs->getZ()[i]), ewkb + 16, 8);
86 case GAIA_LINESTRINGM :
87 for(
unsigned int i = 0; i < nPts; ++i)
89 memcpy(&(cs->getCoordinates()[i]), ewkb, 16);
90 memcpy(&(cs->getM()[i]), ewkb + 16, 8);
95 case GAIA_LINESTRINGZM :
96 for(
unsigned int i = 0; i < nPts; ++i)
98 memcpy(&(cs->getCoordinates()[i]), ewkb, 16);
99 memcpy(&(cs->getZ()[i]), ewkb + 16, 8);
100 memcpy(&(cs->getM()[i]), ewkb + 24, 8);
108 for(
unsigned int i = 0; i < nPts; ++i)
115 for(
unsigned int i = 0; i < nPts; ++i)
119 for(
unsigned int i = 0; i < nPts; ++i)
132 const unsigned char* ewkbb = ewkb;
134 if((ewkb[0] != GAIA_MARK_START) || (ewkb[38] != GAIA_MARK_MBR))
135 throw te::da::Exception(TR_COMMON(
"It is an invalid SpatiaLite geometry!"));
138 double llx = 0.0, lly = 0.0, urx = 0.0, ury = 0.0;
139 memcpy(&srid, ewkb + 2, 4);
140 memcpy(&llx, ewkb + 6, 8);
141 memcpy(&lly, ewkb + 14, 8);
142 memcpy(&urx, ewkb + 22, 8);
143 memcpy(&ury, ewkb + 30, 8);
158 if(ewkbb[0] != GAIA_MARK_END)
161 throw te::da::Exception(TR_COMMON(
"It is an invalid SpatiaLite geometry!"));
167 te::gm::Geometry* te::sqlite::EWKBReader::readHex(
const unsigned char* hewkb)
178 te::gm::Geometry* te::sqlite::EWKBReader::getGeometry(
const unsigned char* ewkb,
179 const unsigned char** endptr,
180 unsigned char ewkbByteOrder,
184 unsigned int gType = GAIA_UNKNOWN;
186 memcpy(&gType, ewkb, 4);
200 return getPoint(ewkb, endptr, ewkbByteOrder, gType, srid, mbr);
202 case GAIA_LINESTRING:
203 case GAIA_LINESTRINGZ:
204 case GAIA_LINESTRINGM:
205 case GAIA_LINESTRINGZM:
206 return getLineString(ewkb, endptr, ewkbByteOrder, gType, srid, mbr);
212 return getPolygon(ewkb, endptr, ewkbByteOrder, gType, srid, mbr);
214 case GAIA_MULTIPOINT:
215 case GAIA_MULTIPOINTZ:
216 case GAIA_MULTIPOINTM:
217 case GAIA_MULTIPOINTZM:
218 case GAIA_MULTILINESTRING:
219 case GAIA_MULTILINESTRINGZ:
220 case GAIA_MULTILINESTRINGM:
221 case GAIA_MULTILINESTRINGZM:
222 case GAIA_MULTIPOLYGON:
223 case GAIA_MULTIPOLYGONZ:
224 case GAIA_MULTIPOLYGONM:
225 case GAIA_MULTIPOLYGONZM:
226 case GAIA_GEOMETRYCOLLECTION:
227 case GAIA_GEOMETRYCOLLECTIONZ:
228 case GAIA_GEOMETRYCOLLECTIONM:
229 case GAIA_GEOMETRYCOLLECTIONZM:
230 return getGeometryCollection(ewkb, endptr, ewkbByteOrder, gType, srid, mbr);
233 throw te::da::Exception(TR_COMMON(
"This kind of geometry id not supported by SpatiaLite!"));
237 te::gm::Point* te::sqlite::EWKBReader::getPoint(
const unsigned char* ewkb,
238 const unsigned char** endptr,
239 unsigned char ewkbByteOrder,
250 memcpy(&y, ewkb + 8, 8);
254 if(gtype != GAIA_POINT)
269 assert(gtype == GAIA_POINTZM);
271 memcpy(&m, ewkb + 8, 8);
302 throw te::da::Exception(TR_COMMON(
"This kind of geometry id not supported by SpatiaLite!"));
307 const unsigned char** endptr,
308 unsigned char ewkbByteOrder,
313 te::gm::LineString* l = GetCoordSequence<te::gm::LineString>(ewkb, endptr, gtype, ewkbByteOrder, srid, mbr);
319 const unsigned char** endptr,
320 unsigned char ewkbByteOrder,
323 te::gm::LinearRing* r = GetCoordSequence<te::gm::LinearRing>(ewkb, endptr, gtype, ewkbByteOrder, srid, 0);
327 te::gm::Polygon* te::sqlite::EWKBReader::getPolygon(
const unsigned char* ewkb,
328 const unsigned char** endptr,
329 unsigned char ewkbByteOrder,
334 unsigned int nRings = 0;
335 memcpy(&nRings, ewkb, 4);
343 unsigned int rType = GAIA_UNKNOWN;
348 rType = GAIA_LINESTRING;
353 rType = GAIA_LINESTRINGZ;
358 rType = GAIA_LINESTRINGM;
363 rType = GAIA_LINESTRINGZM;
368 throw te::da::Exception(TR_COMMON(
"This kind of geometry id not supported by SpatiaLite!"));
376 for(
unsigned int i = 0; i < nRings; ++i)
387 const unsigned char** endptr,
388 unsigned char ewkbByteOrder,
393 unsigned int nGeoms = 0;
394 memcpy(&nGeoms, ewkb, 4);
404 case GAIA_MULTIPOLYGON:
405 case GAIA_MULTIPOLYGONZ:
406 case GAIA_MULTIPOLYGONM:
407 case GAIA_MULTIPOLYGONZM:
411 case GAIA_MULTIPOINT:
412 case GAIA_MULTIPOINTZ:
413 case GAIA_MULTIPOINTM:
414 case GAIA_MULTIPOINTZM:
418 case GAIA_MULTILINESTRING:
419 case GAIA_MULTILINESTRINGZ:
420 case GAIA_MULTILINESTRINGM:
421 case GAIA_MULTILINESTRINGZM:
425 case GAIA_GEOMETRYCOLLECTION:
426 case GAIA_GEOMETRYCOLLECTIONZ:
427 case GAIA_GEOMETRYCOLLECTIONM:
428 case GAIA_GEOMETRYCOLLECTIONZM:
433 throw te::da::Exception(TR_COMMON(
"This kind of geometry id not supported by SpatiaLite!"));
439 throw te::da::Exception(TR_COMMON(
"This kind of geometry id not supported by SpatiaLite!"));
441 for(
unsigned int i = 0; i < nGeoms; ++i)
443 if(ewkb[0] != GAIA_MARK_ENTITY)
446 unsigned int classtype = GAIA_UNKNOWN;
448 memcpy(&classtype, ewkb + 1, 4);
462 g = getPoint(ewkb, endptr, ewkbByteOrder, classtype, srid, 0);
465 case GAIA_LINESTRING:
466 case GAIA_LINESTRINGZ:
467 case GAIA_LINESTRINGM:
468 case GAIA_LINESTRINGZM:
469 g = getLineString(ewkb, endptr, ewkbByteOrder, classtype, srid, 0);
476 g = getPolygon(ewkb, endptr, ewkbByteOrder, GAIA_POLYGON, srid, 0);
480 throw te::da::Exception(TR_COMMON(
"This kind of geometry is not handled by TerraLib SpatiaLite driver yet!"));
491 #endif // TE_ENABLE_SPATIALITE
MultiPolygon is a MultiSurface whose elements are Polygons.
std::vector< Curve * > & getRings()
It returns the polygon rings.
static const MachineByteOrder sm_machineByteOrder
A flag that indicates the machine byte order (Big Endian or Little Endian).
A point with a z-coordinate value and an associated measurement.
A point with an associated measure.
A LinearRing is a LineString that is both closed and simple.
MultiPoint is a GeometryCollection whose elements are restricted to points.
A point with z-coordinate value.
LineString is a curve with linear interpolation between points.
A point with x and y coordinate values.
An Envelope defines a 2D rectangular region.
Utility functions for the TerraLib SQLite Data Access driver.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
MultiLineString is a MultiCurve whose elements are LineStrings.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
const std::vector< Geometry * > & getGeometries() const
It returns a reference to the internal list of geometries.
T * GetCoordSequence(const char *ewkb, const char **endptr, unsigned int gType, char byteOrder)
char * Hex2Binary(const char *hex)
It converts each pair of hex characters from a NULL-terminated string of hex characters into a binary...
It is a collection of other geometric objects.
virtual void setSRID(int srid)=0
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
void SwapBytes(T &v)
It swaps the bytes in local.
An utility class for reading a SpatiaLite EWKB geometry.