Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
gdal/ogr data items: reenable SetCrs capabilities (fixes #12505)
(cherry picked from commit 78ecddb)
  • Loading branch information
jef-n committed Jun 29, 2015
1 parent 5647056 commit 8b503a1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 77 deletions.
21 changes: 9 additions & 12 deletions src/providers/gdal/qgsgdaldataitems.cpp
Expand Up @@ -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()
Expand All @@ -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;
}
Expand Down
116 changes: 51 additions & 65 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -37,90 +37,76 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem* parent,
{
mToolTip = uri;
setState( Populated ); // children are not expected
}

QgsOgrLayerItem::~QgsOgrLayerItem()
{
}

QgsLayerItem::Capability QgsOgrLayerItem::capabilities()
{
QgsDebugMsg( "mPath = " + mPath );
OGRRegisterAll();
OGRSFDriverH hDriver;
OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver );

if ( !hDataSource )
return NoCapabilities;
if ( hDataSource )
{
QString driverName = OGR_Dr_GetName( hDriver );
OGR_DS_Destroy( hDataSource );

QString driverName = OGR_Dr_GetName( hDriver );
OGR_DS_Destroy( hDataSource );
if ( driverName == "ESRI Shapefile" )
mCapabilities |= SetCrs;

if ( driverName == "ESRI Shapefile" )
return SetCrs;
// It it is impossible to assign a crs to an existing layer
// No OGR_L_SetSpatialRef : http://trac.osgeo.org/gdal/ticket/4032
}
}

return NoCapabilities;
QgsOgrLayerItem::~QgsOgrLayerItem()
{
}

bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
QgsLayerItem::Capability QgsOgrLayerItem::capabilities()
{
QgsDebugMsg( "mPath = " + mPath );
OGRRegisterAll();
OGRSFDriverH hDriver;
OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver );
return mCapabilities & SetCrs ? SetCrs : NoCapabilities;
}

if ( !hDataSource )
bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
{
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();

// 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;
}
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 );

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
Expand Down

0 comments on commit 8b503a1

Please sign in to comment.