mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-01 00:46:20 -05:00
gdal/ogr data items: reenable SetCrs capabilities (fixes #12505)
This commit is contained in:
parent
41ad9abb40
commit
78ecddb65d
@ -38,6 +38,12 @@ QgsGdalLayerItem::QgsGdalLayerItem( QgsDataItem* parent,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
setState( Populated );
|
setState( Populated );
|
||||||
|
|
||||||
|
GDALAllRegister();
|
||||||
|
GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update );
|
||||||
|
|
||||||
|
if ( hDS )
|
||||||
|
mCapabilities |= SetCrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
QgsGdalLayerItem::~QgsGdalLayerItem()
|
QgsGdalLayerItem::~QgsGdalLayerItem()
|
||||||
@ -46,32 +52,23 @@ QgsGdalLayerItem::~QgsGdalLayerItem()
|
|||||||
|
|
||||||
QgsLayerItem::Capability QgsGdalLayerItem::capabilities()
|
QgsLayerItem::Capability QgsGdalLayerItem::capabilities()
|
||||||
{
|
{
|
||||||
// Check if data source can be opened for update
|
return mCapabilities & SetCrs ? SetCrs : NoCapabilities;
|
||||||
QgsDebugMsg( "mPath = " + mPath );
|
|
||||||
GDALAllRegister();
|
|
||||||
GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update );
|
|
||||||
|
|
||||||
if ( !hDS )
|
|
||||||
return NoCapabilities;
|
|
||||||
|
|
||||||
return SetCrs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QgsGdalLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
|
bool QgsGdalLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
|
||||||
{
|
{
|
||||||
QgsDebugMsg( "mPath = " + mPath );
|
|
||||||
GDALAllRegister();
|
|
||||||
GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update );
|
GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update );
|
||||||
|
|
||||||
if ( !hDS )
|
if ( !hDS )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QString wkt = crs.toWkt();
|
QString wkt = crs.toWkt();
|
||||||
if ( GDALSetProjection( hDS, wkt.toLocal8Bit().data() ) != CE_None )
|
if ( GDALSetProjection( hDS, wkt.toLocal8Bit().data() ) != CE_None )
|
||||||
{
|
{
|
||||||
|
GDALClose( hDS );
|
||||||
QgsDebugMsg( "Could not set CRS" );
|
QgsDebugMsg( "Could not set CRS" );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
GDALClose( hDS );
|
GDALClose( hDS );
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,22 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem* parent,
|
|||||||
{
|
{
|
||||||
mToolTip = uri;
|
mToolTip = uri;
|
||||||
setState( Populated ); // children are not expected
|
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()
|
QgsOgrLayerItem::~QgsOgrLayerItem()
|
||||||
@ -45,82 +61,52 @@ QgsOgrLayerItem::~QgsOgrLayerItem()
|
|||||||
|
|
||||||
QgsLayerItem::Capability QgsOgrLayerItem::capabilities()
|
QgsLayerItem::Capability QgsOgrLayerItem::capabilities()
|
||||||
{
|
{
|
||||||
QgsDebugMsg( "mPath = " + mPath );
|
return mCapabilities & SetCrs ? SetCrs : NoCapabilities;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
|
bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
|
||||||
{
|
{
|
||||||
QgsDebugMsg( "mPath = " + mPath );
|
if ( !( mCapabilities & SetCrs ) )
|
||||||
OGRRegisterAll();
|
|
||||||
OGRSFDriverH hDriver;
|
|
||||||
OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver );
|
|
||||||
|
|
||||||
if ( !hDataSource )
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QString driverName = OGR_Dr_GetName( hDriver );
|
QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) );
|
||||||
OGR_DS_Destroy( hDataSource );
|
QString wkt = crs.toWkt();
|
||||||
|
|
||||||
// we are able to assign CRS only to shapefiles :-(
|
// save ordinary .prj file
|
||||||
if ( driverName == "ESRI Shapefile" )
|
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 ) );
|
QTextStream prjStream( &prjFile );
|
||||||
QString wkt = crs.toWkt();
|
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
|
// save qgis-specific .qpj file (maybe because of better wkt compatibility?)
|
||||||
OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt.toLocal8Bit().data() );
|
QFile qpjFile( layerName + ".qpj" );
|
||||||
OSRMorphToESRI( hSRS ); // this is the important stuff for shapefile .prj
|
if ( qpjFile.open( QIODevice::WriteOnly ) )
|
||||||
char* pszOutWkt = NULL;
|
{
|
||||||
OSRExportToWkt( hSRS, &pszOutWkt );
|
QTextStream qpjStream( &qpjFile );
|
||||||
QFile prjFile( layerName + ".prj" );
|
qpjStream << wkt.toLocal8Bit().data() << endl;
|
||||||
if ( prjFile.open( QIODevice::WriteOnly ) )
|
qpjFile.close();
|
||||||
{
|
}
|
||||||
QTextStream prjStream( &prjFile );
|
else
|
||||||
prjStream << pszOutWkt << endl;
|
{
|
||||||
prjFile.close();
|
QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) );
|
||||||
}
|
return false;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// It it is impossible to assign a crs to an existing layer
|
return true;
|
||||||
// No OGR_L_SetSpatialRef : http://trac.osgeo.org/gdal/ticket/4032
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QgsOgrLayerItem::layerName() const
|
QString QgsOgrLayerItem::layerName() const
|
||||||
|
Loading…
x
Reference in New Issue
Block a user