Skip to content
Permalink
Browse files

gdal/ogr data items: reenable SetCrs capabilities (fixes #12505)

  • Loading branch information
jef-n committed Jun 1, 2015
1 parent 41ad9ab commit 78ecddb65d6b3aba6b8b6126b1d45f8129756b59
Showing with 60 additions and 77 deletions.
  1. +9 −12 src/providers/gdal/qgsgdaldataitems.cpp
  2. +51 −65 src/providers/ogr/qgsogrdataitems.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;
}
@@ -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

0 comments on commit 78ecddb

Please sign in to comment.
You can’t perform that action at this time.