Skip to content

Commit cf0f00d

Browse files
author
jef
committedNov 28, 2009
shapefile projection fix: save the real projection in .qpj next to the .prj file (fixes #2123 and #2154)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12274 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed
 

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
9595
}
9696

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

102102
if ( srs )
103103
{
104-
if ( shapefileName != layerName )
104+
if ( driverName == "ESRI Shapefile" )
105105
{
106-
QFile prjFile( layerName + ".prj" );
106+
QFile prjFile( layerName + ".qpj" );
107107
if ( prjFile.open( QIODevice::WriteOnly ) )
108108
{
109109
QTextStream prjStream( &prjFile );
@@ -112,7 +112,7 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
112112
}
113113
else
114114
{
115-
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
115+
QgsDebugMsg( "Couldn't open file " + layerName + ".qpj" );
116116
}
117117
}
118118

@@ -420,10 +420,10 @@ bool QgsVectorFileWriter::deleteShapeFile( QString theFileName )
420420
// TODO: should be case-insensitive
421421
//
422422
QString myFileBase = theFileName.replace( ".shp", "" );
423-
bool ok = TRUE;
423+
bool ok = true;
424424

425-
const char* suffixes[] = { ".shp", ".shx", ".dbf", ".prj", ".qix" };
426-
for ( std::size_t i = 0; i < sizeof( suffixes ) / sizeof( char* ); i++ )
425+
const char *suffixes[] = { ".shp", ".shx", ".dbf", ".prj", ".qix", ".qpj" };
426+
for ( std::size_t i = 0; i < sizeof( suffixes ) / sizeof( *suffixes ); i++ )
427427
{
428428
QString file = myFileBase + suffixes[i];
429429
QFileInfo myInfo( file );
@@ -432,7 +432,7 @@ bool QgsVectorFileWriter::deleteShapeFile( QString theFileName )
432432
if ( !QFile::remove( file ) )
433433
{
434434
QgsDebugMsg( "Removing file failed : " + file );
435-
ok = FALSE;
435+
ok = false;
436436
}
437437
}
438438
}

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ int QgsOgrProvider::capabilities() const
10261026
ability |= ChangeGeometries;
10271027
}
10281028

1029+
#if 0
10291030
if ( OGR_L_TestCapability( ogrLayer, "FastSpatialFilter" ) )
10301031
// TRUE if this layer implements spatial filtering efficiently.
10311032
// Layers that effectively read all features, and test them with the
@@ -1059,25 +1060,20 @@ int QgsOgrProvider::capabilities() const
10591060
{
10601061
// No use required for this QGIS release.
10611062
}
1063+
#endif
10621064

1063-
if ( 1 )
1065+
// OGR doesn't handle shapefiles without attributes, ie. missing DBFs well, fixes #803
1066+
if ( ogrDriverName == "ESRI Shapefile" )
10641067
{
1065-
// Ideally this should test for Shapefile type and GDAL >= 1.2.6
1066-
// In reality, createSpatialIndex() looks after itself.
1068+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1260
1069+
// test for Shapefile type and GDAL >= 1.2.6
10671070
ability |= CreateSpatialIndex;
1068-
}
1069-
1071+
#endif
10701072
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1600
1071-
// adding attributes was added in GDAL 1.6
1072-
if ( ogrDriverName.startsWith( "ESRI" ) )
1073-
{
1073+
// adding attributes was added in GDAL 1.6
10741074
ability |= AddAttributes;
1075-
}
10761075
#endif
10771076

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

15871583
OGR_DS_Destroy( dataSource );
15881584

1589-
if ( uri.endsWith( ".shp", Qt::CaseInsensitive ) )
1585+
QString driverName = OGR_Dr_GetName( driver );
1586+
1587+
if ( driverName == "ESRI Shapefile" )
15901588
{
1591-
QString layerName = uri.left( uri.length() - 4 );
1592-
QFile prjFile( layerName + ".prj" );
1589+
QString layerName = uri.left( uri.indexOf( ".shp", Qt::CaseInsensitive ) );
1590+
QFile prjFile( layerName + ".qpj" );
15931591
if ( prjFile.open( QIODevice::WriteOnly ) )
15941592
{
15951593
QTextStream prjStream( &prjFile );
@@ -1598,7 +1596,7 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
15981596
}
15991597
else
16001598
{
1601-
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
1599+
QgsDebugMsg( "Couldn't open file " + layerName + ".qpj" );
16021600
}
16031601
}
16041602

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

16191617
QgsCoordinateReferenceSystem srs;
16201618

1619+
if ( ogrDriver )
1620+
{
1621+
QString driverName = OGR_Dr_GetName( ogrDriver );
1622+
1623+
if ( driverName == "ESRI Shapefile" )
1624+
{
1625+
QString layerName = mFilePath.left( mFilePath.indexOf( ".shp", Qt::CaseInsensitive ) );
1626+
QFile prjFile( layerName + ".qpj" );
1627+
if ( prjFile.open( QIODevice::ReadOnly ) )
1628+
{
1629+
QTextStream prjStream( &prjFile );
1630+
QString myWktString = prjStream.readLine();
1631+
prjFile.close();
1632+
1633+
// create CRS from Wkt
1634+
srs.createFromWkt( myWktString );
1635+
1636+
if ( srs.isValid() )
1637+
return srs;
1638+
}
1639+
}
1640+
}
1641+
16211642
OGRSpatialReferenceH mySpatialRefSys = OGR_L_GetSpatialRef( ogrLayer );
16221643
if ( mySpatialRefSys == NULL )
16231644
{
@@ -1626,10 +1647,10 @@ QgsCoordinateReferenceSystem QgsOgrProvider::crs()
16261647
else
16271648
{
16281649
// get the proj4 text
1629-
char * ppszProj4;
1650+
char *ppszProj4;
16301651
OSRExportToProj4( mySpatialRefSys, &ppszProj4 );
16311652
QgsDebugMsg( ppszProj4 );
1632-
char *pszWkt = NULL;
1653+
char *pszWkt = NULL;
16331654
OSRExportToWkt( mySpatialRefSys, &pszWkt );
16341655
QString myWktString = QString( pszWkt );
16351656
OGRFree( pszWkt );

0 commit comments

Comments
 (0)
Please sign in to comment.