27 #include "../../../../common/Exception.h" 28 #include "../../../../common/Globals.h" 29 #include "../../../../core/translator/Translator.h" 30 #include "../../../../dataaccess/dataset/DataSetType.h" 31 #include "../../../../dataaccess/utils/Utils.h" 32 #include "../../../../datatype/ArrayProperty.h" 33 #include "../../../../datatype/DateTime.h" 34 #include "../../../../datatype/DateTimeProperty.h" 35 #include "../../../../datatype/NumericProperty.h" 36 #include "../../../../datatype/Property.h" 37 #include "../../../../datatype/SimpleProperty.h" 38 #include "../../../../datatype/StringProperty.h" 39 #include "../../../../gdal/Raster.h" 40 #include "../../../../geometry/Envelope.h" 41 #include "../../../../geometry/Geometry.h" 42 #include "../../../../geometry/GeometryProperty.h" 43 #include "../../../../geometry/WKBReader.h" 44 #include "../../../../raster/BandProperty.h" 45 #include "../../../../raster/Grid.h" 46 #include "../../../../raster/RasterFactory.h" 47 #include "../../../../raster/RasterProperty.h" 48 #include "../../../../srs/SpatialReferenceSystemManager.h" 49 #include "../../../../srs/Config.h" 55 #include <gdal_priv.h> 58 #include <ogrsf_frmts.h> 59 #include <ogr_spatialref.h> 62 #include <boost/algorithm/string.hpp> 63 #include <boost/filesystem.hpp> 67 int wkbSize = ogrGeom->WkbSize();
69 unsigned char* wkbArray =
new unsigned char[wkbSize];
71 ogrGeom->exportToWkb(wkbNDR, wkbArray);
94 char* wkbArray =
new char[size];
100 OGRGeometry* ogrGeom = 0;
102 OGRErr result = OGRGeometryFactory::createFromWkb((
unsigned char*)wkbArray, srs, &ogrGeom, static_cast<int>(size));
106 if(result == OGRERR_NONE)
109 if(result == OGRERR_NOT_ENOUGH_DATA)
112 if(result == OGRERR_UNSUPPORTED_GEOMETRY_TYPE)
115 if(result == OGRERR_CORRUPT_DATA)
128 OGREnvelope* envOGR =
new OGREnvelope();
129 envOGR->MinX = env->
m_llx;
130 envOGR->MinY = env->
m_lly;
131 envOGR->MaxX = env->
m_urx;
132 envOGR->MaxY = env->
m_ury;
143 OGRErr ogrReturn = osrs->AutoIdentifyEPSG();
144 if( ogrReturn == OGRERR_NONE )
146 const char* srsAuth = osrs->GetAuthorityCode(0);
150 srid = atoi(srsAuth);
157 ogrReturn = osrs->exportToWkt( &wktPtr );
159 if( ogrReturn == OGRERR_NONE )
161 std::pair< std::string, unsigned int > customSRID;
162 std::string projRefStr( wktPtr );
170 srid = (
int)customSRID.second;
180 char* proj4StrPtr = 0;
181 ogrReturn = osrs->exportToProj4( &proj4StrPtr );
183 if( ogrReturn == OGRERR_NONE )
185 std::pair< std::string, unsigned int > customSRID;
186 std::string projRefStr( proj4StrPtr );
188 OGRFree( proj4StrPtr );
194 srid = (
int)customSRID.second;
205 std::string straux(osrs->GetRoot()->GetChild(0)->GetValue());
207 if (boost::find_first(straux,
"SIRGAS"))
209 if (osrs->IsGeographic())
212 else if (boost::find_first(straux,
"UTM "))
214 double centralm = osrs->GetProjParm(SRS_PP_CENTRAL_MERIDIAN,-1);
217 int zone = (
int)(centralm/6 + 31);
219 double fsnorth = osrs->GetProjParm(SRS_PP_FALSE_NORTHING,-1);
222 else if (fsnorth == 0)
233 std::auto_ptr< OGRSpatialReference > osrs(
new OGRSpatialReference() );
235 OGRErr error = osrs->importFromEPSG(srid);
237 if( error != OGRERR_NONE )
244 if( !wktStr.empty() )
246 char* wktStrPtr = (
char*)wktStr.c_str();
247 error = osrs->importFromWkt( &wktStrPtr );
252 error = OGRERR_UNSUPPORTED_SRS;
256 if( error != OGRERR_NONE )
260 std::string proj4Str =
263 if( !proj4Str.empty() )
265 char* proj4StrPtr = (
char*)proj4Str.c_str();
266 error = osrs->importFromProj4( proj4StrPtr );
271 error = OGRERR_UNSUPPORTED_SRS;
275 if(error != OGRERR_NONE)
278 return osrs.release();
285 int nFields = featDef->GetFieldCount();
287 for(
int i = 0; i < nFields; i++)
289 OGRFieldDefn* fieldDef = featDef->GetFieldDefn(i);
294 OGRwkbGeometryType ogrGeomType = featDef->GetGeomType();
296 if(ogrGeomType != wkbNone)
300 dt->
add(geomPropertyType);
317 OGRFeatureDefn* featDef =
new OGRFeatureDefn(dt->
getName().c_str());
320 for(
unsigned int i = 0; i < props.size(); i++)
339 std::string name = fieldDef->GetNameRef();
340 switch(fieldDef->GetType())
358 if(fieldDef->GetWidth() == 0)
403 OGRFieldDefn* fieldDef =
new OGRFieldDefn(p->
getName().c_str(), OFTInteger);
416 fieldDef->SetType(OFTIntegerList);
418 fieldDef->SetType(OFTStringList);
420 fieldDef->SetType(OFTRealList);
427 fieldDef->SetType(OFTString);
428 fieldDef->SetWidth(static_cast<int>(static_cast<te::dt::StringProperty*>(p)->size()));
432 fieldDef->SetType(OFTReal);
436 fieldDef->SetType(OFTReal);
437 fieldDef->SetPrecision(static_cast<te::dt::NumericProperty*>(p)->getScale());
441 fieldDef->SetType(OFTBinary);
449 fieldDef->SetType(OFTDate);
451 fieldDef->SetType(OFTTime);
453 fieldDef->SetType(OFTDateTime);
482 case wkbMultiLineString:
485 case wkbMultiPolygon:
488 case wkbGeometryCollection:
497 case wkbLineString25D:
503 case wkbMultiPoint25D:
506 case wkbMultiLineString25D:
509 case wkbMultiPolygon25D:
512 case wkbGeometryCollection25D:
515 case wkbMultiSurface:
537 return wkbLineString;
543 return wkbMultiPoint;
546 return wkbMultiLineString;
549 return wkbMultiPolygon;
552 return wkbGeometryCollection;
562 return wkbLineString25D;
567 return wkbPolygon25D;
572 return wkbMultiPoint25D;
577 return wkbMultiLineString25D;
582 return wkbMultiPolygon25D;
587 return wkbGeometryCollection25D;
593 return wkbMultiPolygon25D;
599 return wkbMultiPolygon25D;
613 boost::filesystem::path mpath(path.c_str());
615 std::string ext = mpath.extension().string();
617 if(ext ==
".shp" || ext ==
".SHP")
618 return std::string(
"ESRI Shapefile");
620 if(ext ==
".mif" || ext ==
".MIF")
621 return std::string(
"Mapinfo File");
623 if(ext ==
".kml" || ext ==
".KML")
624 return std::string(
"KML");
626 if(ext ==
".geojson" || ext ==
".GEOJSON")
627 return std::string(
"GeoJSON");
629 if(ext ==
".gml" || ext ==
".GML")
630 return std::string(
"GML");
632 if(ext ==
".dxf" || ext ==
".DXF")
633 return std::string(
"DXF");
635 if(ext ==
".dgn" || ext ==
".DGN")
636 return std::string(
"DGN");
643 std::vector<std::string> drivernames;
645 int ndrivers = OGRSFDriverRegistrar::GetRegistrar()->GetDriverCount();
647 for (
int i = 0; i < ndrivers; ++i)
649 GDALDriver* driver = GetGDALDriverManager()->GetDriver(i);
651 if (filterCreate && !OGR_Dr_TestCapability(driver, ODrCCreateDataSource))
653 drivernames.push_back(driver->GetDescription());
661 std::map<std::string, std::string>::const_iterator it = connInfo.find(
"URI");
663 if(it != connInfo.end())
666 it = connInfo.find(
"SOURCE");
668 if(it != connInfo.end())
677 std::size_t pos = sql.find(
"AND Intersection");
680 if(pos == std::string::npos)
681 pos = sql.find(
"WHERE Intersection");
683 if(pos == std::string::npos)
686 std::string newQuery;
688 std::size_t pos2 = sql.find(
"))", pos);
689 newQuery = sql.substr(0, pos);
690 newQuery += sql.substr(pos2 + 2);
697 const char *gpkgFormat =
"GPKG";
699 GDALDriver *gpkgDriver;
701 gpkgDriver = GetGDALDriverManager()->GetDriverByName(gpkgFormat);
703 char **papszOptions = NULL;
705 GDALDataset *poDstDS;
706 poDstDS = gpkgDriver->Create(outFileName.c_str(), 0, 0, 0, GDT_Unknown, papszOptions);
707 GDALClose((GDALDatasetH)poDstDS);
714 const char *gpkgFormat =
"GPKG";
715 GDALDriver *gpkgDriver;
716 gpkgDriver = GetGDALDriverManager()->GetDriverByName(gpkgFormat);
718 char **papszOptions = NULL;
719 papszOptions = CSLSetNameValue(papszOptions,
"APPEND_SUBDATASET",
"YES");
720 papszOptions = CSLSetNameValue(papszOptions,
"TILING_SCHEME",
"GoogleMapsCompatible");
721 papszOptions = CSLSetNameValue(papszOptions,
"ZOOM_LEVEL_STRATEGY",
"LOWER");
723 GDALDataset *poDstDS = gpkgDriver->CreateCopy(outFileName.c_str(), gdalRaster->
getGDALDataset(), FALSE, papszOptions, NULL, NULL);;
728 boost::scoped_array< int > overviewsIndexes(
new int[levels]);
730 for (
unsigned int overViewIdx = 1; overViewIdx <= levels; ++overViewIdx)
736 unsigned int index = (
unsigned int)std::pow(2, overViewIdx);
737 overviewsIndexes[(overViewIdx - 1)] = index;
740 CPLErr returnValue = poDstDS->BuildOverviews(
"NEAREST", (
int)levels, overviewsIndexes.get(), 0, NULL, NULL, NULL);
741 poDstDS->FlushCache();
744 CSLDestroy(papszOptions);
745 GDALClose((GDALDatasetH)poDstDS);
GDALDataset * getGDALDataset() const
Returns the raster GDAL handler.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
TERRAMOBILEPLUGINSDLLEXPORT void copyToGeopackage(te::rst::Raster *raster, std::string outFileName)
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
TEOGREXPORT OGRGeometry * Convert2OGR(const te::gm::Geometry *teGeom)
It converts the TerraLib Geometry to OGR Geometry.
An atomic property like an integer or double.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
This class represents Raster data.
A class that models the description of a dataset.
double m_urx
Upper right corner x-coordinate.
static const MachineByteOrder sm_machineByteOrder
A flag that indicates the machine byte order (Big Endian or Little Endian).
#define TE_TR(message)
It marks a string in order to get translated.
std::size_t getWkbSize() const _NOEXCEPT_OP(true)
It returns the size required by a WKB representation for this geometric object.
It models a property definition.
TERRAMOBILEPLUGINSDLLEXPORT std::vector< std::string > GetOGRDrivers(bool filterCreate=false)
It returns the list of OGR Drivers available.
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
double m_llx
Lower left corner x-coordinate.
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
TERRAMOBILEPLUGINSDLLEXPORT std::string GetOGRConnectionInfo(const std::map< std::string, std::string > &connInfo)
unsigned int getMultiResLevelsCount() const
Returns the current number of multi-resolution pyramid levels.
TERRAMOBILEPLUGINSDLLEXPORT OGRGeometry * Convert2OGR(const te::gm::Geometry *teGeom)
It converts the TerraLib Geometry to OGR Geometry.
TERRAMOBILEPLUGINSDLLEXPORT std::string RemoveSpatialSql(const std::string &sql)
TERRAMOBILEPLUGINSDLLEXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
Utility functions for the data access module.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
double m_lly
Lower left corner y-coordinate.
int getType() const
It returns the property data type.
void add(Constraint *c)
It adds a new constraint.
#define TE_SRS_SIRGAS2000
Property * getElementType() const
It returns the type of array elements.
The type for variable-length multidimensional arrays.
double m_ury
Upper right corner y-coordinate.
The type for date and time types: date, date period, date duration, time duration, time instant, time period, time instant with time zone or time period with time zone.
std::string GetDriverName(const std::string &path)
It tries extract the driver name used by OGR Library based on the given path.
DateTimeType
The subtype of date and time type, based on ISO 8621.
TERRAMOBILEPLUGINSDLLEXPORT int Convert2TerraLibProjection(OGRSpatialReference *osrs)
It converts the OGR Projection to TerraLib Projection.
TERRAMOBILEPLUGINSDLLEXPORT OGRSpatialReference * Convert2OGRProjection(int srid)
It converts the TerraLib Projection to OGR Projection.
DateTimeType getSubType() const
It returns the date time property sub type.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
te::da::DataSetType * Convert2TerraLib(sqlite3_stmt *pStmt)
TEOGREXPORT OGRSpatialReference * Convert2OGRProjection(int srid)
It converts the TerraLib Projection to OGR Projection.
TERRAMOBILEPLUGINSDLLEXPORT void createGeopackage(std::string outFileName)
static Geometry * read(const char *wkb)
It returns a valid geometry from a given WKB.
void getWkb(char *wkb, te::common::MachineByteOrder byteOrder) const _NOEXCEPT_OP(false)
It serializes the geometry to a WKB representation into the specified buffer.
const std::string & getName() const
It returns the property name.