From 78ecddb65d6b3aba6b8b6126b1d45f8129756b59 Mon Sep 17 00:00:00 2001 From: "Juergen E. Fischer" Date: Mon, 1 Jun 2015 14:42:27 +0200 Subject: [PATCH] gdal/ogr data items: reenable SetCrs capabilities (fixes #12505) --- src/providers/gdal/qgsgdaldataitems.cpp | 21 ++--- src/providers/ogr/qgsogrdataitems.cpp | 116 +++++++++++------------- 2 files changed, 60 insertions(+), 77 deletions(-) diff --git a/src/providers/gdal/qgsgdaldataitems.cpp b/src/providers/gdal/qgsgdaldataitems.cpp index dc290c832d8..ea0c450f92f 100644 --- a/src/providers/gdal/qgsgdaldataitems.cpp +++ b/src/providers/gdal/qgsgdaldataitems.cpp @@ -38,6 +38,12 @@ QgsGdalLayerItem::QgsGdalLayerItem( QgsDataItem* parent, } else setState( Populated ); + + GDALAllRegister(); + GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update ); + + if ( hDS ) + mCapabilities |= SetCrs; } QgsGdalLayerItem::~QgsGdalLayerItem() @@ -46,32 +52,23 @@ QgsGdalLayerItem::~QgsGdalLayerItem() QgsLayerItem::Capability QgsGdalLayerItem::capabilities() { - // Check if data source can be opened for update - QgsDebugMsg( "mPath = " + mPath ); - GDALAllRegister(); - GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update ); - - if ( !hDS ) - return NoCapabilities; - - return SetCrs; + return mCapabilities & SetCrs ? SetCrs : NoCapabilities; } bool QgsGdalLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) { - QgsDebugMsg( "mPath = " + mPath ); - GDALAllRegister(); GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update ); - if ( !hDS ) return false; QString wkt = crs.toWkt(); if ( GDALSetProjection( hDS, wkt.toLocal8Bit().data() ) != CE_None ) { + GDALClose( hDS ); QgsDebugMsg( "Could not set CRS" ); return false; } + GDALClose( hDS ); return true; } diff --git a/src/providers/ogr/qgsogrdataitems.cpp b/src/providers/ogr/qgsogrdataitems.cpp index 5b0c0250d13..2920ec4bc39 100644 --- a/src/providers/ogr/qgsogrdataitems.cpp +++ b/src/providers/ogr/qgsogrdataitems.cpp @@ -37,6 +37,22 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem* parent, { mToolTip = uri; setState( Populated ); // children are not expected + + OGRRegisterAll(); + OGRSFDriverH hDriver; + OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver ); + + if ( hDataSource ) + { + QString driverName = OGR_Dr_GetName( hDriver ); + OGR_DS_Destroy( hDataSource ); + + if ( driverName == "ESRI Shapefile" ) + mCapabilities |= SetCrs; + + // It it is impossible to assign a crs to an existing layer + // No OGR_L_SetSpatialRef : http://trac.osgeo.org/gdal/ticket/4032 + } } QgsOgrLayerItem::~QgsOgrLayerItem() @@ -45,82 +61,52 @@ QgsOgrLayerItem::~QgsOgrLayerItem() QgsLayerItem::Capability QgsOgrLayerItem::capabilities() { - QgsDebugMsg( "mPath = " + mPath ); - OGRRegisterAll(); - OGRSFDriverH hDriver; - OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver ); - - if ( !hDataSource ) - return NoCapabilities; - - QString driverName = OGR_Dr_GetName( hDriver ); - OGR_DS_Destroy( hDataSource ); - - if ( driverName == "ESRI Shapefile" ) - return SetCrs; - - return NoCapabilities; + return mCapabilities & SetCrs ? SetCrs : NoCapabilities; } bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) { - QgsDebugMsg( "mPath = " + mPath ); - OGRRegisterAll(); - OGRSFDriverH hDriver; - OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver ); - - if ( !hDataSource ) + if ( !( mCapabilities & SetCrs ) ) return false; - QString driverName = OGR_Dr_GetName( hDriver ); - OGR_DS_Destroy( hDataSource ); + QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) ); + QString wkt = crs.toWkt(); - // we are able to assign CRS only to shapefiles :-( - if ( driverName == "ESRI Shapefile" ) + // save ordinary .prj file + OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt.toLocal8Bit().data() ); + OSRMorphToESRI( hSRS ); // this is the important stuff for shapefile .prj + char* pszOutWkt = NULL; + OSRExportToWkt( hSRS, &pszOutWkt ); + QFile prjFile( layerName + ".prj" ); + if ( prjFile.open( QIODevice::WriteOnly ) ) { - QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) ); - QString wkt = crs.toWkt(); + QTextStream prjStream( &prjFile ); + prjStream << pszOutWkt << endl; + prjFile.close(); + } + else + { + QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( layerName ), tr( "OGR" ) ); + return false; + } + OSRDestroySpatialReference( hSRS ); + CPLFree( pszOutWkt ); - // save ordinary .prj file - OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt.toLocal8Bit().data() ); - OSRMorphToESRI( hSRS ); // this is the important stuff for shapefile .prj - char* pszOutWkt = NULL; - OSRExportToWkt( hSRS, &pszOutWkt ); - QFile prjFile( layerName + ".prj" ); - if ( prjFile.open( QIODevice::WriteOnly ) ) - { - QTextStream prjStream( &prjFile ); - prjStream << pszOutWkt << endl; - prjFile.close(); - } - else - { - QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( layerName ), tr( "OGR" ) ); - return false; - } - OSRDestroySpatialReference( hSRS ); - CPLFree( pszOutWkt ); - - // save qgis-specific .qpj file (maybe because of better wkt compatibility?) - QFile qpjFile( layerName + ".qpj" ); - if ( qpjFile.open( QIODevice::WriteOnly ) ) - { - QTextStream qpjStream( &qpjFile ); - qpjStream << wkt.toLocal8Bit().data() << endl; - qpjFile.close(); - } - else - { - QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) ); - return false; - } - - return true; + // save qgis-specific .qpj file (maybe because of better wkt compatibility?) + QFile qpjFile( layerName + ".qpj" ); + if ( qpjFile.open( QIODevice::WriteOnly ) ) + { + QTextStream qpjStream( &qpjFile ); + qpjStream << wkt.toLocal8Bit().data() << endl; + qpjFile.close(); + } + else + { + QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) ); + return false; } - // It it is impossible to assign a crs to an existing layer - // No OGR_L_SetSpatialRef : http://trac.osgeo.org/gdal/ticket/4032 - return false; + return true; } QString QgsOgrLayerItem::layerName() const