diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 873925baf54..3c16aa21d89 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -130,7 +130,7 @@ // // Gdal/Ogr includes // -#include +#include // // Other includes @@ -1743,20 +1743,10 @@ static void buildSupportedVectorFileFilter_(QString & fileFilters) return; } - // first get the GDAL driver manager - - OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); - - if (!driverRegistrar) - { - QMessageBox::warning(this,tr("OGR Driver Manager"),tr("unable to get OGRDriverManager")); - return; // XXX good place to throw exception if we - } // XXX decide to do exceptions - // then iterate through all of the supported drivers, adding the // corresponding file filter - OGRSFDriver *driver; // current driver + OGRSFDriverH driver; // current driver QString driverName; // current driver name @@ -1768,9 +1758,9 @@ static void buildSupportedVectorFileFilter_(QString & fileFilters) // open datasets with no explicitly defined file name extension. QgsDebugMsg("Driver count: " + QString::number(driverRegistrar->GetDriverCount())); - for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) + for (int i = 0; i < OGRGetDriverCount(); ++i) { - driver = driverRegistrar->GetDriver(i); + driver = OGRGetDriver(i); Q_CHECK_PTR(driver); @@ -1780,9 +1770,7 @@ static void buildSupportedVectorFileFilter_(QString & fileFilters) continue; } - driverName = driver->GetName(); - - + driverName = OGR_Dr_GetName(driver); if (driverName.startsWith("ESRI")) { diff --git a/src/core/qgsspatialrefsys.cpp b/src/core/qgsspatialrefsys.cpp index 967920fc281..81ad1fffd40 100644 --- a/src/core/qgsspatialrefsys.cpp +++ b/src/core/qgsspatialrefsys.cpp @@ -33,8 +33,7 @@ #include //gdal and ogr includes (needed for == operator) -#include -#include +#include #include #include @@ -164,8 +163,12 @@ void QgsSpatialRefSys::validate() //this is really ugly but we need to get a QString to a char** const char *mySourceCharArrayPointer = mProj4String.latin1(); //create the sr and populate it from a wkt proj definition - OGRSpatialReference myOgrSpatialRef; - OGRErr myInputResult = myOgrSpatialRef.importFromProj4( mySourceCharArrayPointer ); + OGRSpatialReferenceH myOgrSpatialRef = OSRNewSpatialReference(NULL); + + OGRErr myInputResult = + OSRImportFromProj4( myOgrSpatialRef, mySourceCharArrayPointer ); + + OSRDestroySpatialReference( myOgrSpatialRef ); if (myInputResult==OGRERR_NONE) { @@ -187,8 +190,10 @@ void QgsSpatialRefSys::validate() //this is really ugly but we need to get a QString to a char** const char *mySourceCharArrayPointer = mProj4String.latin1(); //create the sr and populate it from a wkt proj definition - OGRSpatialReference myOgrSpatialRef; - OGRErr myInputResult = myOgrSpatialRef.importFromProj4( mySourceCharArrayPointer ); + OGRSpatialReferenceH myOgrSpatialRef = OSRNewSpatialReference(NULL); + OGRErr myInputResult = OSRImportFromProj4( myOgrSpatialRef, mySourceCharArrayPointer ); + + OSRDestroySpatialReference( myOgrSpatialRef ); if (! myInputResult==OGRERR_NONE) { @@ -290,9 +295,9 @@ bool QgsSpatialRefSys::createFromWkt(QString theWkt) #define OGRERR_UNSUPPORTED_SRS 7 */ - OGRSpatialReference myOgrSpatialRef; + OGRSpatialReferenceH myOgrSpatialRef = OSRNewSpatialReference( NULL ); - OGRErr myInputResult = myOgrSpatialRef.importFromWkt( &pWkt ); + OGRErr myInputResult = OSRImportFromWkt( myOgrSpatialRef, &pWkt ); if (myInputResult != OGRERR_NONE) { QgsDebugMsg("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"); @@ -301,15 +306,19 @@ bool QgsSpatialRefSys::createFromWkt(QString theWkt) QgsDebugMsg("INPUT: " + theWkt); QgsDebugMsg("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); mIsValidFlag = false; + OSRDestroySpatialReference( myOgrSpatialRef ); return false; } // always morph from esri as it doesn't hurt anything - myOgrSpatialRef.morphFromESRI(); + // FW: Hey, that's not right! It can screw stuff up! Disable + //myOgrSpatialRef.morphFromESRI(); + // create the proj4 structs needed for transforming - char *proj4src; - myOgrSpatialRef.exportToProj4(&proj4src); + char *proj4src = NULL; + OSRExportToProj4(myOgrSpatialRef, &proj4src); + OSRDestroySpatialReference( myOgrSpatialRef ); //now that we have the proj4string, delegate to createFromProj4String so // that we can try to fill in the remaining class members... @@ -467,8 +476,9 @@ bool QgsSpatialRefSys::isValid() const //this is really ugly but we need to get a QString to a char** const char *mySourceCharArrayPointer = mProj4String.latin1(); //create the sr and populate it from a wkt proj definition - OGRSpatialReference myOgrSpatialRef; - OGRErr myResult = myOgrSpatialRef.importFromProj4( mySourceCharArrayPointer ); + OGRSpatialReferenceH myOgrSpatialRef = OSRNewSpatialReference( NULL ); + OGRErr myResult = OSRImportFromProj4( myOgrSpatialRef, mySourceCharArrayPointer ); + OSRDestroySpatialReference( myOgrSpatialRef ); if (myResult==OGRERR_NONE) { //QgsDebugMsg("The OGRe says it's a valid SRS with proj4 string: " + mProj4String); @@ -903,16 +913,16 @@ void QgsSpatialRefSys::setMapUnits() } char *unitName; - OGRSpatialReference myOgrSpatialRef; - myOgrSpatialRef.importFromProj4(mProj4String.latin1()); + OGRSpatialReferenceH myOgrSpatialRef = OSRNewSpatialReference(NULL); + OSRImportFromProj4(myOgrSpatialRef, mProj4String.latin1()); // Of interest to us is that this call adds in a unit parameter if // one doesn't already exist. - myOgrSpatialRef.Fixup(); + OSRFixup(myOgrSpatialRef); - if (myOgrSpatialRef.IsProjected()) + if (OSRIsProjected(myOgrSpatialRef)) { - double toMeter = myOgrSpatialRef.GetLinearUnits(&unitName); + double toMeter = OSRGetLinearUnits(myOgrSpatialRef,&unitName); QString unit(unitName); // If the units parameter was created during the Fixup() call @@ -940,7 +950,7 @@ void QgsSpatialRefSys::setMapUnits() } else { - myOgrSpatialRef.GetAngularUnits(&unitName); + OSRGetAngularUnits(myOgrSpatialRef, &unitName); QString unit(unitName); if (unit == "degree") mMapUnits = QGis::DEGREES; @@ -951,6 +961,7 @@ void QgsSpatialRefSys::setMapUnits() } QgsDebugMsg("Projection has angular units of " + unit); } + OSRDestroySpatialReference( myOgrSpatialRef ); } @@ -1104,10 +1115,10 @@ bool QgsSpatialRefSys::equals(QString theProj4CharArray) //create the sr and populate it from a wkt proj definition - OGRSpatialReference myOgrSpatialRef1; - OGRSpatialReference myOgrSpatialRef2; - OGRErr myInputResult1 = myOgrSpatialRef1.importFromProj4( myCharArrayPointer1 ); - OGRErr myInputResult2 = myOgrSpatialRef2.importFromProj4( theProj4CharArray.latin1() ); + OGRSpatialReferenceH myOgrSpatialRef1 = OSRNewSpatialReference( NULL ); + OGRSpatialReferenceH myOgrSpatialRef2 = OSRNewSpatialReference( NULL ); + OGRErr myInputResult1 = OSRImportFromProj4( myOgrSpatialRef1, myCharArrayPointer1 ); + OGRErr myInputResult2 = OSRImportFromProj4( myOgrSpatialRef1, theProj4CharArray.latin1() ); // Could do some error reporting here... if (myInputResult1 != OGRERR_NONE) @@ -1115,62 +1126,45 @@ bool QgsSpatialRefSys::equals(QString theProj4CharArray) if (myInputResult2 != OGRERR_NONE) {} - if (myOgrSpatialRef1.IsGeographic() && myOgrSpatialRef2.IsGeographic()) + if (OSRIsGeographic(myOgrSpatialRef1) && OSRIsGeographic(myOgrSpatialRef2)) { // qWarning("QgsSpatialRefSys::operator== srs1 and srs2 are geographic "); - myMatchFlag = myOgrSpatialRef1.IsSameGeogCS(&myOgrSpatialRef2); + myMatchFlag = OSRIsSameGeogCS(myOgrSpatialRef1,myOgrSpatialRef2); } - else if (myOgrSpatialRef1.IsProjected() && myOgrSpatialRef2.IsProjected()) + else if (OSRIsProjected(myOgrSpatialRef1) && OSRIsProjected(myOgrSpatialRef2)) { // qWarning("QgsSpatialRefSys::operator== srs1 and srs2 are projected "); - myMatchFlag = myOgrSpatialRef1.IsSame(&myOgrSpatialRef2); + myMatchFlag = OSRIsSame(myOgrSpatialRef1,myOgrSpatialRef2); } else { // qWarning("QgsSpatialRefSys::operator== srs1 and srs2 are different types "); myMatchFlag = false; } - //find out the units: - /* Not needed anymore here - keeping here as a note because I am gonna use it elsewhere - const char *myUnitsArrayPointer1; - const char *myUnitsArrayPointer2; - OGRErr myUnitsValid1 = myOgrSpatialRef1.GetLinearUnits(&myUnitsArrayPointer1 ); - OGRErr myUnitsValid2 = myOgrSpatialRef2.GetLinearUnits(&myUnitsArrayPointer2 ); - QString myUnitsString1(myUnitsArrayPointer1); - QString myUnitsString2(myUnitsArrayPointer2); - */ + OSRDestroySpatialReference( myOgrSpatialRef1 ); + OSRDestroySpatialReference( myOgrSpatialRef2 ); - - - - //placeholder to be replaced with ogr tests - if (myMatchFlag) - { -// qWarning("QgsSpatialRefSys::operator== result: srs's are equal "); - } - else - { -// qWarning("QgsSpatialRefSys::operator== result: srs's are not equal "); - } return myMatchFlag; } QString QgsSpatialRefSys::toWkt() const { - OGRSpatialReference myOgrSpatialRef; - OGRErr myInputResult = myOgrSpatialRef.importFromProj4(mProj4String.latin1()); + OGRSpatialReferenceH myOgrSpatialRef = OSRNewSpatialReference(NULL); + OGRErr myInputResult = OSRImportFromProj4(myOgrSpatialRef,mProj4String.latin1()); QString myWkt; if (myInputResult == OGRERR_NONE) { char* WKT; - if(myOgrSpatialRef.exportToWkt(&WKT) == OGRERR_NONE) + if(OSRExportToWkt(myOgrSpatialRef,&WKT) == OGRERR_NONE) { myWkt = WKT; OGRFree(WKT); } } - + + OSRDestroySpatialReference( myOgrSpatialRef ); + return myWkt; } diff --git a/src/core/qgsvectorfilewriter.cpp b/src/core/qgsvectorfilewriter.cpp index 8b1a4c12ebe..8215deb735c 100644 --- a/src/core/qgsvectorfilewriter.cpp +++ b/src/core/qgsvectorfilewriter.cpp @@ -31,7 +31,8 @@ #include -#include +#include +#include QgsVectorFileWriter::QgsVectorFileWriter(const QString& shapefileName, @@ -45,11 +46,9 @@ QgsVectorFileWriter::QgsVectorFileWriter(const QString& shapefileName, QString driverName = "ESRI Shapefile"; // find driver in OGR - OGRSFDriver *poDriver; + OGRSFDriverH poDriver; OGRRegisterAll(); - poDriver = - OGRSFDriverRegistrar::GetRegistrar()-> - GetDriverByName(driverName.toLocal8Bit().data()); + poDriver = OGRGetDriverByName(driverName.toLocal8Bit().data()); if (poDriver == NULL) { @@ -58,7 +57,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(const QString& shapefileName, } // create the data source - mDS = poDriver->CreateDataSource(shapefileName, NULL); + mDS = OGR_Dr_CreateDataSource(poDriver,shapefileName, NULL); if (mDS == NULL) { mError = ErrCreateDataSource; @@ -76,20 +75,22 @@ QgsVectorFileWriter::QgsVectorFileWriter(const QString& shapefileName, } // consider spatial reference system of the layer - OGRSpatialReference* ogrRef = NULL; + OGRSpatialReferenceH ogrRef = NULL; if (srs) { QString srsWkt = srs->toWkt(); - ogrRef = new OGRSpatialReference(srsWkt.toLocal8Bit().data()); + ogrRef = OSRNewSpatialReference(srsWkt.toLocal8Bit().data()); } // datasource created, now create the output layer QString layerName = shapefileName.left(shapefileName.find(".shp")); OGRwkbGeometryType wkbType = static_cast(geometryType); - mLayer = mDS->CreateLayer(QFile::encodeName(layerName).data(), ogrRef, wkbType, NULL); + mLayer = OGR_DS_CreateLayer(mDS,QFile::encodeName(layerName).data(), ogrRef, wkbType, NULL); if (srs) - delete ogrRef; + { + OSRDestroySpatialReference( ogrRef ); + } if (mLayer == NULL) { @@ -128,15 +129,15 @@ QgsVectorFileWriter::QgsVectorFileWriter(const QString& shapefileName, } // create field definition - OGRFieldDefn fld(mCodec->fromUnicode(attrField.name()), ogrType); - fld.SetWidth(attrField.length()); - fld.SetPrecision(attrField.precision()); + OGRFieldDefnH fld = OGR_Fld_Create(mCodec->fromUnicode(attrField.name()), ogrType); + OGR_Fld_SetWidth(fld,attrField.length()); + OGR_Fld_SetPrecision(fld,attrField.precision()); // create the field QgsDebugMsg("creating field " + attrField.name() + " type " + QString(QVariant::typeToName(attrField.type())) + " width length " + QString::number(attrField.length())); - if (mLayer->CreateField(&fld) != OGRERR_NONE) + if (OGR_L_CreateField(mLayer,fld,TRUE) != OGRERR_NONE) { QgsDebugMsg("error creating field " + attrField.name()); } @@ -149,33 +150,9 @@ QgsVectorFileWriter::QgsVectorFileWriter(const QString& shapefileName, mGeom = createEmptyGeometry(mWkbType); } -OGRGeometry* QgsVectorFileWriter::createEmptyGeometry(QGis::WKBTYPE wkbType) +OGRGeometryH QgsVectorFileWriter::createEmptyGeometry(QGis::WKBTYPE wkbType) { - // create instance of OGR geometry (will be used to import from WKB) - switch (wkbType) - { - case QGis::WKBPoint: - case QGis::WKBPoint25D: - return new OGRPoint; - case QGis::WKBLineString: - case QGis::WKBLineString25D: - return new OGRLineString; - case QGis::WKBPolygon: - case QGis::WKBPolygon25D: - return new OGRPolygon; - case QGis::WKBMultiPoint: - case QGis::WKBMultiPoint25D: - return new OGRMultiPoint; - case QGis::WKBMultiLineString: - case QGis::WKBMultiLineString25D: - return new OGRMultiLineString; - case QGis::WKBMultiPolygon: - case QGis::WKBMultiPolygon25D: - return new OGRMultiPolygon; - default: - assert(0 && "invalid WKB type"); - return NULL; - } + return OGR_G_CreateGeometry( (OGRwkbGeometryType) wkbType ); } @@ -192,7 +169,7 @@ bool QgsVectorFileWriter::addFeature(QgsFeature& feature) QgsAttributeMap::const_iterator it; // create the feature - OGRFeature *poFeature = new OGRFeature(mLayer->GetLayerDefn()); + OGRFeatureH poFeature = OGR_F_Create(OGR_L_GetLayerDefn(mLayer)); // attribute handling const QgsAttributeMap& attributes = feature.attributeMap(); @@ -209,17 +186,18 @@ bool QgsVectorFileWriter::addFeature(QgsFeature& feature) QString attrName = mFields[it.key()].name(); QByteArray encAttrName = mCodec->fromUnicode(attrName); const QVariant& attrValue = it.value(); + int ogrField = OGR_F_GetFieldIndex( poFeature, encAttrName.data() ); switch (attrValue.type()) { case QVariant::Int: - poFeature->SetField(encAttrName.data(), attrValue.toInt()); + OGR_F_SetFieldInteger(poFeature, ogrField, attrValue.toInt()); break; case QVariant::Double: - poFeature->SetField(encAttrName.data(), attrValue.toDouble()); + OGR_F_SetFieldDouble(poFeature, ogrField, attrValue.toDouble()); break; case QVariant::String: - poFeature->SetField(encAttrName.data(), mCodec->fromUnicode(attrValue.toString()).data()); + OGR_F_SetFieldString(poFeature, ogrField, mCodec->fromUnicode(attrValue.toString()).data()); break; default: //assert(0 && "invalid variant type"); @@ -241,42 +219,42 @@ bool QgsVectorFileWriter::addFeature(QgsFeature& feature) // Btw. OGRGeometry must be exactly of the type of the geometry which it will receive // i.e. Polygons can't be imported to OGRMultiPolygon - OGRGeometry* mGeom2 = createEmptyGeometry(geom->wkbType()); + OGRGeometryH mGeom2 = createEmptyGeometry(geom->wkbType()); - OGRErr err = mGeom2->importFromWkb(geom->wkbBuffer(), geom->wkbSize()); + OGRErr err = OGR_G_ImportFromWkb(mGeom2,geom->wkbBuffer(), geom->wkbSize()); if (err != OGRERR_NONE) { QgsDebugMsg("Failed to import geometry from WKB: " + QString::number(err)); - OGRFeature::DestroyFeature(poFeature); + OGR_F_Destroy( poFeature ); return false; } // pass ownership to geometry - poFeature->SetGeometryDirectly(mGeom2); + OGR_F_SetGeometryDirectly(poFeature,mGeom2); } else { - OGRErr err = mGeom->importFromWkb(geom->wkbBuffer(), geom->wkbSize()); + OGRErr err = OGR_G_ImportFromWkb(mGeom,geom->wkbBuffer(), geom->wkbSize()); if (err != OGRERR_NONE) { QgsDebugMsg("Failed to import geometry from WKB: " + QString::number(err)); - OGRFeature::DestroyFeature(poFeature); + OGR_F_Destroy( poFeature ); return false; } // set geometry (ownership is not passed to OGR) - poFeature->SetGeometry(mGeom); + OGR_F_SetGeometry(poFeature,mGeom); } // put the created feature to layer - if (mLayer->CreateFeature(poFeature) != OGRERR_NONE) + if (OGR_L_CreateFeature(mLayer,poFeature) != OGRERR_NONE) { QgsDebugMsg("Failed to create feature in shapefile"); - OGRFeature::DestroyFeature(poFeature); + OGR_F_Destroy( poFeature ); return false; } - OGRFeature::DestroyFeature(poFeature); + OGR_F_Destroy( poFeature ); return true; } @@ -284,10 +262,14 @@ bool QgsVectorFileWriter::addFeature(QgsFeature& feature) QgsVectorFileWriter::~QgsVectorFileWriter() { if (mGeom) - delete mGeom; + { + OGR_G_DestroyGeometry( mGeom ); + } if (mDS) - OGRDataSource::DestroyDataSource(mDS); + { + OGR_DS_Destroy( mDS ); + } } diff --git a/src/core/qgsvectorfilewriter.h b/src/core/qgsvectorfilewriter.h index 7037b544381..ab4b8e18b88 100644 --- a/src/core/qgsvectorfilewriter.h +++ b/src/core/qgsvectorfilewriter.h @@ -24,9 +24,9 @@ #include -class OGRDataSource; -class OGRLayer; -class OGRGeometry; +typedef void *OGRDataSourceH; +typedef void *OGRLayerH; +typedef void *OGRGeometryH; class QTextCodec; @@ -81,11 +81,11 @@ class CORE_EXPORT QgsVectorFileWriter static bool deleteShapeFile(QString theFileName); protected: - OGRGeometry* createEmptyGeometry(QGis::WKBTYPE wkbType); + OGRGeometryH createEmptyGeometry(QGis::WKBTYPE wkbType); - OGRDataSource* mDS; - OGRLayer* mLayer; - OGRGeometry* mGeom; + OGRDataSourceH mDS; + OGRLayerH mLayer; + OGRGeometryH mGeom; QgsFieldMap mFields; diff --git a/src/core/raster/qgsrasterlayer.cpp b/src/core/raster/qgsrasterlayer.cpp index 0af522f8f5d..69179b871f8 100644 --- a/src/core/raster/qgsrasterlayer.cpp +++ b/src/core/raster/qgsrasterlayer.cpp @@ -28,7 +28,9 @@ email : tim at linfiniti.com #include "qgsrasterviewport.h" #include "qgsrect.h" #include "qgsspatialrefsys.h" + #include "gdalwarper.h" +#include "cpl_conv.h" #include "qgspseudocolorshader.h" #include "qgsfreakoutshader.h" @@ -121,18 +123,10 @@ void QgsRasterLayer::buildSupportedRasterFileFilter(QString & theFileFiltersStri // first get the GDAL driver manager registerGdalDrivers(); - GDALDriverManager *myGdalDriverManager = GetGDALDriverManager(); - - if (!myGdalDriverManager) - { - QgsLogger::warning("unable to get GDALDriverManager"); - return; // XXX good place to throw exception if we - } // XXX decide to do exceptions - // then iterate through all of the supported drivers, adding the // corresponding file filter - GDALDriver *myGdalDriver; // current driver + GDALDriverH myGdalDriver; // current driver char **myGdalDriverMetadata; // driver metadata strings @@ -145,7 +139,7 @@ void QgsRasterLayer::buildSupportedRasterFileFilter(QString & theFileFiltersStri QStringList catchallFilter; // for Any file(*.*), but also for those // drivers with no specific file filter - GDALDriver *jp2Driver = NULL; // first JPEG2000 driver found + GDALDriverH jp2Driver = NULL; // first JPEG2000 driver found // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file @@ -158,9 +152,9 @@ void QgsRasterLayer::buildSupportedRasterFileFilter(QString & theFileFiltersStri // driver, which will be found in DMD_LONGNAME, which will have the // same form. - for (int i = 0; i < myGdalDriverManager->GetDriverCount(); ++i) + for (int i = 0; i < GDALGetDriverCount(); ++i) { - myGdalDriver = myGdalDriverManager->GetDriver(i); + myGdalDriver = GDALGetDriver(i); Q_CHECK_PTR(myGdalDriver); @@ -172,17 +166,17 @@ void QgsRasterLayer::buildSupportedRasterFileFilter(QString & theFileFiltersStri // now we need to see if the driver is for something currently // supported; if not, we give it a miss for the next driver - myGdalDriverDescription = myGdalDriver->GetDescription(); + myGdalDriverDescription = GDALGetDescription(myGdalDriver); if (!isSupportedRasterDriver(myGdalDriverDescription)) { // not supported, therefore skip - QgsDebugMsg("skipping unsupported driver " + QString(myGdalDriver->GetDescription())); + QgsDebugMsg("skipping unsupported driver " + QString(GDALGetDescription(myGdalDriver))); continue; } // std::cerr << "got driver string " << myGdalDriver->GetDescription() << "\n"; - myGdalDriverMetadata = myGdalDriver->GetMetadata(); + myGdalDriverMetadata = GDALGetMetadata(myGdalDriver,NULL); // presumably we know we've run out of metadta if either the // address is 0, or the first character is null @@ -275,7 +269,7 @@ void QgsRasterLayer::buildSupportedRasterFileFilter(QString & theFileFiltersStri } else { - catchallFilter << QString(myGdalDriver->GetDescription()); + catchallFilter << QString(GDALGetDescription(myGdalDriver)); } } @@ -439,7 +433,7 @@ QgsRasterLayer::~QgsRasterLayer() { if ( mGdalBaseDataset ) { - mGdalBaseDataset->Dereference(); + GDALDereferenceDataset( mGdalBaseDataset ); GDALClose(mGdalDataset); } } @@ -454,7 +448,7 @@ bool QgsRasterLayer::readFile( QString const & fileName ) mGdalDataset = NULL; //open the dataset making sure we handle char encoding of locale properly - mGdalBaseDataset = (GDALDataset *) GDALOpen(QFile::encodeName(fileName).constData(), GA_ReadOnly); + mGdalBaseDataset = GDALOpen(QFile::encodeName(fileName).constData(), GA_ReadOnly); if (mGdalBaseDataset == NULL) { @@ -466,23 +460,29 @@ bool QgsRasterLayer::readFile( QString const & fileName ) mLastModified = lastModified ( fileName ); // Check if we need a warped VRT for this file. - if( (mGdalBaseDataset->GetGeoTransform(mGeoTransform) == CE_None + if( (GDALGetGeoTransform(mGdalBaseDataset,mGeoTransform) == CE_None && (mGeoTransform[1] < 0.0 || mGeoTransform[2] != 0.0 || mGeoTransform[4] != 0.0 || mGeoTransform[5] > 0.0)) - || mGdalBaseDataset->GetGCPCount() > 0 ) + || GDALGetGCPCount(mGdalBaseDataset) > 0 ) { QgsLogger::warning("Creating Warped VRT."); - mGdalDataset = (GDALDataset *) + mGdalDataset = GDALAutoCreateWarpedVRT( mGdalBaseDataset, NULL, NULL, GRA_NearestNeighbour, 0.2, NULL ); + if( mGdalDataset == NULL ) + { + QgsLogger::warning("Warped VRT Creation failed."); + mGdalDataset = mGdalBaseDataset; + GDALReferenceDataset( mGdalDataset ); + } } else { mGdalDataset = mGdalBaseDataset; - mGdalDataset->Reference(); + GDALReferenceDataset( mGdalDataset ); } //check f this file has pyramids @@ -532,11 +532,11 @@ bool QgsRasterLayer::readFile( QString const & fileName ) // Use the affine transform to get geo coordinates for // the corners of the raster double myXMax = mGeoTransform[0] + - mGdalDataset->GetRasterXSize() * mGeoTransform[1] + - mGdalDataset->GetRasterYSize() * mGeoTransform[2]; + GDALGetRasterXSize(mGdalDataset) * mGeoTransform[1] + + GDALGetRasterYSize(mGdalDataset) * mGeoTransform[2]; double myYMin = mGeoTransform[3] + - mGdalDataset->GetRasterXSize() * mGeoTransform[4] + - mGdalDataset->GetRasterYSize() * mGeoTransform[5]; + GDALGetRasterXSize(mGdalDataset) * mGeoTransform[4] + + GDALGetRasterYSize(mGdalDataset) * mGeoTransform[5]; mLayerExtent.setXmax(myXMax); // The affine transform reduces to these values at the @@ -548,8 +548,8 @@ bool QgsRasterLayer::readFile( QString const & fileName ) // // Set up the x and y dimensions of this raster layer // - mRasterXDim = mGdalDataset->GetRasterXSize(); - mRasterYDim = mGdalDataset->GetRasterYSize(); + mRasterXDim = GDALGetRasterXSize(mGdalDataset); + mRasterYDim = GDALGetRasterYSize(mGdalDataset); // // Determin the nodatavalue @@ -557,7 +557,7 @@ bool QgsRasterLayer::readFile( QString const & fileName ) mNoDataValue = -9999; //Standard default? mValidNoDataValue = false; int isValid = false; - double myNoDataValue = mGdalDataset->GetRasterBand(1)->GetNoDataValue(&isValid); + double myNoDataValue = GDALGetRasterNoDataValue(GDALGetRasterBand(mGdalDataset,1),&isValid); if(isValid) { mNoDataValue = myNoDataValue; @@ -571,10 +571,10 @@ bool QgsRasterLayer::readFile( QString const & fileName ) } //initialise the raster band stats and contrast enhancement vector - for (int i = 1; i <= mGdalDataset->GetRasterCount(); i++) + for (int i = 1; i <= GDALGetRasterCount(mGdalDataset); i++) { - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(i); - QString myColorQString = GDALGetColorInterpretationName(myGdalBand->GetColorInterpretation()); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,i); + QString myColorQString = GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(myGdalBand)); QgsRasterBandStats myRasterBandStats; //myRasterBandStats.bandName = myColorQString ; myRasterBandStats.bandName=QString::number(i) + " : " + myColorQString; @@ -587,7 +587,7 @@ bool QgsRasterLayer::readFile( QString const & fileName ) mRasterStatsList.push_back(myRasterBandStats); //Build a new contrast enhancement for the band and store in list - QgsContrastEnhancement myContrastEnhancement((QgsContrastEnhancement::QgsRasterDataType)myGdalBand->GetRasterDataType()); + QgsContrastEnhancement myContrastEnhancement((QgsContrastEnhancement::QgsRasterDataType)GDALGetRasterDataType(myGdalBand)); mContrastEnhancementList.append(myContrastEnhancement); } @@ -597,7 +597,7 @@ bool QgsRasterLayer::readFile( QString const & fileName ) //decide what type of layer this is... //note that multiband images can have one or more 'undefindd' bands, //so we must do this check first! - if ((mGdalDataset->GetRasterCount() > 1)) + if ((GDALGetRasterCount(mGdalDataset) > 1)) { rasterLayerType = MULTIBAND; } @@ -626,7 +626,7 @@ bool QgsRasterLayer::readFile( QString const & fileName ) mRedBandName = getRasterBandName(1); // sensible default mGreenBandName = getRasterBandName(2); // sensible default //for the third layer we cant be sure so.. - if (mGdalDataset->GetRasterCount() > 2) + if (GDALGetRasterCount(mGdalDataset) > 2) { mBlueBandName = getRasterBandName(3); // sensible default } @@ -669,12 +669,12 @@ QString QgsRasterLayer::getProjectionWKT() { QString myWKTString; QgsSpatialRefSys mySRS; - myWKTString=QString (mGdalDataset->GetProjectionRef()); + myWKTString=QString (GDALGetProjectionRef(mGdalDataset)); mySRS.createFromWkt(myWKTString); if (!mySRS.isValid()) { //try to get the gcp srs from the raster layer if available - myWKTString=QString(mGdalDataset->GetGCPProjection()); + myWKTString=QString(GDALGetGCPProjection(mGdalDataset)); // What is the purpose of this piece of code? // Sideeffects from validate()? @@ -695,7 +695,7 @@ void QgsRasterLayer::closeDataset() if ( !mValid ) return; mValid = FALSE; - mGdalBaseDataset->Dereference(); + GDALDereferenceDataset(mGdalBaseDataset); mGdalBaseDataset = NULL; GDALClose(mGdalDataset); @@ -884,10 +884,10 @@ bool QgsRasterLayer::hasBand(QString const & theBandName) { QgsDebugMsg("Looking for band : " + theBandName); - for (int i = 1; i <= mGdalDataset->GetRasterCount(); i++) + for (int i = 1; i <= GDALGetRasterCount(mGdalDataset); i++) { - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(i); - QString myColorQString = GDALGetColorInterpretationName(myGdalBand->GetColorInterpretation()); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,i); + QString myColorQString = GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(myGdalBand)); #ifdef QGISDEBUG QgsLogger::debug("band", i, __FILE__, __FUNCTION__, __LINE__, 2); #endif @@ -951,7 +951,7 @@ QPixmap QgsRasterLayer::getPaletteAsPixmap() { QgsDebugMsg("....found paletted image"); QgsColorTable *myColorTable = colorTable ( 1 ); - GDALRasterBandH myGdalBand = mGdalDataset->GetRasterBand(1); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,1); if( GDALGetRasterColorInterpretation(myGdalBand) == GCI_PaletteIndex && myColorTable->defined() ) { QgsDebugMsg("....found GCI_PaletteIndex"); @@ -1437,8 +1437,8 @@ void QgsRasterLayer::drawSingleBandGray(QPainter * theQPainter, QgsRasterViewPor return; } - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); - GDALDataType myDataType = myGdalBand->GetRasterDataType(); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); + GDALDataType myDataType = GDALGetRasterDataType(myGdalBand); void *myGdalScanData = readData ( myGdalBand, theRasterViewPort ); /* Check for out of memory error */ @@ -1541,8 +1541,8 @@ void QgsRasterLayer::drawSingleBandPseudoColor(QPainter * theQPainter, } QgsRasterBandStats myRasterBandStats = getRasterBandStats(theBandNo); - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); - GDALDataType myDataType = myGdalBand->GetRasterDataType(); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); + GDALDataType myDataType = GDALGetRasterDataType(myGdalBand); void *myGdalScanData = readData ( myGdalBand, theRasterViewPort ); /* Check for out of memory error */ @@ -1644,8 +1644,8 @@ void QgsRasterLayer::drawPalettedSingleBandColor(QPainter * theQPainter, QgsRast return; } - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); - GDALDataType myDataType = myGdalBand->GetRasterDataType(); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); + GDALDataType myDataType = GDALGetRasterDataType(myGdalBand); void *myGdalScanData = readData ( myGdalBand, theRasterViewPort ); /* Check for out of memory error */ @@ -1730,8 +1730,8 @@ void QgsRasterLayer::drawPalettedSingleBandGray(QPainter * theQPainter, QgsRaste } QgsRasterBandStats myRasterBandStats = getRasterBandStats(theBandNo); - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); - GDALDataType myDataType = myGdalBand->GetRasterDataType(); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); + GDALDataType myDataType = GDALGetRasterDataType(myGdalBand); void *myGdalScanData = readData ( myGdalBand, theRasterViewPort ); /* Check for out of memory error */ @@ -1829,8 +1829,8 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor(QPainter * theQPainter, Q } QgsRasterBandStats myRasterBandStats = getRasterBandStats(theBandNo); - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); - GDALDataType myDataType = myGdalBand->GetRasterDataType(); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); + GDALDataType myDataType = GDALGetRasterDataType(myGdalBand); void *myGdalScanData = readData ( myGdalBand, theRasterViewPort ); /* Check for out of memory error */ @@ -1957,8 +1957,8 @@ void QgsRasterLayer::drawPalettedMultiBandColor(QPainter * theQPainter, QgsRaste return; } - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); - GDALDataType myDataType = myGdalBand->GetRasterDataType(); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); + GDALDataType myDataType = GDALGetRasterDataType(myGdalBand); void *myGdalScanData = readData ( myGdalBand, theRasterViewPort ); /* Check for out of memory error */ @@ -2094,13 +2094,13 @@ void QgsRasterLayer::drawMultiBandColor(QPainter * theQPainter, QgsRasterViewPor { return; } - GDALRasterBand *myGdalRedBand = mGdalDataset->GetRasterBand(myRedBandNo); - GDALRasterBand *myGdalGreenBand = mGdalDataset->GetRasterBand(myGreenBandNo); - GDALRasterBand *myGdalBlueBand = mGdalDataset->GetRasterBand(myBlueBandNo); + GDALRasterBandH myGdalRedBand = GDALGetRasterBand(mGdalDataset,myRedBandNo); + GDALRasterBandH myGdalGreenBand = GDALGetRasterBand(mGdalDataset,myGreenBandNo); + GDALRasterBandH myGdalBlueBand = GDALGetRasterBand(mGdalDataset,myBlueBandNo); - GDALDataType myRedType = myGdalRedBand->GetRasterDataType(); - GDALDataType myGreenType = myGdalGreenBand->GetRasterDataType(); - GDALDataType myBlueType = myGdalBlueBand->GetRasterDataType(); + GDALDataType myRedType = GDALGetRasterDataType(myGdalRedBand); + GDALDataType myGreenType = GDALGetRasterDataType(myGdalGreenBand); + GDALDataType myBlueType = GDALGetRasterDataType(myGdalBlueBand); void *myGdalRedData = readData ( myGdalRedBand, theRasterViewPort ); void *myGdalGreenData = readData ( myGdalGreenBand, theRasterViewPort ); @@ -2294,7 +2294,7 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(QString const & theB //we cant use a vector iterator because the iterator is astruct not a class //and the qvector model does not like this. - for (int i = 1; i <= mGdalDataset->GetRasterCount(); i++) + for (int i = 1; i <= GDALGetRasterCount(mGdalDataset); i++) { QgsRasterBandStats myRasterBandStats = getRasterBandStats(i); if (myRasterBandStats.bandName == theBandNameQString) @@ -2395,7 +2395,7 @@ That this is a cpu intensive and slow task! const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(int theBandNo) { // check if we have received a valid band number - if ((mGdalDataset->GetRasterCount() < theBandNo) && rasterLayerType != PALETTE) + if ((GDALGetRasterCount(mGdalDataset) < theBandNo) && rasterLayerType != PALETTE) { // invalid band id, return nothing QgsRasterBandStats myNullReturnStats; @@ -2421,10 +2421,10 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(int theBandNo) emit setStatus(QString("Retrieving stats for ")+name()); qApp->processEvents(); QgsDebugMsg("QgsRasterLayer::retrieve stats for band " + QString::number(theBandNo)); - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); - QString myColorerpretation = GDALGetColorInterpretationName(myGdalBand->GetColorInterpretation()); + QString myColorerpretation = GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(myGdalBand)); //declare a colorTable to hold a palette - will only be used if the layer color interp is palette ??? //get the palette colour table @@ -2477,13 +2477,13 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(int theBandNo) // let the user know we're going to possibly be taking a while //QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); - GDALDataType myDataType = myGdalBand->GetRasterDataType(); + GDALDataType myDataType = GDALGetRasterDataType(myGdalBand); int myNXBlocks, myNYBlocks, myXBlockSize, myYBlockSize; - myGdalBand->GetBlockSize( &myXBlockSize, &myYBlockSize ); + GDALGetBlockSize( myGdalBand, &myXBlockSize, &myYBlockSize ); - myNXBlocks = (myGdalBand->GetXSize() + myXBlockSize - 1) / myXBlockSize; - myNYBlocks = (myGdalBand->GetYSize() + myYBlockSize - 1) / myYBlockSize; + myNXBlocks = (GDALGetRasterXSize(myGdalBand) + myXBlockSize - 1) / myXBlockSize; + myNYBlocks = (GDALGetRasterYSize(myGdalBand) + myYBlockSize - 1) / myYBlockSize; void *myData = CPLMalloc ( myXBlockSize * myYBlockSize * GDALGetDataTypeSize(myDataType)/8 ); @@ -2499,21 +2499,21 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(int theBandNo) //ifdefs below to remove compiler warning about unused vars #ifdef QGISDEBUG int success; - double GDALminimum = myGdalBand->GetMinimum( &success ); + double GDALminimum = GDALGetRasterMinimum( myGdalBand, &success ); if ( ! success ) { QgsDebugMsg("myGdalBand->GetMinimum() failed"); } - double GDALmaximum = myGdalBand->GetMaximum( &success ); + double GDALmaximum = GDALGetRasterMaximum( myGdalBand, &success ); if ( ! success ) { QgsDebugMsg("myGdalBand->GetMaximum() failed"); } - double GDALnodata = myGdalBand->GetNoDataValue( &success ); + double GDALnodata = GDALGetRasterNoDataValue( myGdalBand, &success ); if ( ! success ) { @@ -2538,8 +2538,8 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(int theBandNo) QgsDebugMsg("starting manual stat computation"); #endif - int myGdalBandXSize = myGdalBand->GetXSize(); - int myGdalBandYSize = myGdalBand->GetYSize(); + int myGdalBandXSize = GDALGetRasterXSize(myGdalBand); + int myGdalBandYSize = GDALGetRasterYSize(myGdalBand); for( int iYBlock = 0; iYBlock < myNYBlocks; iYBlock++ ) { emit drawingProgress ( iYBlock, myNYBlocks * 2 ); @@ -2547,7 +2547,7 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(int theBandNo) for( int iXBlock = 0; iXBlock < myNXBlocks; iXBlock++ ) { int nXValid, nYValid; - myGdalBand->ReadBlock( iXBlock, iYBlock, myData ); + GDALReadBlock( myGdalBand, iXBlock, iYBlock, myData ); // Compute the portion of the block that is valid // for partial edge blocks. @@ -2617,8 +2617,8 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats(int theBandNo) for( int iXBlock = 0; iXBlock < myNXBlocks; iXBlock++ ) { int nXValid, nYValid; - - myGdalBand->ReadBlock( iXBlock, iYBlock, myData ); + + GDALReadBlock( myGdalBand, iXBlock, iYBlock, myData ); // Compute the portion of the block that is valid // for partial edge blocks. @@ -2926,8 +2926,8 @@ QPixmap QgsRasterLayer::getLegendQPixmap(bool theWithNameFlag) // // Get the adjusted matrix stats // - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(1); - QString myColorerpretation = GDALGetColorInterpretationName(myGdalBand->GetColorInterpretation()); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,1); + QString myColorerpretation = GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(myGdalBand)); @@ -3178,8 +3178,8 @@ QPixmap QgsRasterLayer::getDetailedLegendQPixmap(int theLabelCount=3) // // Get the adjusted matrix stats // - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(1); - QString myColorerpretation = GDALGetColorInterpretationName(myGdalBand->GetColorInterpretation()); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,1); + QString myColorerpretation = GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(myGdalBand)); QPixmap myLegendQPixmap; //will be initialised once we know what drawing style is active QPainter myQPainter; // @@ -3502,9 +3502,9 @@ QString QgsRasterLayer::getMetadata() myMetadataQString += ""; if (mProviderKey.isEmpty()) { - myMetadataQString += QString(mGdalDataset->GetDriver()->GetDescription()); + myMetadataQString += QString(GDALGetDescription(GDALGetDatasetDriver(mGdalDataset))); myMetadataQString += "
"; - myMetadataQString += QString(mGdalDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME)); + myMetadataQString += QString(GDALGetMetadataItem(GDALGetDatasetDriver(mGdalDataset),GDAL_DMD_LONGNAME,NULL)); } else { @@ -3526,11 +3526,11 @@ QString QgsRasterLayer::getMetadata() myMetadataQString += tr("Dataset Description"); myMetadataQString += ""; myMetadataQString += ""; - myMetadataQString += QFile::decodeName(mGdalDataset->GetDescription()); + myMetadataQString += QFile::decodeName(GDALGetDescription(mGdalDataset)); myMetadataQString += ""; - char ** GDALmetadata = mGdalDataset->GetMetadata(); + char ** GDALmetadata = GDALGetMetadata(mGdalDataset,NULL); if ( GDALmetadata ) { @@ -3542,9 +3542,10 @@ QString QgsRasterLayer::getMetadata() QgsDebugMsg("dataset has no metadata"); } - for ( int i = 1; i <= mGdalDataset->GetRasterCount(); ++i ) + for ( int i = 1; i <= GDALGetRasterCount(mGdalDataset); ++i ) { - mGdalDataset->GetRasterBand(i)->GetMetadata(); + GDALRasterBandH gdalBand = GDALGetRasterBand(mGdalDataset,i); + GDALmetadata = GDALGetMetadata(gdalBand,NULL); if ( GDALmetadata ) { @@ -3556,7 +3557,7 @@ QString QgsRasterLayer::getMetadata() QgsDebugMsg("band " + QString::number(i) + "has no metadata"); } - char ** GDALcategories = mGdalDataset->GetRasterBand(i)->GetCategoryNames(); + char ** GDALcategories = GDALGetRasterCategoryNames(gdalBand); if ( GDALcategories ) { @@ -3576,12 +3577,12 @@ QString QgsRasterLayer::getMetadata() myMetadataQString += tr("Dimensions:"); myMetadataQString += ""; myMetadataQString += ""; - myMetadataQString += tr("X: ") + QString::number(mGdalDataset->GetRasterXSize()) + - tr(" Y: ") + QString::number(mGdalDataset->GetRasterYSize()) + tr(" Bands: ") + QString::number(mGdalDataset->GetRasterCount()); + myMetadataQString += tr("X: ") + QString::number(GDALGetRasterXSize(mGdalDataset)) + + tr(" Y: ") + QString::number(GDALGetRasterYSize(mGdalDataset)) + tr(" Bands: ") + QString::number(GDALGetRasterCount(mGdalDataset)); myMetadataQString += ""; //just use the first band - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(1); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,1); myMetadataQString += ""; myMetadataQString += tr("No Data Value"); @@ -3602,7 +3603,7 @@ QString QgsRasterLayer::getMetadata() myMetadataQString += tr("Data Type:"); myMetadataQString += ""; myMetadataQString += ""; - switch (myGdalBand->GetRasterDataType()) + switch (GDALGetRasterDataType(myGdalBand)) { case GDT_Byte: myMetadataQString += tr("GDT_Byte - Eight bit unsigned integer"); @@ -3683,7 +3684,7 @@ QString QgsRasterLayer::getMetadata() if (mProviderKey.isEmpty()) { - if (mGdalDataset->GetGeoTransform(mGeoTransform) != CE_None) + if (GDALGetGeoTransform(mGdalDataset,mGeoTransform) != CE_None) { // if the raster does not have a valid transform we need to use // a pixel size of (1,-1), but GDAL returns (1,1) @@ -3858,14 +3859,14 @@ QString QgsRasterLayer::buildPyramids(RasterPyramidList const & theRasterPyramid registerGdalDrivers(); //close the gdal dataset and reopen it in read / write mode - delete mGdalDataset; - mGdalDataset = (GDALDataset *) GDALOpen(QFile::encodeName(mDataSource).constData(), GA_Update); + GDALClose( mGdalDataset ); + mGdalDataset = GDALOpen(QFile::encodeName(mDataSource).constData(), GA_Update); // if the dataset couldn't be opened in read / write mode, tell the user if (!mGdalDataset) { emit drawingProgress(0,0); - mGdalDataset = (GDALDataset *) GDALOpen(QFile::encodeName(mDataSource).constData(), GA_ReadOnly); + mGdalDataset = GDALOpen(QFile::encodeName(mDataSource).constData(), GA_ReadOnly); return "ERROR_WRITE_FORMAT"; } @@ -3912,26 +3913,26 @@ QString QgsRasterLayer::buildPyramids(RasterPyramidList const & theRasterPyramid //see ticket #284 if(theResamplingMethod==tr("Average Magphase")) { - myError = mGdalDataset->BuildOverviews( "MODE", 1, myOverviewLevelsArray, 0, NULL, + myError = GDALBuildOverviews( mGdalDataset, "MODE", 1, myOverviewLevelsArray, 0, NULL, GDALDummyProgress, NULL ); } else if(theResamplingMethod==tr("Average")) { - myError = mGdalDataset->BuildOverviews( "AVERAGE", 1, myOverviewLevelsArray, 0, NULL, + myError = GDALBuildOverviews( mGdalDataset, "AVERAGE", 1, myOverviewLevelsArray, 0, NULL, GDALDummyProgress, NULL ); } else // fall back to nearest neighbor { - myError = mGdalDataset->BuildOverviews( "NEAREST", 1, myOverviewLevelsArray, 0, NULL, + myError = GDALBuildOverviews( mGdalDataset, "NEAREST", 1, myOverviewLevelsArray, 0, NULL, GDALDummyProgress, NULL ); } if (myError == CE_Failure || CPLGetLastErrorNo()==CPLE_NotSupported ) { //something bad happenend //QString myString = QString (CPLGetLastError()); - delete mGdalDataset; - mGdalDataset = (GDALDataset *) GDALOpen(QFile::encodeName(mDataSource).constData(), GA_ReadOnly); + GDALClose( mGdalDataset ); + mGdalDataset = GDALOpen(QFile::encodeName(mDataSource).constData(), GA_ReadOnly); emit drawingProgress(0,0); return "FAILED_NOT_SUPPORTED"; } @@ -3947,8 +3948,8 @@ QString QgsRasterLayer::buildPyramids(RasterPyramidList const & theRasterPyramid } QgsDebugMsg("Pyramid overviews built"); //close the gdal dataset and reopen it in read only mode - delete mGdalDataset; - mGdalDataset = (GDALDataset *) GDALOpen(QFile::encodeName(mDataSource).constData(), GA_ReadOnly); + GDALClose( mGdalDataset ); + mGdalDataset = GDALOpen(QFile::encodeName(mDataSource).constData(), GA_ReadOnly); emit drawingProgress(0,0); return NULL; // returning null on success } @@ -4043,12 +4044,12 @@ bool QgsRasterLayer::isEditable() const -void QgsRasterLayer::readColorTable ( GDALRasterBand *gdalBand, QgsColorTable *theColorTable ) +void QgsRasterLayer::readColorTable ( GDALRasterBandH gdalBand, QgsColorTable *theColorTable ) { QgsDebugMsg("QgsRasterLayer::readColorTable()"); // First try to read color table from metadata - char **metadata = gdalBand->GetMetadata(); + char **metadata = GDALGetMetadata(gdalBand,NULL); theColorTable->clear(); bool found = false; while ( metadata && metadata[0] ) @@ -4079,15 +4080,15 @@ void QgsRasterLayer::readColorTable ( GDALRasterBand *gdalBand, QgsColorTable *t // If no color table was found, try to read it from GDALColorTable if ( !found ) { - GDALColorTable *gdalColorTable = gdalBand->GetColorTable(); + GDALColorTableH gdalColorTable = GDALGetRasterColorTable(gdalBand); if ( gdalColorTable ) { - int count = gdalColorTable->GetColorEntryCount(); + int count = GDALGetColorEntryCount(gdalColorTable); for ( int i = 0; i < count; i++ ) { - const GDALColorEntry *colorEntry = gdalColorTable->GetColorEntry ( i ); + const GDALColorEntry *colorEntry = GDALGetColorEntry(gdalColorTable,i); if ( !colorEntry ) continue; @@ -4109,9 +4110,9 @@ QgsColorTable *QgsRasterLayer::colorTable ( int theBandNo ) } -void *QgsRasterLayer::readData ( GDALRasterBand *gdalBand, QgsRasterViewPort *viewPort ) +void *QgsRasterLayer::readData ( GDALRasterBandH gdalBand, QgsRasterViewPort *viewPort ) { - GDALDataType type = gdalBand->GetRasterDataType(); + GDALDataType type = GDALGetRasterDataType(gdalBand); int size = GDALGetDataTypeSize ( type ) / 8; QgsDebugMsg("QgsRasterLayer::readData: calling RasterIO with " +\ @@ -4131,15 +4132,15 @@ void *QgsRasterLayer::readData ( GDALRasterBand *gdalBand, QgsRasterViewPort *vi } else { - CPLErr myErr = gdalBand->RasterIO ( GF_Read, - viewPort->rectXOffset, - viewPort->rectYOffset, - viewPort->clippedWidth, - viewPort->clippedHeight, - data, - viewPort->drawableAreaXDim, - viewPort->drawableAreaYDim, - type, 0, 0 ); + CPLErr myErr = GDALRasterIO( gdalBand, GF_Read, + viewPort->rectXOffset, + viewPort->rectYOffset, + viewPort->clippedWidth, + viewPort->clippedHeight, + data, + viewPort->drawableAreaXDim, + viewPort->drawableAreaYDim, + type, 0, 0 ); if (myErr != CPLE_None) { QgsLogger::warning("RaterIO error: " + QString(CPLGetLastErrorMsg())); @@ -4793,7 +4794,7 @@ void QgsRasterLayer::identify(const QgsPoint& point, std::map& if ( x < mLayerExtent.xMin() || x > mLayerExtent.xMax() || y < mLayerExtent.yMin() || y > mLayerExtent.yMax() ) { // Outside the raster - for ( int i = 1; i <= mGdalDataset->GetRasterCount(); i++ ) + for ( int i = 1; i <= GDALGetRasterCount(mGdalDataset); i++ ) { results[tr("Band") + QString::number(i)] = tr("out of extent"); } @@ -4810,14 +4811,15 @@ void QgsRasterLayer::identify(const QgsPoint& point, std::map& QgsDebugMsg( "row = " + QString::number(row) + " col = " + QString::number(col)); - for ( int i = 1; i <= mGdalDataset->GetRasterCount(); i++ ) + for ( int i = 1; i <= GDALGetRasterCount(mGdalDataset); i++ ) { - GDALRasterBand *gdalBand = mGdalDataset->GetRasterBand(i); - GDALDataType type = gdalBand->GetRasterDataType(); + GDALRasterBandH gdalBand = GDALGetRasterBand(mGdalDataset,i); + GDALDataType type = GDALGetRasterDataType(gdalBand); int size = GDALGetDataTypeSize ( type ) / 8; void *data = CPLMalloc ( size ); - CPLErr err = gdalBand->RasterIO ( GF_Read, col, row, 1, 1, data, 1, 1, type, 0, 0 ); + CPLErr err = GDALRasterIO( gdalBand, GF_Read, col, row, 1, 1, + data, 1, 1, type, 0, 0 ); if (err != CPLE_None) { @@ -4861,7 +4863,7 @@ QString QgsRasterLayer::identifyAsText(const QgsPoint& point) void QgsRasterLayer::populateHistogram(int theBandNo, int theBinCount,bool theIgnoreOutOfRangeFlag,bool theHistogramEstimatedFlag) { - GDALRasterBand *myGdalBand = mGdalDataset->GetRasterBand(theBandNo); + GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,theBandNo); QgsRasterBandStats myRasterBandStats = getRasterBandStats(theBandNo); //calculate the histogram for this band //we assume that it only needs to be calculated if the lenght of the histogram @@ -4891,7 +4893,7 @@ void QgsRasterLayer::populateHistogram(int theBandNo, int theBinCount,bool theIg * ) */ double myerval = (myRasterBandStats.maxVal-myRasterBandStats.minVal)/theBinCount; - myGdalBand->GetHistogram( myRasterBandStats.minVal-0.1*myerval, myRasterBandStats.maxVal+0.1*myerval, theBinCount, myHistogramArray ,theIgnoreOutOfRangeFlag ,theHistogramEstimatedFlag , GDALDummyProgress, NULL ); + GDALGetRasterHistogram( myGdalBand, myRasterBandStats.minVal-0.1*myerval, myRasterBandStats.maxVal+0.1*myerval, theBinCount, myHistogramArray ,theIgnoreOutOfRangeFlag ,theHistogramEstimatedFlag , GDALDummyProgress, NULL ); for (int myBin = 0; myBin +#define CPL_SUPRESS_CPLUSPLUS + +#include // // Forward declarations @@ -163,9 +165,6 @@ class QgsRasterLayerProperties; struct QgsRasterViewPort; class QImage; class QPixmap; - -class GDALDataset; -class GDALRasterBand; class QSlider; class QLibrary; @@ -316,10 +315,12 @@ public: void resetNoDataValue() { mNoDataValue = -9999; - if(mGdalDataset != NULL && mGdalDataset->GetRasterCount() > 0) + if(mGdalDataset != NULL && GDALGetRasterCount(mGdalDataset) > 0) { int myRequestValid; - double myValue = mGdalDataset->GetRasterBand(1)->GetNoDataValue(&myRequestValid); + double myValue = GDALGetRasterNoDataValue( + GDALGetRasterBand( mGdalDataset, 1 ), &myRequestValid); + if(0 != myRequestValid) { setNoDataValue(myValue); @@ -947,13 +948,13 @@ private: QgsMapToPixel * theQgsMapToPixel, QImage* theImage); /** \brief Read color table from GDAL raster band */ - void readColorTable ( GDALRasterBand *gdalBand, QgsColorTable *theColorTable ); + void readColorTable ( GDALRasterBandH gdalBand, QgsColorTable *theColorTable ); /** \brief Allocate memory and load data to that allocated memory, data type is the same * as raster band. The memory must be released later! * \return pointer to the memory */ - void *readData ( GDALRasterBand *gdalBand, QgsRasterViewPort *viewPort ); + void *readData ( GDALRasterBandH gdalBand, QgsRasterViewPort *viewPort ); /** \brief Read a raster value on given position from memory block created by readData() * \param index index in memory block @@ -992,9 +993,9 @@ private: /** \brief Flag to indicate whether debug infor overlay should be rendered onto the raster. */ bool mDebugOverlayFlag; /** \brief Pointer to the gdaldataset. */ - GDALDataset * mGdalBaseDataset; + GDALDatasetH mGdalBaseDataset; /** \brief Pointer to the gdaldataset (possibly warped vrt). */ - GDALDataset * mGdalDataset; + GDALDatasetH mGdalDataset; /** \brief Values for mapping pixel to world coordinates. Contents of * this array are the same as the gdal adfGeoTransform */ double mGeoTransform[6]; diff --git a/src/plugins/georeferencer/qgsimagewarper.cpp b/src/plugins/georeferencer/qgsimagewarper.cpp index 158d83845d9..19a8450a326 100644 --- a/src/plugins/georeferencer/qgsimagewarper.cpp +++ b/src/plugins/georeferencer/qgsimagewarper.cpp @@ -19,9 +19,7 @@ #include #include #include -#include #include -#include #include @@ -32,12 +30,11 @@ void QgsImageWarper::warp(const QString& input, const QString& output, ResamplingMethod resampling, bool useZeroAsTrans, const QString& compression) { // Open input file GDALAllRegister(); - GDALDataset* hSrcDS = static_cast(GDALOpen(QFile::encodeName(input).constData(), - GA_ReadOnly)); + GDALDatasetH hSrcDS = GDALOpen(QFile::encodeName(input).constData(),GA_ReadOnly); // Setup warp options. GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions(); psWarpOptions->hSrcDS = hSrcDS; - psWarpOptions->nBandCount = hSrcDS->GetRasterCount(); + psWarpOptions->nBandCount = GDALGetRasterCount(hSrcDS); psWarpOptions->panSrcBands = (int *) CPLMalloc(sizeof(int) * psWarpOptions->nBandCount); psWarpOptions->panDstBands = @@ -52,8 +49,8 @@ void QgsImageWarper::warp(const QString& input, const QString& output, // check the bounds for the warped raster // order: upper right, lower right, lower left (y points down) - double x[] = { hSrcDS->GetRasterXSize(), hSrcDS->GetRasterXSize(), 0 }; - double y[] = { 0, hSrcDS->GetRasterYSize(), hSrcDS->GetRasterYSize() }; + double x[] = { GDALGetRasterXSize(hSrcDS), GDALGetRasterXSize(hSrcDS), 0 }; + double y[] = { 0, GDALGetRasterYSize(hSrcDS), GDALGetRasterYSize(hSrcDS) }; int s[] = { 0, 0, 0 }; TransformParameters tParam = { mAngle, 0, 0 }; transform(&tParam, FALSE, 3, x, y, NULL, s); @@ -73,32 +70,37 @@ void QgsImageWarper::warp(const QString& input, const QString& output, psWarpOptions->pTransformerArg = &tParam; // create the output file - GDALDriver* driver = static_cast(GDALGetDriverByName("GTiff")); + GDALDriverH driver = GDALGetDriverByName("GTiff"); char **papszOptions = NULL; papszOptions = CSLSetNameValue(papszOptions, "INIT_DEST", "NO_DATA"); papszOptions = CSLSetNameValue(papszOptions, "COMPRESS", compression); - GDALDataset* hDstDS = - driver->Create(QFile::encodeName(output).constData(), newXSize, newYSize, - hSrcDS->GetRasterCount(), - hSrcDS->GetRasterBand(1)->GetRasterDataType(), - papszOptions); - for (int i = 0; i < hSrcDS->GetRasterCount(); ++i) + GDALDatasetH hDstDS = + GDALCreate(driver, + QFile::encodeName(output).constData(), newXSize, newYSize, + GDALGetRasterCount(hSrcDS), + GDALGetRasterDataType(GDALGetRasterBand(hSrcDS,1)), + papszOptions ); + + for( int i = 0; i < GDALGetRasterCount(hSrcDS); ++i ) + { + GDALRasterBandH hSrcBand = GDALGetRasterBand(hSrcDS,i+1); + GDALRasterBandH hDstBand = GDALGetRasterBand(hDstDS,i+1); + GDALColorTableH cTable = GDALGetRasterColorTable(hSrcDS); + if (cTable) { - GDALColorTable* cTable = hSrcDS->GetRasterBand(i+1)->GetColorTable(); - if (cTable) - { - hDstDS->GetRasterBand(i+1)->SetColorTable(cTable); - } - double noData = hSrcDS->GetRasterBand(i+1)->GetNoDataValue(); - if (noData == -1e10 && useZeroAsTrans) - { - hDstDS->GetRasterBand(i+1)->SetNoDataValue(0); - } - else - { - hDstDS->GetRasterBand(i+1)->SetNoDataValue(noData); - } + GDALSetRasterColorTable(hDstDS,cTable); } + + double noData = GDALGetRasterNoDataValue(hSrcBand,NULL); + if (noData == -1e10 && useZeroAsTrans) + { + GDALSetRasterNoDataValue(hDstBand,0); + } + else + { + GDALSetRasterNoDataValue(hDstBand,noData); + } + } psWarpOptions->hDstDS = hDstDS; // Initialize and execute the warp operation. @@ -107,8 +109,9 @@ void QgsImageWarper::warp(const QString& input, const QString& output, oOperation.ChunkAndWarpImage(0, 0, GDALGetRasterXSize(hDstDS), GDALGetRasterYSize(hDstDS)); GDALDestroyWarpOptions(psWarpOptions); - delete hSrcDS; - delete hDstDS; + + GDALClose( hSrcDS ); + GDALClose( hDstDS ); } diff --git a/src/plugins/spit/qgsshapefile.cpp b/src/plugins/spit/qgsshapefile.cpp index 0aef51f7126..07139905470 100644 --- a/src/plugins/spit/qgsshapefile.cpp +++ b/src/plugins/spit/qgsshapefile.cpp @@ -17,8 +17,7 @@ /* $Id$ */ #include -#include -#include +#include #include #include #include @@ -49,11 +48,11 @@ QgsShapeFile::QgsShapeFile(QString name, QString encoding){ filename = name; features = 0; OGRRegisterAll(); - ogrDataSource = OGRSFDriverRegistrar::Open(QFile::encodeName(filename).constData()); + ogrDataSource = OGROpen(QFile::encodeName(filename).constData(),FALSE,NULL); if (ogrDataSource != NULL){ valid = true; - ogrLayer = ogrDataSource->GetLayer(0); - features = ogrLayer->GetFeatureCount(); + ogrLayer = OGR_DS_GetLayer(ogrDataSource,0); + features = OGR_L_GetFeatureCount(ogrLayer,TRUE); } else valid = false; @@ -68,12 +67,7 @@ QgsShapeFile::QgsShapeFile(QString name, QString encoding){ } QgsShapeFile::~QgsShapeFile(){ - if(ogrDataSource != 0) - { - // don't delete the layer if the datasource is bad -- (causes crash) - delete ogrLayer; - } - delete ogrDataSource; + OGR_DS_Destroy( ogrDataSource ); delete filename; delete geom_type; } @@ -92,26 +86,26 @@ bool QgsShapeFile::scanGeometries() sg->show(); qApp->processEvents(); - OGRFeature *feat; + OGRFeatureH feat; OGRwkbGeometryType currentType = wkbUnknown; bool multi = false; - while((feat = ogrLayer->GetNextFeature())) + while((feat = OGR_L_GetNextFeature(ogrLayer))) { qApp->processEvents(); // feat->DumpReadable(NULL); - OGRGeometry *geom = feat->GetGeometryRef(); + OGRGeometryH geom = OGR_F_GetGeometryRef(feat); if(geom) { - QString gml = geom->exportToGML(); + QString gml = OGR_G_ExportToGML(geom); // std::cerr << gml << std::endl; if(gml.find("gml:Multi") > -1) { // std::cerr << "MULTI Part Feature detected" << std::endl; multi = true; } - OGRFeatureDefn *fDef = feat->GetDefnRef(); - OGRwkbGeometryType gType = fDef->GetGeomType(); + OGRFeatureDefnH fDef = OGR_F_GetDefnRef(feat); + OGRwkbGeometryType gType = OGR_FD_GetGeomType(fDef); // std::cerr << fDef->GetGeomType() << std::endl; if(gType > currentType) { @@ -135,7 +129,7 @@ bool QgsShapeFile::scanGeometries() hasMoreDimensions = true; } - ogrLayer->ResetReading(); + OGR_L_ResetReading(ogrLayer); geom_type = geometries[currentType]; if(multi && (geom_type.find("MULTI") == -1)) { @@ -151,14 +145,14 @@ QString QgsShapeFile::getFeatureClass(){ // type. qApp->processEvents(); isMulti = scanGeometries(); - OGRFeature *feat; + OGRFeatureH feat; // skip features without geometry - while ((feat = ogrLayer->GetNextFeature()) != NULL) { - if (feat->GetGeometryRef()) + while ((feat = OGR_L_GetNextFeature(ogrLayer)) != NULL) { + if (OGR_F_GetGeometryRef(feat)) break; } if(feat){ - OGRGeometry *geom = feat->GetGeometryRef(); + OGRGeometryH geom = OGR_F_GetGeometryRef(feat); if(geom){ /* OGR doesn't appear to report geometry type properly * for a layer containing both polygon and multipolygon @@ -222,18 +216,18 @@ QString QgsShapeFile::getFeatureClass(){ } } dbf.close(); - int numFields = feat->GetFieldCount(); + int numFields = OGR_F_GetFieldCount(feat); for(int n=0; ntoUnicode(feat->GetFieldDefnRef(n)->GetNameRef()); + QString s = codec->toUnicode(OGR_Fld_GetNameRef(OGR_F_GetFieldDefnRef(feat,n))); column_names.push_back(s); } }else valid = false; - delete feat; + OGR_F_Destroy( feat ); }else valid = false; - ogrLayer->ResetReading(); + OGR_L_ResetReading(ogrLayer); return valid?geom_type:QString::null; } @@ -380,20 +374,20 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col, break; } - OGRFeature *feat = ogrLayer->GetNextFeature(); + OGRFeatureH feat = OGR_L_GetNextFeature(ogrLayer); if(feat){ - OGRGeometry *geom = feat->GetGeometryRef(); + OGRGeometryH geom = OGR_F_GetGeometryRef(feat); if(geom){ query = "INSERT INTO \"" + schema + "\".\"" + table_name + "\"" + QString(" VALUES( %1, ").arg(m); - int num = geom->WkbSize(); + int num = OGR_G_WkbSize(geom); char * geo_temp = new char[num*3]; // 'GeometryFromText' supports only 2D coordinates // TODO for proper 2.5D support we would need to use 'GeomFromEWKT' if (hasMoreDimensions) - geom->setCoordinateDimension(2); - geom->exportToWkt(&geo_temp); + OGR_G_SetCoordinateDimension(geom,2); + OGR_G_ExportToWkt(geom,&geo_temp); QString geometry(geo_temp); QString quotes; @@ -409,7 +403,7 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col, query += quotes; // escape the string value and cope with blank data - QString val = codec->toUnicode(feat->GetFieldAsString(n)); + QString val = codec->toUnicode(OGR_F_GetFieldAsString(feat,n)); if (val.isEmpty() && numericType) { val = "NULL"; @@ -452,7 +446,7 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col, qApp->processEvents(); delete[] geo_temp; } - delete feat; + OGR_F_Destroy( feat ); } } // create the GIST index if the the load was successful @@ -461,7 +455,7 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col, // prompt user to see if they want to build the index and warn // them about the potential time-cost } - ogrLayer->ResetReading(); + OGR_L_ResetReading(ogrLayer); return result; } diff --git a/src/plugins/spit/qgsshapefile.h b/src/plugins/spit/qgsshapefile.h index 906b29dfefb..0a2a3971223 100644 --- a/src/plugins/spit/qgsshapefile.h +++ b/src/plugins/spit/qgsshapefile.h @@ -23,7 +23,7 @@ #include #include #include -#include +#include class QProgressDialog; class QTextCodec; @@ -62,8 +62,8 @@ class QgsShapeFile : public QObject private: QString table_name; - OGRDataSource *ogrDataSource; - OGRLayer * ogrLayer; + OGRDataSourceH ogrDataSource; + OGRLayerH ogrLayer; bool import_canceled; bool valid; //! Flag to indicate the file contains multiple geometry types diff --git a/src/providers/mysql/qgsmysqlprovider.cpp b/src/providers/mysql/qgsmysqlprovider.cpp index 7c1703f3eaf..f53149e2807 100644 --- a/src/providers/mysql/qgsmysqlprovider.cpp +++ b/src/providers/mysql/qgsmysqlprovider.cpp @@ -31,8 +31,6 @@ #include #include -#include - #include "../../src/qgsdataprovider.h" #include "../../src/qgsencodingfiledialog.h" #include "../../src/qgsfeature.h" @@ -790,199 +788,8 @@ int QgsMySQLProvider::capabilities() const bool QgsMySQLProvider::saveAsShapefile() { - // save the layer as a shapefile - QString driverName = "ESRI Shapefile"; - OGRSFDriver *poDriver; - OGRRegisterAll(); - poDriver = - OGRSFDriverRegistrar::GetRegistrar()-> - GetDriverByName((const char *)driverName.toLocal8Bit().data()); - bool returnValue = true; - if (poDriver != NULL) - { - // get a name for the shapefile - // Get a file to process, starting at the current directory - // Set inital dir to last used in MySQL plugin - QSettings settings; - QString enc; - QString shapefileName; - QString filter = QString("Shapefiles (*.shp)"); - QString dirName = settings.readEntry("/Plugin-MySQL/text_path", "./"); - - QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog(0, - tr("Save layer as..."), - dirName, - filter, - QString("UTF-8")); - - // allow for selection of more than one file - openFileDialog->setMode(QFileDialog::AnyFile); - - - if (openFileDialog->exec() == QDialog::Accepted) - { - shapefileName = openFileDialog->selectedFile(); - enc = openFileDialog->encoding(); - } - else - { - return returnValue; - } - - if (!shapefileName.isNull()) - { - // add the extension if not present - if (shapefileName.find(".shp") == -1) - { - shapefileName += ".shp"; - } - OGRDataSource *poDS; - // create the data source - poDS = poDriver->CreateDataSource(QFile::encodeName(shapefileName).constData(), NULL); - if (poDS != NULL) - { - QTextCodec* saveCodec = QTextCodec::codecForName(enc.toLocal8Bit().data()); - if(!saveCodec) - { -#ifdef QGISDEBUG - qWarning("error finding QTextCodec in QgsMySQLProvider::saveAsShapefile()"); -#endif - saveCodec = QTextCodec::codecForLocale(); - } - - std::cerr << "created datasource" << std::endl; - // datasource created, now create the output layer, use utf8() for now. - OGRLayer *poLayer; - poLayer = - poDS->CreateLayer((const char *) (shapefileName. - left(shapefileName.find(".shp"))).utf8(), NULL, - static_cast < OGRwkbGeometryType > (1), NULL); - if (poLayer != NULL) - { - std::cerr << "created layer" << std::endl; - // calculate the field lengths - int *lengths = getFieldLengths(); - // create the fields - std::cerr << "creating " << attributeFields. - size() << " fields" << std::endl; - for (int i = 0; i < attributeFields.size(); i++) - { - // check the field length - if > 10 we need to truncate it - QgsField attrField = attributeFields[i]; - if (attrField.name().length() > 10) - { - attrField = attrField.name().left(10); - } - // all fields are created as string (for now) - OGRFieldDefn fld(saveCodec->fromUnicode(attrField.name()), OFTString); - // set the length for the field -- but we don't know what it is... - fld.SetWidth(lengths[i]); - // create the field - std::cerr << "creating field " << (const char *)attrField. - name().toLocal8Bit().data() << " width length " << lengths[i] << std::endl; - if (poLayer->CreateField(&fld) != OGRERR_NONE) - { - QMessageBox::warning(0, tr("Error"), - tr("Error creating field ") + attrField.name()); - } - } - // read the MySQL file and create the features - std::cerr << "Done creating fields" << std::endl; - // read the line - reset(); - QTextStream stream(mFile); - QString line; - while (!stream.atEnd()) - { - line = stream.readLine(); // line of text excluding '\n' - std::cerr << (const char *)line.toLocal8Bit().data() << std::endl; - // split the line - QStringList parts = - QStringList::split(QRegExp(mDelimiter), line, true); - std::cerr << "Split line into " << parts.size() << std::endl; - - // create the feature - OGRFeature *poFeature; - - poFeature = new OGRFeature(poLayer->GetLayerDefn()); - - // iterate over the parts and set the fields - std::cerr << "Setting the field values" << std::endl; - // set limit - we will ignore extra fields on the line - int limit = attributeFields.size(); - - if (parts.size() < limit) - { - - // this is bad - not enough values where supplied on the line - // TODO We should inform the user about this... - } - else - { - - for (int i = 0; i < limit; i++) - { - if (parts[i] != QString::null) - { - std::cerr << "Setting " << i << " " << (const char *)attributeFields[i]. - name().toLocal8Bit().data() << " to " << (const char *)parts[i].toLocal8Bit().data() << std::endl; - poFeature->SetField(saveCodec->fromUnicode(attributeFields[i].name()).data(), - saveCodec->fromUnicode(parts[i]).data()); - - } - else - { - poFeature->SetField(saveCodec->fromUnicode(attributeFields[i].name()).data(), ""); - } - } - std::cerr << "Field values set" << std::endl; - // create the point - OGRPoint *poPoint = new OGRPoint(); - QString sX = parts[fieldPositions[mXField]]; - QString sY = parts[fieldPositions[mYField]]; - poPoint->setX(sX.toDouble()); - poPoint->setY(sY.toDouble()); - std::cerr << "Setting geometry" << std::endl; - - poFeature->SetGeometryDirectly(poPoint); - if (poLayer->CreateFeature(poFeature) != OGRERR_NONE) - { - std::cerr << "Failed to create feature in shapefile" << std:: - endl; - } - else - { - std::cerr << "Added feature" << std::endl; - } - - delete poFeature; - } - } - delete poDS; - } - else - { - QMessageBox::warning(0, tr("Error"), tr("Layer creation failed")); - } - - } - else - { - QMessageBox::warning(0, tr("Error creating shapefile"), - tr("The shapefile could not be created (") + - shapefileName + ")"); - } - - } - //std::cerr << "Saving to " << shapefileName << std::endl; - } - else - { - QMessageBox::warning(0, tr("Driver not found"), - driverName + tr(" driver is not available")); - returnValue = false; - } - return returnValue; + // OGR based save to shapefile method removed, unused? + return false; } diff --git a/src/providers/ogr/qgsogrfactory.cpp b/src/providers/ogr/qgsogrfactory.cpp deleted file mode 100644 index 760848dd47e..00000000000 --- a/src/providers/ogr/qgsogrfactory.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/*************************************************************************** - qgsogrfactory.cpp - -------------------------------------- - Date : Sun Sep 16 12:18:21 AKDT 2007 - Copyright : (C) 2004 Christoph Spoerri - Email : - *************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ - -#include -#include - -#include "qgis.h" -#include "qgsogrfactory.h" - -QgsOGRFactory::QgsOGRFactory() -{ -} - - -QgsOGRFactory::~QgsOGRFactory() -{ -} - -bool QgsOGRFactory::testCapability(int t) -{ - switch (t) - { - case QgsDataProviderFactory::Copy: - return TRUE; - case QgsDataProviderFactory::Rename: - return FALSE; - case QgsDataProviderFactory::Move: - return TRUE; - default: - return FALSE; - } -} - -void QgsOGRFactory::setURI(QString uri) -{ - dataSourceURI = uri; - OGRRegisterAll(); - - // make connection to the data source - std::cerr << "Data source uri is " << dataSourceURI << std::endl; - // try to open for read - ogrDS = OGRSFDriverRegistrar::Open(QFile::encodeName(dataSourceURI).constData(), FALSE, &ogrDriver); - if(ogrDS != NULL) - { -#ifdef QGISDEBUG - std::cerr << "Data source is valid" << std::endl; -#endif - valid = true; - } else - { - QMessageBox::warning(NULL, tr("Wrong Path/URI"), tr("The provided path for the dataset is not valid.")); - valid = false; - } -} - -bool QgsOGRFactory::create(QString newLocation, QString newName, QString type) -{ - -} - -QgsDataProvider* QgsOGRFactory::open( QString name) -{ - QgsDataProvider *d = (QgsDataProvider *) new QgsShapeFileProvider(dataSourceURI+QString("/")+name); - return d; -} - -QStringList QgsOGRFactory::getLayers() -{ - OGRLayer *lyr; - OGRFeatureDefn *lyrDef; - QStringList lyrList = QStringList(); - - if (valid) { - for (int i = 0 ; i < ogrDS->GetLayerCount(); i++) - { - lyr = ogrDS->GetLayer(i); - lyrDef = lyr->GetLayerDefn(); - std::cerr << "Layer " << i << " is: " << lyrDef->GetName() << "\n"; - //TODO append extension to name, so that it can be opened properly - lyrList.append(lyrDef->GetName()); - } - } - return lyrList; -} - -bool QgsOGRFactory::copy(QString oldName, QString newName) -{ - OGRLayer *oldLyr; - std::cout << "Copy ogr datasource" << std::endl; - - if (valid) - { - oldLyr = ogrDS->GetLayerByName((const char*) oldName); - std::cout << "datasource is " << ogrDriver->GetName() << std::endl; - if (QString::compare(ogrDriver->GetName(),"ESRI Shapefile")==0) { - std::cout << "Copy shapefile" << std::endl; - - } - } -} - -/** - * Class factory to return a pointer to a newly created - * QgsShapeFileProvider object - */ -QGISEXTERN QgsOGRFactory * classFactory_Fact() -{ - return new QgsOGRFactory(); -} diff --git a/src/providers/ogr/qgsogrfactory.h b/src/providers/ogr/qgsogrfactory.h deleted file mode 100644 index 4fe3314b218..00000000000 --- a/src/providers/ogr/qgsogrfactory.h +++ /dev/null @@ -1,59 +0,0 @@ -/*************************************************************************** - qgsogrfactory.h - -------------------------------------- - Date : Sun Sep 16 12:18:58 AKDT 2007 - Copyright : (C) 2004 Christoph Spoerri - Email : - *************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ - -#ifndef QGSOGRFACTORY_H -#define QGSOGRFACTORY_H - -#include -#include - -#include - -#include "qgsshapefileprovider.h" -#include "../../src/qgsdataproviderfactory.h" -#include "../../src/qgsdataprovider.h" - - - -/** -@author Christoph Spoerri -*/ -class QgsOGRFactory : public QgsDataProviderFactory -{ -public: - QgsOGRFactory(); - virtual ~QgsOGRFactory(); - - QString getFactoryType() { return "OGR Dataprovider Factory"; } - bool testCapability(int); - void setURI(QString uri); - QStringList getLayers(); - bool create(QString newLocation, QString newName, QString type); - QgsDataProvider* open(QString name); - - bool copy(QString oldName, QString newName); - bool copy(QString oldName, QString newLocation, QString newName) {}; - bool move(QString newLocation) {}; - bool rename(QString newName) {}; - -private: - bool valid; - QString dataSourceURI; - OGRDataSource * ogrDS; - OGRSFDriver * ogrDriver; - -}; - -#endif diff --git a/src/providers/ogr/qgsogrprovider.cpp b/src/providers/ogr/qgsogrprovider.cpp index ecde4387f36..fb1b15b9d3c 100644 --- a/src/providers/ogr/qgsogrprovider.cpp +++ b/src/providers/ogr/qgsogrprovider.cpp @@ -22,13 +22,11 @@ email : sherman at mrcc.com #include #include +#define CPL_SUPRESS_CPLUSPLUS #include // to collect version information - -#include -#include -#include +#include +#include #include -#include "ogr_api.h"//only for a test #include #include @@ -83,14 +81,14 @@ QgsOgrProvider::QgsOgrProvider(QString const & uri) // try to open for update, but disable error messages to avoid a // message if the file is read only, because we cope with that // ourselves. - CPLPushErrorHandler(&CPLQuietErrorHandler); - ogrDataSource = OGRSFDriverRegistrar::Open(QFile::encodeName(uri).constData(), TRUE, &ogrDriver); + CPLPushErrorHandler(CPLQuietErrorHandler); + ogrDataSource = OGROpen(QFile::encodeName(uri).constData(), TRUE, &ogrDriver); CPLPopErrorHandler(); if(ogrDataSource == NULL) { // try to open read-only - ogrDataSource = OGRSFDriverRegistrar::Open(QFile::encodeName(uri).constData(), FALSE, &ogrDriver); + ogrDataSource = OGROpen(QFile::encodeName(uri).constData(), FALSE, &ogrDriver); //TODO Need to set a flag or something to indicate that the layer //TODO is in read-only mode, otherwise edit ops will fail @@ -99,25 +97,29 @@ QgsOgrProvider::QgsOgrProvider(QString const & uri) if (ogrDataSource != NULL) { QgsDebugMsg("Data source is valid"); - QgsDebugMsg("OGR Driver was " + QString(ogrDriver->GetName())); + QgsDebugMsg("OGR Driver was " + QString(OGR_Dr_GetName(ogrDriver))); valid = true; - ogrDriverName = ogrDriver->GetName(); + ogrDriverName = OGR_Dr_GetName(ogrDriver); - ogrLayer = ogrDataSource->GetLayer(0); + ogrLayer = OGR_DS_GetLayer(ogrDataSource,0); // get the extent_ (envelope) of the layer QgsDebugMsg("Starting get extent\n"); - extent_ = new OGREnvelope(); - ogrLayer->GetExtent(extent_); + // TODO: This can be expensive, do we really need it! + + extent_ = calloc(sizeof(OGREnvelope),1); + OGR_L_GetExtent(ogrLayer,(OGREnvelope *) extent_, TRUE ); QgsDebugMsg("Finished get extent\n"); // getting the total number of features in the layer - numberFeatures = ogrLayer->GetFeatureCount(); + // TODO: This can be expensive, do we really need it! + numberFeatures = OGR_L_GetFeatureCount(ogrLayer, TRUE); + // check the validity of the layer QgsDebugMsg("checking validity\n"); @@ -141,12 +143,16 @@ QgsOgrProvider::QgsOgrProvider(QString const & uri) QgsOgrProvider::~QgsOgrProvider() { - OGRDataSource::DestroyDataSource(ogrDataSource); + OGR_DS_Destroy(ogrDataSource); ogrDataSource = 0; - delete extent_; + free(extent_); extent_ = 0; delete geometryFactory; - delete mSelectionRectangle; + if( mSelectionRectangle ) + { + OGR_G_DestroyGeometry( mSelectionRectangle ); + mSelectionRectangle = 0; + } } void QgsOgrProvider::setEncoding(const QString& e) @@ -159,32 +165,33 @@ void QgsOgrProvider::loadFields() { //the attribute fields need to be read again when the encoding changes mAttributeFields.clear(); - OGRFeatureDefn* fdef = ogrLayer->GetLayerDefn(); + OGRFeatureDefnH fdef = OGR_L_GetLayerDefn(ogrLayer); if(fdef) { - geomType = fdef->GetGeomType(); + geomType = OGR_FD_GetGeomType(fdef); //Some ogr drivers (e.g. GML) are not able to determine the geometry type of a layer like this. //In such cases, we examine the first feature if(geomType == wkbUnknown) { - ogrLayer->ResetReading(); - OGRFeature* firstFeature = ogrLayer->GetNextFeature(); + OGR_L_ResetReading(ogrLayer); + OGRFeatureH firstFeature = OGR_L_GetNextFeature(ogrLayer); if(firstFeature) { - OGRGeometry* firstGeometry = firstFeature->GetGeometryRef(); + OGRGeometryH firstGeometry = OGR_F_GetGeometryRef(firstFeature); if(firstGeometry) - { - geomType = firstGeometry->getGeometryType(); - } + { + geomType = OGR_G_GetGeometryType(firstGeometry); + } + OGR_F_Destroy( firstFeature ); } - ogrLayer->ResetReading(); + OGR_L_ResetReading(ogrLayer); } - for(int i=0;iGetFieldCount();++i) + for(int i=0;iGetFieldDefn(i); - OGRFieldType ogrType = fldDef->GetType(); + OGRFieldDefnH fldDef = OGR_FD_GetFieldDefn(fdef,i); + OGRFieldType ogrType = OGR_Fld_GetType(fldDef); QVariant::Type varType; switch (ogrType) { @@ -192,15 +199,18 @@ void QgsOgrProvider::loadFields() case OFTReal: varType = QVariant::Double; break; // unsupported in OGR 1.3 //case OFTDateTime: varType = QVariant::DateTime; break; +#if GDAL_VERSION_NUM >= 1400 case OFTString: varType = QVariant::String; break; +#endif default: varType = QVariant::String; // other unsupported, leave it as a string } - mAttributeFields.insert(i, QgsField( - mEncoding->toUnicode(fldDef->GetNameRef()), varType, - mEncoding->toUnicode(fldDef->GetFieldTypeName(ogrType)), - fldDef->GetWidth(), - fldDef->GetPrecision() )); + mAttributeFields.insert( + i, QgsField( + mEncoding->toUnicode(OGR_Fld_GetNameRef(fldDef)), varType, + mEncoding->toUnicode(OGR_GetFieldTypeName(ogrType)), + OGR_Fld_GetWidth(fldDef), + OGR_Fld_GetPrecision(fldDef) )); } } } @@ -219,22 +229,22 @@ bool QgsOgrProvider::getFeatureAtId(int featureId, bool fetchGeometry, QgsAttributeList fetchAttributes) { - OGRFeature *fet = ogrLayer->GetFeature(featureId); + OGRFeatureH fet = OGR_L_GetFeature(ogrLayer,featureId); if (fet == NULL) return false; - feature.setFeatureId(fet->GetFID()); + feature.setFeatureId(OGR_F_GetFID(fet)); /* fetch geometry */ if (fetchGeometry) { - OGRGeometry *geom = fet->GetGeometryRef(); + OGRGeometryH geom = OGR_F_GetGeometryRef(fet); // get the wkb representation - unsigned char *wkb = new unsigned char[geom->WkbSize()]; - geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb); + unsigned char *wkb = new unsigned char[OGR_G_WkbSize(geom)]; + OGR_G_ExportToWkb(geom,(OGRwkbByteOrder) QgsApplication::endian(), wkb); - feature.setGeometryAndOwnership(wkb, geom->WkbSize()); + feature.setGeometryAndOwnership(wkb, OGR_G_WkbSize(geom)); } /* fetch attributes */ @@ -254,47 +264,48 @@ bool QgsOgrProvider::getNextFeature(QgsFeature& feature) return false; } - OGRFeature *fet; + OGRFeatureH fet; QgsRect selectionRect; - while ((fet = ogrLayer->GetNextFeature()) != NULL) + while ((fet = OGR_L_GetNextFeature(ogrLayer)) != NULL) { // skip features without geometry - if (fet->GetGeometryRef() == NULL && !mFetchFeaturesWithoutGeom) + if (OGR_F_GetGeometryRef(fet) == NULL && !mFetchFeaturesWithoutGeom) { - delete fet; + OGR_F_Destroy( fet ); continue; } - OGRFeatureDefn * featureDefinition = fet->GetDefnRef(); - QString featureTypeName = featureDefinition ? QString(featureDefinition->GetName()) : QString(""); - feature.setFeatureId(fet->GetFID()); + OGRFeatureDefnH featureDefinition = OGR_F_GetDefnRef(fet); + QString featureTypeName = featureDefinition ? QString(OGR_FD_GetName(featureDefinition)) : QString(""); + feature.setFeatureId(OGR_F_GetFID(fet)); feature.setTypeName(featureTypeName); /* fetch geometry */ if (mFetchGeom) { - OGRGeometry *geom = fet->GetGeometryRef(); + OGRGeometryH geom = OGR_F_GetGeometryRef(fet); // get the wkb representation - unsigned char *wkb = new unsigned char[geom->WkbSize()]; - geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb); + unsigned char *wkb = new unsigned char[OGR_G_WkbSize(geom)]; + OGR_G_ExportToWkb(geom,(OGRwkbByteOrder) QgsApplication::endian(), wkb); - feature.setGeometryAndOwnership(wkb, geom->WkbSize()); + feature.setGeometryAndOwnership(wkb, OGR_G_WkbSize(geom)); if (mUseIntersect) { //precise test for intersection with search rectangle //first make QgsRect from OGRPolygon - OGREnvelope env; + OGREnvelope env; + memset( &env, 0, sizeof(env) ); if(mSelectionRectangle) - mSelectionRectangle->getEnvelope(&env); - if(env.IsInit()) //if envelope is invalid, skip the precise intersection test + OGR_G_GetEnvelope(mSelectionRectangle,&env); + if(env.MinX != 0 || env.MinY != 0 || env.MaxX != 0 || env.MaxY != 0 ) //if envelope is invalid, skip the precise intersection test { selectionRect.set(env.MinX, env.MinY, env.MaxX, env.MaxY); if(!feature.geometry()->intersects(selectionRect)) { - delete fet; + OGR_F_Destroy( fet ); continue; } } @@ -315,14 +326,14 @@ bool QgsOgrProvider::getNextFeature(QgsFeature& feature) if (fet) { - delete fet; + OGR_F_Destroy( fet ); return true; } else { QgsDebugMsg("Feature is null"); // probably should reset reading here - ogrLayer->ResetReading(); + OGR_L_ResetReading(ogrLayer); return false; } } @@ -337,12 +348,11 @@ void QgsOgrProvider::select(QgsAttributeList fetchAttributes, QgsRect rect, bool // spatial query to select features if(rect.isEmpty()) { - ogrLayer->SetSpatialFilter(0); + OGR_L_SetSpatialFilter(ogrLayer,0); } else { - OGRGeometry *filter = 0; - filter = OGRGeometryFactory::createGeometry(wkbPolygon); + OGRGeometryH filter = 0; QString wktExtent = QString("POLYGON ((%1))").arg(rect.asPolygon()); const char *wktText = (const char *)wktExtent; @@ -350,56 +360,49 @@ void QgsOgrProvider::select(QgsAttributeList fetchAttributes, QgsRect rect, bool { // store the selection rectangle for use in filtering features during // an identify and display attributes - delete mSelectionRectangle; - mSelectionRectangle = new OGRPolygon(); - mSelectionRectangle->importFromWkt((char **)&wktText); + if( mSelectionRectangle ) + OGR_G_DestroyGeometry( mSelectionRectangle ); + + OGR_G_CreateFromWkt( (char **)&wktText, + NULL, &mSelectionRectangle); } - - // reset the extent for the ogr filter - wktExtent = QString("POLYGON ((%1))").arg(rect.asPolygon()); - wktText = (const char *)wktExtent; - - OGRErr result = ((OGRPolygon *) filter)->importFromWkt((char **)&wktText); - //TODO - detect an error in setting the filter and figure out what to - //TODO about it. If setting the filter fails, all records will be returned - if (result == OGRERR_NONE) - { - QgsDebugMsg("Setting spatial filter using " + wktExtent); - ogrLayer->SetSpatialFilter(filter); - //ogrLayer->SetSpatialFilterRect(rect->xMin(), rect->yMin(), rect->xMax(), rect->yMax()); - } - else - { - QgsDebugMsg("Setting spatial filter failed!"); - } - OGRGeometryFactory::destroyGeometry(filter); + + wktText = (const char *) wktExtent; + OGR_G_CreateFromWkt( (char **)&wktText, NULL, &filter ); + QgsDebugMsg("Setting spatial filter using " + wktExtent); + OGR_L_SetSpatialFilter( ogrLayer, filter ); + OGR_G_DestroyGeometry( filter ); } } -unsigned char * QgsOgrProvider::getGeometryPointer(OGRFeature *fet) +unsigned char * QgsOgrProvider::getGeometryPointer(OGRFeatureH fet) { - OGRGeometry *geom = fet->GetGeometryRef(); + OGRGeometryH geom = OGR_F_GetGeometryRef(fet); unsigned char *gPtr=0; + + if( geom == NULL ) + return NULL; + // get the wkb representation - gPtr = new unsigned char[geom->WkbSize()]; + gPtr = new unsigned char[OGR_G_WkbSize(geom)]; - geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), gPtr); + OGR_G_ExportToWkb(geom,(OGRwkbByteOrder) QgsApplication::endian(), gPtr); return gPtr; - } QgsRect QgsOgrProvider::extent() { - mExtentRect.set(extent_->MinX, extent_->MinY, extent_->MaxX, extent_->MaxY); + OGREnvelope *ext = (OGREnvelope *) extent_; + mExtentRect.set(ext->MinX, ext->MinY, ext->MaxX, ext->MaxY); return mExtentRect; } size_t QgsOgrProvider::layerCount() const { - return ogrDataSource->GetLayerCount(); + return OGR_DS_GetLayerCount(ogrDataSource); } // QgsOgrProvider::layerCount() @@ -427,9 +430,9 @@ uint QgsOgrProvider::fieldCount() const return mAttributeFields.size(); } -void QgsOgrProvider::getFeatureAttribute(OGRFeature * ogrFet, QgsFeature & f, int attindex) +void QgsOgrProvider::getFeatureAttribute(OGRFeatureH ogrFet, QgsFeature & f, int attindex) { - OGRFieldDefn *fldDef = ogrFet->GetFieldDefnRef(attindex); + OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef(ogrFet, attindex); if ( ! fldDef ) { @@ -438,7 +441,7 @@ void QgsOgrProvider::getFeatureAttribute(OGRFeature * ogrFet, QgsFeature & f, in } //QString fld = mEncoding->toUnicode(fldDef->GetNameRef()); - QByteArray cstr(ogrFet->GetFieldAsString(attindex)); + QByteArray cstr(OGR_F_GetFieldAsString(ogrFet,attindex)); QString str = mEncoding->toUnicode(cstr); QVariant value; @@ -462,7 +465,7 @@ const QgsFieldMap & QgsOgrProvider::fields() const void QgsOgrProvider::reset() { - ogrLayer->ResetReading(); + OGR_L_ResetReading(ogrLayer); } @@ -477,151 +480,23 @@ bool QgsOgrProvider::isValid() bool QgsOgrProvider::addFeature(QgsFeature& f) { bool returnValue = true; - OGRFeatureDefn* fdef=ogrLayer->GetLayerDefn(); - OGRFeature* feature=new OGRFeature(fdef); + OGRFeatureDefnH fdef=OGR_L_GetLayerDefn(ogrLayer); + OGRFeatureH feature= OGR_F_Create(fdef); QGis::WKBTYPE ftype = f.geometry()->wkbType(); unsigned char* wkb = f.geometry()->wkbBuffer(); OGRErr err; - - switch(ftype) - { - case QGis::WKBPoint: - { - OGRPoint* p=new OGRPoint(); - p->importFromWkb(wkb,1+sizeof(int)+2*sizeof(double)); - err = feature->SetGeometry(p); - if(err != OGRERR_NONE) - { - delete p; - return false; - } - break; - } - case QGis::WKBLineString: - { - OGRLineString* l=new OGRLineString(); - int length; - memcpy(&length,wkb+1+sizeof(int),sizeof(int)); - l->importFromWkb(wkb,1+2*sizeof(int)+2*length*sizeof(double)); - err = feature->SetGeometry(l); - if(err != OGRERR_NONE) - { - delete l; - return false; - } - break; - } - case QGis::WKBPolygon: - { - OGRPolygon* pol=new OGRPolygon(); - int numrings; - int totalnumpoints=0; - int numpoints;//number of points in one ring - unsigned char* ptr=wkb+1+sizeof(int); - memcpy(&numrings,ptr,sizeof(int)); - ptr+=sizeof(int); - for(int i=0;iimportFromWkb(wkb,1+2*sizeof(int)+numrings*sizeof(int)+totalnumpoints*2*sizeof(double)); - err = feature->SetGeometry(pol); - if(err != OGRERR_NONE) - { - delete pol; - return false; - } - break; - } - case QGis::WKBMultiPoint: - { - OGRMultiPoint* multip= new OGRMultiPoint(); - int count; - //determine how many points - memcpy(&count,wkb+1+sizeof(int),sizeof(int)); - multip->importFromWkb(wkb,1+2*sizeof(int)+count*2*sizeof(double)); - err = feature->SetGeometry(multip); - if(err != OGRERR_NONE) - { - delete multip; - return false; - } - break; - } - case QGis::WKBMultiLineString: - { - OGRMultiLineString* multil=new OGRMultiLineString(); - int numlines; - memcpy(&numlines,wkb+1+sizeof(int),sizeof(int)); - int totalpoints=0; - int numpoints;//number of point in one line - unsigned char* ptr=wkb+9; - for(int i=0;iimportFromWkb(wkb,size); - err = feature->SetGeometry(multil); - if(err != OGRERR_NONE) - { - delete multil; - return false; - } - break; - } - case QGis::WKBMultiPolygon: - { - OGRMultiPolygon* multipol=new OGRMultiPolygon(); - int numpolys; - memcpy(&numpolys,wkb+1+sizeof(int),sizeof(int)); - int numrings;//number of rings in one polygon - int totalrings=0; - int totalpoints=0; - int numpoints;//number of points in one ring - unsigned char* ptr=wkb+9; - for(int i=0;iimportFromWkb(wkb,size); - err = feature->SetGeometry(multipol); - if(err != OGRERR_NONE) - { - delete multipol; - return false; - } - break; - } - default: + if( f.geometry()->wkbSize() > 0 ) + { + OGRGeometryH geom = NULL; + + if( OGR_G_CreateFromWkb( wkb, NULL, &geom, f.geometry()->wkbSize() ) + != OGRERR_NONE ) { - QgsLogger::debug("Unknown feature type of: ", (int)(ftype), 1, - __FILE__, __FUNCTION__, __LINE__); - return false; - break; + return false; } + + OGR_F_SetGeometryDirectly( feature, geom ); } QgsAttributeMap attrs = f.attributeMap(); @@ -632,41 +507,45 @@ bool QgsOgrProvider::addFeature(QgsFeature& f) int targetAttributeId = it.key(); // don't try to set field from attribute map if it's not present in layer - if (targetAttributeId >= fdef->GetFieldCount()) + if (targetAttributeId >= OGR_FD_GetFieldCount(fdef)) continue; //if(!s.isEmpty()) // continue; - if(fdef->GetFieldDefn(targetAttributeId)->GetType()==OFTInteger) + OGRFieldDefnH fldDef = OGR_FD_GetFieldDefn( fdef, targetAttributeId ); + + switch( OGR_Fld_GetType(fldDef) ) { - feature->SetField(targetAttributeId,it->toInt()); - } - else if(fdef->GetFieldDefn(targetAttributeId)->GetType()==OFTReal) - { - feature->SetField(targetAttributeId,it->toDouble()); - } - else if(fdef->GetFieldDefn(targetAttributeId)->GetType()==OFTString) - { - QgsDebugMsg( QString("Writing string attribute %1 with %2, encoding %3") - .arg( targetAttributeId ) - .arg( it->toString() ) - .arg( mEncoding->name().data() ) ); - feature->SetField(targetAttributeId,mEncoding->fromUnicode(it->toString()).constData()); - } - else - { - QgsLogger::warning("QgsOgrProvider::addFeature, no type found"); + case OFTInteger: + OGR_F_SetFieldInteger(feature,targetAttributeId,it->toInt()); + break; + + case OFTReal: + OGR_F_SetFieldDouble(feature,targetAttributeId,it->toDouble()); + break; + + case OFTString: + QgsDebugMsg( QString("Writing string attribute %1 with %2, encoding %3") + .arg( targetAttributeId ) + .arg( it->toString() ) + .arg( mEncoding->name().data() ) ); + OGR_F_SetFieldString(feature,targetAttributeId,mEncoding->fromUnicode(it->toString()).constData()); + break; + + default: + QgsLogger::warning("QgsOgrProvider::addFeature, no type found"); + break; } } - if(ogrLayer->CreateFeature(feature)!=OGRERR_NONE) + if( OGR_L_CreateFeature(ogrLayer,feature) != OGRERR_NONE) { QgsLogger::warning("Writing of the feature failed"); returnValue = false; } ++numberFeatures; - delete feature; + OGR_F_Destroy( feature ); return returnValue; } @@ -683,8 +562,8 @@ bool QgsOgrProvider::addFeatures(QgsFeatureList & flist) } // flush features - ogrLayer->SyncToDisk(); - numberFeatures = ogrLayer->GetFeatureCount(); //new feature count + OGR_L_SyncToDisk(ogrLayer); + numberFeatures = OGR_L_GetFeatureCount(ogrLayer,TRUE); //new feature count return returnvalue; } @@ -694,39 +573,36 @@ bool QgsOgrProvider::addAttributes(const QgsNewAttributesMap & attributes) for(QgsNewAttributesMap::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) { + OGRFieldDefnH fielddefn = + OGR_Fld_Create(mEncoding->fromUnicode(iter.key()).data(),OFTInteger); + if(*iter=="OFTInteger") { - OGRFieldDefn fielddefn(mEncoding->fromUnicode(iter.key()).data(),OFTInteger); - if(ogrLayer->CreateField(&fielddefn)!=OGRERR_NONE) - { - QgsLogger::warning("QgsOgrProvider.cpp: writing of OFTInteger field failed"); - returnvalue=false; - } + OGR_Fld_SetType( fielddefn, OFTInteger ); } - else if(*iter=="OFTReal") + else if(*iter=="OFTReal") { - OGRFieldDefn fielddefn(mEncoding->fromUnicode(iter.key()).data(),OFTReal); - if(ogrLayer->CreateField(&fielddefn)!=OGRERR_NONE) - { - QgsLogger::warning("QgsOgrProvider.cpp: writing of OFTReal field failed"); - returnvalue=false; - } + OGR_Fld_SetType( fielddefn, OFTReal ); } - else if(*iter=="OFTString") + else if(*iter=="OFTString") { - OGRFieldDefn fielddefn(mEncoding->fromUnicode(iter.key()).data(),OFTString); - if(ogrLayer->CreateField(&fielddefn)!=OGRERR_NONE) - { - QgsLogger::warning("QgsOgrProvider.cpp: writing of OFTString field failed"); - returnvalue=false; - } + OGR_Fld_SetType( fielddefn, OFTString ); } else { QgsLogger::warning("QgsOgrProvider::addAttributes, type not found"); returnvalue=false; + continue; } + + if( OGR_L_CreateField(ogrLayer,fielddefn,TRUE) != OGRERR_NONE) + { + QgsLogger::warning("QgsOgrProvider.cpp: writing of OFTInteger field failed"); + returnvalue=false; + } + OGR_Fld_Destroy( fielddefn ); } + return returnvalue; } @@ -736,7 +612,7 @@ bool QgsOgrProvider::changeAttributeValues(const QgsChangedAttributesMap & attr_ { long fid = (long) it.key(); - OGRFeature *of = ogrLayer->GetFeature ( fid ); + OGRFeatureH of = OGR_L_GetFeature( ogrLayer, fid ); if ( !of ) { @@ -749,25 +625,25 @@ bool QgsOgrProvider::changeAttributeValues(const QgsChangedAttributesMap & attr_ for( QgsAttributeMap::const_iterator it2 = attr.begin(); it2 != attr.end(); ++it2 ) { int f = it2.key(); - - OGRFieldDefn *fd = of->GetFieldDefnRef ( f ); + + OGRFieldDefnH fd = OGR_F_GetFieldDefnRef( of, f ); if (fd == NULL) { QgsLogger::warning("QgsOgrProvider::changeAttributeValues, Field " + QString::number(f) + " doesn't exist"); continue; } - OGRFieldType type = fd->GetType(); + OGRFieldType type = OGR_Fld_GetType( fd ); switch ( type ) { case OFTInteger: - of->SetField ( f, it2->toInt() ); + OGR_F_SetFieldInteger ( of, f, it2->toInt() ); break; case OFTReal: - of->SetField ( f, it2->toDouble() ); + OGR_F_SetFieldDouble ( of, f, it2->toDouble() ); break; case OFTString: - of->SetField ( f, mEncoding->fromUnicode(it2->toString()).constData() ); + OGR_F_SetFieldString ( of, f, mEncoding->fromUnicode(it2->toString()).constData() ); break; default: QgsLogger::warning("QgsOgrProvider::changeAttributeValues, Unknown field type, cannot change attribute"); @@ -776,22 +652,22 @@ bool QgsOgrProvider::changeAttributeValues(const QgsChangedAttributesMap & attr_ } - ogrLayer->SetFeature ( of ); + OGR_L_SetFeature( ogrLayer, of ); } - ogrLayer->SyncToDisk(); + OGR_L_SyncToDisk( ogrLayer ); return true; } bool QgsOgrProvider::changeGeometryValues(QgsGeometryMap & geometry_map) { - OGRFeature* theOGRFeature = 0; - OGRGeometry* theNewGeometry = 0; + OGRFeatureH theOGRFeature = 0; + OGRGeometryH theNewGeometry = 0; for (QgsGeometryMap::iterator it = geometry_map.begin(); it != geometry_map.end(); ++it) { - theOGRFeature = ogrLayer->GetFeature(it.key()); + theOGRFeature = OGR_L_GetFeature(ogrLayer,it.key()); if(!theOGRFeature) { QgsLogger::warning("QgsOgrProvider::changeGeometryValues, cannot find feature"); @@ -799,13 +675,13 @@ bool QgsOgrProvider::changeGeometryValues(QgsGeometryMap & geometry_map) } //create an OGRGeometry - if (OGRGeometryFactory::createFromWkb(it->wkbBuffer(), - ogrLayer->GetSpatialRef(), - &theNewGeometry, - it->wkbSize()) != OGRERR_NONE) + if (OGR_G_CreateFromWkb(it->wkbBuffer(), + OGR_L_GetSpatialRef(ogrLayer), + &theNewGeometry, + it->wkbSize()) != OGRERR_NONE) { QgsLogger::warning("QgsOgrProvider::changeGeometryValues, error while creating new OGRGeometry"); - delete theNewGeometry; + OGR_G_DestroyGeometry( theNewGeometry ); theNewGeometry = 0; continue; } @@ -817,17 +693,18 @@ bool QgsOgrProvider::changeGeometryValues(QgsGeometryMap & geometry_map) } //set the new geometry - if(theOGRFeature->SetGeometryDirectly(theNewGeometry) != OGRERR_NONE) + if(OGR_F_SetGeometryDirectly(theOGRFeature, theNewGeometry) != OGRERR_NONE) { QgsLogger::warning("QgsOgrProvider::changeGeometryValues, error while replacing geometry"); - delete theNewGeometry; + OGR_G_DestroyGeometry( theNewGeometry ); theNewGeometry = 0; continue; } - ogrLayer->SetFeature(theOGRFeature); + OGR_L_SetFeature(ogrLayer,theOGRFeature); + OGR_F_Destroy( theOGRFeature); } - ogrLayer->SyncToDisk(); + OGR_L_SyncToDisk(ogrLayer); return true; } @@ -836,7 +713,7 @@ bool QgsOgrProvider::createSpatialIndex() QString filename=dataSourceUri().section('/',-1,-1);//find out the filename from the uri QString layername=filename.section('.',0,0); QString sql="CREATE SPATIAL INDEX ON "+layername; - ogrDataSource->ExecuteSQL (sql.ascii(), ogrLayer->GetSpatialFilter(),""); + OGR_DS_ExecuteSQL (ogrDataSource,sql.ascii(), OGR_L_GetSpatialFilter(ogrLayer),""); //find out, if the .qix file is there QString indexname = dataSourceUri(); indexname.truncate(dataSourceUri().length()-filename.length()); @@ -863,18 +740,18 @@ bool QgsOgrProvider::deleteFeatures(const QgsFeatureIds & id) } } - ogrLayer->SyncToDisk(); + OGR_L_SyncToDisk(ogrLayer); QString filename=dataSourceUri().section('/',-1,-1);//find out the filename from the uri QString layername=filename.section('.',0,0); QString sql="REPACK " + layername; - ogrDataSource->ExecuteSQL(sql.toLocal8Bit().data(), NULL, NULL); - numberFeatures = ogrLayer->GetFeatureCount(); //new feature count + OGR_DS_ExecuteSQL(ogrDataSource,sql.toLocal8Bit().data(), NULL, NULL); + numberFeatures = OGR_L_GetFeatureCount(ogrLayer,TRUE); //new feature count return returnvalue; } bool QgsOgrProvider::deleteFeature(int id) { - OGRErr res = ogrLayer->DeleteFeature(id); + OGRErr res = OGR_L_DeleteFeature(ogrLayer,id); return (res == OGRERR_NONE); } @@ -892,7 +769,7 @@ int QgsOgrProvider::capabilities() const // here. This is because older versions of OGR don't always have all // the #defines we want to test for here. - if (ogrLayer->TestCapability("RandomRead")) + if (OGR_L_TestCapability(ogrLayer,"RandomRead")) // TRUE if the GetFeature() method works *efficiently* for this layer. // TODO: Perhaps influence if QGIS caches into memory // (vs read from disk every time) based on this setting. @@ -905,19 +782,19 @@ int QgsOgrProvider::capabilities() const } ability |= QgsVectorDataProvider::SelectGeometryAtId; - if (ogrLayer->TestCapability("SequentialWrite")) + if (OGR_L_TestCapability(ogrLayer,"SequentialWrite")) // TRUE if the CreateFeature() method works for this layer. { ability |= QgsVectorDataProvider::AddFeatures; } - if (ogrLayer->TestCapability("DeleteFeature")) + if (OGR_L_TestCapability(ogrLayer,"DeleteFeature")) // TRUE if this layer can delete its features { ability |= DeleteFeatures; } - if (ogrLayer->TestCapability("RandomWrite")) + if (OGR_L_TestCapability(ogrLayer,"RandomWrite")) // TRUE if the SetFeature() method is operational on this layer. { // TODO According to http://shapelib.maptools.org/ (Shapefile C Library V1.2) @@ -929,7 +806,7 @@ int QgsOgrProvider::capabilities() const ability |= QgsVectorDataProvider::ChangeGeometries; } - if (ogrLayer->TestCapability("FastSpatialFilter")) + if (OGR_L_TestCapability(ogrLayer,"FastSpatialFilter")) // TRUE if this layer implements spatial filtering efficiently. // Layers that effectively read all features, and test them with the // OGRFeature intersection methods should return FALSE. @@ -939,7 +816,7 @@ int QgsOgrProvider::capabilities() const // TODO: Perhaps use as a clue by QGIS whether it should build and maintain it's own spatial index for features in this layer. } - if (ogrLayer->TestCapability("FastFeatureCount")) + if (OGR_L_TestCapability(ogrLayer,"FastFeatureCount")) // TRUE if this layer can return a feature count // (via OGRLayer::GetFeatureCount()) efficiently ... ie. without counting // the features. In some cases this will return TRUE until a spatial @@ -948,7 +825,7 @@ int QgsOgrProvider::capabilities() const // TODO: Perhaps use as a clue by QGIS whether it should spawn a thread to count features. } - if (ogrLayer->TestCapability("FastGetExtent")) + if (OGR_L_TestCapability(ogrLayer,"FastGetExtent")) // TRUE if this layer can return its data extent // (via OGRLayer::GetExtent()) efficiently ... ie. without scanning // all the features. In some cases this will return TRUE until a @@ -957,7 +834,7 @@ int QgsOgrProvider::capabilities() const // TODO: Perhaps use as a clue by QGIS whether it should spawn a thread to calculate extent. } - if (ogrLayer->TestCapability("FastSetNextByIndex")) + if (OGR_L_TestCapability(ogrLayer,"FastSetNextByIndex")) // TRUE if this layer can perform the SetNextByIndex() call efficiently. { // No use required for this QGIS release. @@ -1049,18 +926,7 @@ QGISEXTERN QString fileVectorFilters() // first get the GDAL driver manager - OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); - - if (!driverRegistrar) - { - QgsLogger::warning("OGR Driver Manager, unable to get OGRDriverManager"); - return ""; // XXX good place to throw exception if we - } // XXX decide to do exceptions - - // then iterate through all of the supported drivers, adding the - // corresponding file filter - - OGRSFDriver *driver; // current driver + OGRSFDriverH driver; // current driver QString driverName; // current driver name @@ -1070,11 +936,11 @@ QGISEXTERN QString fileVectorFilters() // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension. - QgsDebugMsg( QString("Driver count: %1").arg( driverRegistrar->GetDriverCount() ) ); + QgsDebugMsg( QString("Driver count: %1").arg( OGRGetDriverCount() ) ); - for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) + for (int i = 0; i < OGRGetDriverCount(); ++i) { - driver = driverRegistrar->GetDriver(i); + driver = OGRGetDriver(i); Q_CHECK_PTR(driver); @@ -1084,7 +950,7 @@ QGISEXTERN QString fileVectorFilters() continue; } - driverName = driver->GetName(); + driverName = OGR_Dr_GetName(driver); if (driverName.startsWith("ESRI")) @@ -1253,30 +1119,30 @@ QGISEXTERN bool createEmptyDataSource(const QString& uri, QGis::WKBTYPE vectortype, const std::list >& attributes) { - OGRSFDriver* driver; + OGRSFDriverH driver; OGRRegisterAll(); - driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(format); + driver = OGRGetDriverByName(format); if(driver == NULL) { return false; } - OGRDataSource* dataSource; - dataSource = driver->CreateDataSource(QFile::encodeName(uri).constData(), NULL); + OGRDataSourceH dataSource; + dataSource = OGR_Dr_CreateDataSource(driver,QFile::encodeName(uri).constData(), NULL); if(dataSource == NULL) { return false; } //consider spatial reference system - OGRSpatialReference* reference = NULL; + OGRSpatialReferenceH reference = NULL; QgsSpatialRefSys mySpatialRefSys; mySpatialRefSys.validate(); QString myWKT = mySpatialRefSys.toWkt(); if( !myWKT.isNull() && myWKT.length() != 0 ) { - reference = new OGRSpatialReference(myWKT.toLocal8Bit().data()); + reference = OSRNewSpatialReference(myWKT.toLocal8Bit().data()); } // Map the qgis geometry type to the OGR geometry type @@ -1310,8 +1176,8 @@ QGISEXTERN bool createEmptyDataSource(const QString& uri, } } - OGRLayer* layer; - layer = dataSource->CreateLayer(QFile::encodeName(QFileInfo(uri).baseName()).constData(), reference, OGRvectortype, NULL); + OGRLayerH layer; + layer = OGR_DS_CreateLayer(dataSource,QFile::encodeName(QFileInfo(uri).baseName()).constData(), reference, OGRvectortype, NULL); if(layer == NULL) { return false; @@ -1325,39 +1191,39 @@ QGISEXTERN bool createEmptyDataSource(const QString& uri, { if(it->second == "Real") { - OGRFieldDefn field(codec->fromUnicode(it->first).data(), OFTReal); - field.SetPrecision(3); - field.SetWidth(32); - if(layer->CreateField(&field) != OGRERR_NONE) + OGRFieldDefnH field = OGR_Fld_Create(codec->fromUnicode(it->first).data(), OFTReal); + OGR_Fld_SetPrecision(field,3); + OGR_Fld_SetWidth(field,32); + if( OGR_L_CreateField(layer,field,TRUE) != OGRERR_NONE) { QgsLogger::warning("creation of OFTReal field failed"); } } else if(it->second == "Integer") { - OGRFieldDefn field(codec->fromUnicode(it->first).data(), OFTInteger); - if(layer->CreateField(&field) != OGRERR_NONE) + OGRFieldDefnH field = OGR_Fld_Create(codec->fromUnicode(it->first).data(), OFTInteger); + if(OGR_L_CreateField(layer,field,TRUE) != OGRERR_NONE) { QgsLogger::warning("creation of OFTInteger field failed"); } } else if(it->second == "String") { - OGRFieldDefn field(codec->fromUnicode(it->first).data(), OFTString); - if(layer->CreateField(&field) != OGRERR_NONE) + OGRFieldDefnH field = OGR_Fld_Create(codec->fromUnicode(it->first).data(), OFTString); + if(OGR_L_CreateField(layer,field,TRUE) != OGRERR_NONE) { QgsLogger::warning("creation of OFTString field failed"); } } } - OGRDataSource::DestroyDataSource(dataSource); + OGR_DS_Destroy(dataSource); QgsDebugMsg( QString("GDAL Version number %1").arg( GDAL_VERSION_NUM ) ); #if GDAL_VERSION_NUM >= 1310 if(reference) { - reference->Release(); + OSRRelease( reference ); } #endif //GDAL_VERSION_NUM return true; @@ -1371,28 +1237,19 @@ QgsSpatialRefSys QgsOgrProvider::getSRS() QgsSpatialRefSys srs; - OGRSpatialReference * mySpatialRefSys = ogrLayer->GetSpatialRef(); + OGRSpatialReferenceH mySpatialRefSys = OGR_L_GetSpatialRef(ogrLayer); if (mySpatialRefSys == NULL) { QgsDebugMsg("no spatial reference found"); } else { - // if appropriate, morph the projection from ESRI form - QString fileName = ogrDataSource->GetName(); - QgsDebugMsg("Data source file name is : " + fileName); - if(fileName.contains(".shp")) - { - QgsDebugMsg("Morphing " + fileName + " WKT from ESRI"); - // morph it - mySpatialRefSys->morphFromESRI(); - } // get the proj4 text char * ppszProj4; - mySpatialRefSys->exportToProj4 ( &ppszProj4 ); + OSRExportToProj4(mySpatialRefSys, &ppszProj4 ); QgsDebugMsg(ppszProj4); char *pszWKT = NULL; - mySpatialRefSys->exportToWkt( &pszWKT ); + OSRExportToWkt(mySpatialRefSys, &pszWKT ); QString myWKTString = QString(pszWKT); OGRFree(pszWKT); diff --git a/src/providers/ogr/qgsogrprovider.h b/src/providers/ogr/qgsogrprovider.h index 37262f54f5f..312aa4146a9 100644 --- a/src/providers/ogr/qgsogrprovider.h +++ b/src/providers/ogr/qgsogrprovider.h @@ -30,12 +30,12 @@ email : sherman at mrcc.com class QgsFeature; class QgsField; -class OGRDataSource; -class OGRSFDriver; -class OGRLayer; -class OGRFeature; -class OGREnvelope; -class OGRPolygon; + +typedef void *OGRDataSourceH; +typedef void *OGRSFDriverH; +typedef void *OGRLayerH; +typedef void *OGRFeatureH; +typedef void *OGRGeometryH; /** \class QgsOgrProvider @@ -188,7 +188,7 @@ class QgsOgrProvider : public QgsVectorDataProvider void loadFields(); /**Get an attribute associated with a feature*/ - void getFeatureAttribute(OGRFeature * ogrFet, QgsFeature & f, int attindex); + void getFeatureAttribute(OGRFeatureH ogrFet, QgsFeature & f, int attindex); /** return a provider name @@ -222,19 +222,20 @@ class QgsOgrProvider : public QgsVectorDataProvider private: - unsigned char *getGeometryPointer(OGRFeature * fet); + unsigned char *getGeometryPointer(OGRFeatureH fet); QgsFieldMap mAttributeFields; - OGRDataSource *ogrDataSource; - OGREnvelope *extent_; + OGRDataSourceH ogrDataSource; + void *extent_; + /**This member variable receives the same value as extent_ in the method QgsOgrProvider::extent(). The purpose is to prevent a memory leak*/ QgsRect mExtentRect; - OGRLayer *ogrLayer; + OGRLayerH ogrLayer; // OGR Driver that was actually used to open the layer - OGRSFDriver *ogrDriver; + OGRSFDriverH ogrDriver; // Friendly name of the OGR Driver that was actually used to open the layer QString ogrDriverName; @@ -246,12 +247,11 @@ class QgsOgrProvider : public QgsVectorDataProvider long numberFeatures; //! Selection rectangle - OGRPolygon * mSelectionRectangle; + OGRGeometryH mSelectionRectangle; /**Adds one feature*/ bool addFeature(QgsFeature& f); /**Deletes one feature*/ bool deleteFeature(int id); //! The geometry factory GEOS_GEOM::GeometryFactory *geometryFactory; - }; diff --git a/tests/algorithms/projections/projectioncshandlingtest.h b/tests/algorithms/projections/projectioncshandlingtest.h index fbef321dc7f..d238fe062e6 100644 --- a/tests/algorithms/projections/projectioncshandlingtest.h +++ b/tests/algorithms/projections/projectioncshandlingtest.h @@ -22,7 +22,7 @@ #include //gdal and ogr includes #include -#include +#include #include class ProjectionCsHandlingTest : public CppUnit::TestCase { public: @@ -94,9 +94,10 @@ class ProjectionCsHandlingTest : public CppUnit::TestCase { void testProjImportWkt() { // create a spatial reference system object std::cout << "\n\nCreating and OGRSpatialReference object from WKT" << std::endl; - OGRSpatialReference myInputSpatialRefSys; + OGRSpatialReferenceH myInputSpatialRefSys = OSRNewSpatialReference(NULL); char *pWkt = (char*)wkt.ascii(); - CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE); + CPPUNIT_ASSERT(OSRImportFromWkt(myInputSpatialRefSys,&pWkt)== OGRERR_NONE); + OSRDestroySpatialReference( myInputSpatialRefSys ); } // // Test fetch of proj4 parameters from an OGRSpatialReference object @@ -106,7 +107,7 @@ class ProjectionCsHandlingTest : public CppUnit::TestCase { { std::cout << "\n\nGetting proj4 parameters from OGRSpatialReference object" << std::endl; // set up the spatial ref - OGRSpatialReference myInputSpatialRefSys; + OGRSpatialReferenceH myInputSpatialRefSys; char *pWkt = (char*)wkt.ascii(); CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE); // get the proj4 for the projection