Skip to content

Commit

Permalink
shapefile projection fix: save the real projection in .qpj next to th…
Browse files Browse the repository at this point in the history
…e .prj file (fixes #2123 and #2154)

git-svn-id: http://svn.osgeo.org/qgis/trunk@12274 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 28, 2009
1 parent 6411c7f commit 1712e42
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
16 changes: 8 additions & 8 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -95,15 +95,15 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
}

// datasource created, now create the output layer
QString layerName = shapefileName.left( shapefileName.lastIndexOf( ".shp", Qt::CaseInsensitive ) );
QString layerName = shapefileName.left( shapefileName.indexOf( ".shp", Qt::CaseInsensitive ) );
OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType>( geometryType );
mLayer = OGR_DS_CreateLayer( mDS, QFile::encodeName( layerName ).data(), ogrRef, wkbType, NULL );

if ( srs )
{
if ( shapefileName != layerName )
if ( driverName == "ESRI Shapefile" )
{
QFile prjFile( layerName + ".prj" );
QFile prjFile( layerName + ".qpj" );
if ( prjFile.open( QIODevice::WriteOnly ) )
{
QTextStream prjStream( &prjFile );
Expand All @@ -112,7 +112,7 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
}
else
{
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
QgsDebugMsg( "Couldn't open file " + layerName + ".qpj" );
}
}

Expand Down Expand Up @@ -420,10 +420,10 @@ bool QgsVectorFileWriter::deleteShapeFile( QString theFileName )
// TODO: should be case-insensitive
//
QString myFileBase = theFileName.replace( ".shp", "" );
bool ok = TRUE;
bool ok = true;

const char* suffixes[] = { ".shp", ".shx", ".dbf", ".prj", ".qix" };
for ( std::size_t i = 0; i < sizeof( suffixes ) / sizeof( char* ); i++ )
const char *suffixes[] = { ".shp", ".shx", ".dbf", ".prj", ".qix", ".qpj" };
for ( std::size_t i = 0; i < sizeof( suffixes ) / sizeof( *suffixes ); i++ )
{
QString file = myFileBase + suffixes[i];
QFileInfo myInfo( file );
Expand All @@ -432,7 +432,7 @@ bool QgsVectorFileWriter::deleteShapeFile( QString theFileName )
if ( !QFile::remove( file ) )
{
QgsDebugMsg( "Removing file failed : " + file );
ok = FALSE;
ok = false;
}
}
}
Expand Down
57 changes: 39 additions & 18 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1026,6 +1026,7 @@ int QgsOgrProvider::capabilities() const
ability |= ChangeGeometries;
}

#if 0
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
Expand Down Expand Up @@ -1059,25 +1060,20 @@ int QgsOgrProvider::capabilities() const
{
// No use required for this QGIS release.
}
#endif

if ( 1 )
// OGR doesn't handle shapefiles without attributes, ie. missing DBFs well, fixes #803
if ( ogrDriverName == "ESRI Shapefile" )
{
// Ideally this should test for Shapefile type and GDAL >= 1.2.6
// In reality, createSpatialIndex() looks after itself.
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1260
// test for Shapefile type and GDAL >= 1.2.6
ability |= CreateSpatialIndex;
}

#endif
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1600
// adding attributes was added in GDAL 1.6
if ( ogrDriverName.startsWith( "ESRI" ) )
{
// adding attributes was added in GDAL 1.6
ability |= AddAttributes;
}
#endif

// OGR doesn't handle shapefiles without attributes, ie. missing DBFs well, fixes #803
if ( ogrDriverName.startsWith( "ESRI" ) )
{
if ( mAttributeFields.size() == 0 )
{
QgsDebugMsg( "OGR doesn't handle shapefile without attributes well, ie. missing DBFs" );
Expand Down Expand Up @@ -1586,10 +1582,12 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,

OGR_DS_Destroy( dataSource );

if ( uri.endsWith( ".shp", Qt::CaseInsensitive ) )
QString driverName = OGR_Dr_GetName( driver );

if ( driverName == "ESRI Shapefile" )
{
QString layerName = uri.left( uri.length() - 4 );
QFile prjFile( layerName + ".prj" );
QString layerName = uri.left( uri.indexOf( ".shp", Qt::CaseInsensitive ) );
QFile prjFile( layerName + ".qpj" );
if ( prjFile.open( QIODevice::WriteOnly ) )
{
QTextStream prjStream( &prjFile );
Expand All @@ -1598,7 +1596,7 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
}
else
{
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
QgsDebugMsg( "Couldn't open file " + layerName + ".qpj" );
}
}

Expand All @@ -1618,6 +1616,29 @@ QgsCoordinateReferenceSystem QgsOgrProvider::crs()

QgsCoordinateReferenceSystem srs;

if ( ogrDriver )
{
QString driverName = OGR_Dr_GetName( ogrDriver );

if ( driverName == "ESRI Shapefile" )
{
QString layerName = mFilePath.left( mFilePath.indexOf( ".shp", Qt::CaseInsensitive ) );
QFile prjFile( layerName + ".qpj" );
if ( prjFile.open( QIODevice::ReadOnly ) )
{
QTextStream prjStream( &prjFile );
QString myWktString = prjStream.readLine();
prjFile.close();

// create CRS from Wkt
srs.createFromWkt( myWktString );

if ( srs.isValid() )
return srs;
}
}
}

OGRSpatialReferenceH mySpatialRefSys = OGR_L_GetSpatialRef( ogrLayer );
if ( mySpatialRefSys == NULL )
{
Expand All @@ -1626,10 +1647,10 @@ QgsCoordinateReferenceSystem QgsOgrProvider::crs()
else
{
// get the proj4 text
char * ppszProj4;
char *ppszProj4;
OSRExportToProj4( mySpatialRefSys, &ppszProj4 );
QgsDebugMsg( ppszProj4 );
char *pszWkt = NULL;
char *pszWkt = NULL;
OSRExportToWkt( mySpatialRefSys, &pszWkt );
QString myWktString = QString( pszWkt );
OGRFree( pszWkt );
Expand Down

0 comments on commit 1712e42

Please sign in to comment.