Skip to content

Commit c8d4420

Browse files
etienneskyjef-n
authored andcommitted
add more autotests for ESRI WKT
1 parent dfe4a93 commit c8d4420

File tree

5 files changed

+113
-14
lines changed

5 files changed

+113
-14
lines changed

tests/src/core/testqgscoordinatereferencesystem.cpp

+112-14
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ Email : sherman at mrcc dot com
2323
//header for class being tested
2424
#include <qgscoordinatereferencesystem.h>
2525
#include <qgis.h>
26+
#include <qgsvectorlayer.h>
2627

2728
#include <proj_api.h>
2829
#include <gdal.h>
30+
#include <cpl_conv.h>
2931

3032
class TestQgsCoordinateReferenceSystem: public QObject
3133
{
@@ -60,6 +62,13 @@ class TestQgsCoordinateReferenceSystem: public QObject
6062
void setValidationHint();
6163
private:
6264
void debugPrint( QgsCoordinateReferenceSystem &theCrs );
65+
// these used by createFromESRIWkt()
66+
QStringList myWktStrings;
67+
QList<int> myGdalVersionOK;
68+
QStringList myFiles;
69+
QStringList myProj4Strings;
70+
QStringList myAuthIdStrings;
71+
QString testESRIWkt( int i, QgsCoordinateReferenceSystem &theCrs );
6372
};
6473

6574

@@ -70,11 +79,23 @@ void TestQgsCoordinateReferenceSystem::initTestCase()
7079
//
7180
// init QGIS's paths - true means that all path will be inited from prefix
7281
QgsApplication::init();
82+
QgsApplication::initQgis();
7383
QgsApplication::showSettings();
84+
7485
qDebug() << "GEOPROJ4 constant: " << GEOPROJ4;
7586
qDebug() << "GDAL version (build): " << GDAL_RELEASE_NAME;
7687
qDebug() << "GDAL version (runtime): " << GDALVersionInfo( "RELEASE_NAME" );
7788
qDebug() << "PROJ.4 version: " << PJ_VERSION;
89+
90+
// if user set GDAL_FIX_ESRI_WKT print a warning
91+
#if GDAL_VERSION_NUM >= 1900
92+
if ( strcmp( CPLGetConfigOption( "GDAL_FIX_ESRI_WKT", "" ), "" ) != 0 )
93+
{
94+
qDebug() << "Warning! GDAL_FIX_ESRI_WKT =" << CPLGetConfigOption( "GDAL_FIX_ESRI_WKT", "" )
95+
<< "this might generate errors!";
96+
}
97+
#endif
98+
7899
}
79100

80101
void TestQgsCoordinateReferenceSystem::wktCtor()
@@ -136,25 +157,102 @@ void TestQgsCoordinateReferenceSystem::createFromWkt()
136157
debugPrint( myCrs );
137158
QVERIFY( myCrs.isValid() );
138159
}
160+
161+
QString TestQgsCoordinateReferenceSystem::testESRIWkt( int i, QgsCoordinateReferenceSystem &myCrs )
162+
{
163+
debugPrint( myCrs );
164+
165+
if ( ! myCrs.isValid() )
166+
return QString( "test %1 crs is invalid" );
167+
if ( myCrs.toProj4() != myProj4Strings[i] )
168+
return QString( "test %1 PROJ.4 = [ %2 ] expecting [ %3 ]"
169+
).arg( i ).arg( myCrs.toProj4() ).arg( myProj4Strings[i] );
170+
if ( myCrs.authid() != myAuthIdStrings[i] )
171+
return QString( "test %1 AUTHID = [ %2 ] expecting [ %3 ]"
172+
).arg( i ).arg( myCrs.authid() ).arg( myAuthIdStrings[i] );
173+
174+
return "";
175+
}
139176
void TestQgsCoordinateReferenceSystem::createFromESRIWkt()
140177
{
178+
QString msg;
141179
QgsCoordinateReferenceSystem myCrs;
180+
const char* configOld = CPLGetConfigOption( "GDAL_FIX_ESRI_WKT", "" );
181+
182+
// for more tests add definitions here
183+
142184
// this example file taken from bug #5598
143-
QString myWKTString = "PROJCS[\"Indian_1960_UTM_Zone_48N\",GEOGCS[\"GCS_Indian_1960\",DATUM[\"D_Indian_1960\",SPHEROID[\"Everest_Adjustment_1937\",6377276.345,300.8017]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",500000.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",105.0],PARAMETER[\"Scale_Factor\",0.9996],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";
144-
#if GDAL_VERSION_NUM >= 1800
145-
QString myProj4String = "+proj=utm +zone=48 +a=6377276.345 +b=6356075.41314024 +towgs84=198,881,317,0,0,0,0 +units=m +no_defs";
146-
#else
147-
// for GDAL <1.8 towgs84 is absent but we'll have to live with that...
148-
QString myProj4String = "+proj=utm +zone=48 +a=6377276.345 +b=6356075.41314024 +units=m +no_defs";
149-
#endif
150-
QString myAuthId = "EPSG:3148";
185+
myWktStrings << "PROJCS[\"Indian_1960_UTM_Zone_48N\",GEOGCS[\"GCS_Indian_1960\",DATUM[\"D_Indian_1960\",SPHEROID[\"Everest_Adjustment_1937\",6377276.345,300.8017]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",500000.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",105.0],PARAMETER[\"Scale_Factor\",0.9996],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";
186+
myGdalVersionOK << 1800;
187+
myFiles << "bug5598.shp";
188+
myProj4Strings << "+proj=utm +zone=48 +a=6377276.345 +b=6356075.41314024 +towgs84=198,881,317,0,0,0,0 +units=m +no_defs";
189+
myAuthIdStrings << "EPSG:3148";
151190

152-
// use createFromUserInput and add the ESRI:: prefix to force morphFromESRI
153-
myCrs.createFromUserInput( "ESRI::" + myWKTString );
154-
debugPrint( myCrs );
155-
QVERIFY( myCrs.isValid() );
156-
QVERIFY( myCrs.toProj4() == myProj4String );
157-
QVERIFY( myCrs.authid() == myAuthId );
191+
// this example file taken from bug #5598 - geographic CRS only, supported since gdal 1.9
192+
myWktStrings << "GEOGCS[\"GCS_Indian_1960\",DATUM[\"D_Indian_1960\",SPHEROID[\"Everest_Adjustment_1937\",6377276.345,300.8017]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]";
193+
myFiles << "";
194+
myGdalVersionOK << 1900;
195+
myProj4Strings << "+proj=longlat +a=6377276.345 +b=6356075.41314024 +towgs84=198,881,317,0,0,0,0 +no_defs";
196+
myAuthIdStrings << "EPSG:4131";
197+
198+
// SAD69 geographic CRS, supported since gdal 1.9
199+
myWktStrings << "GEOGCS[\"GCS_South_American_1969\",DATUM[\"D_South_American_1969\",SPHEROID[\"GRS_1967_Truncated\",6378160.0,298.25]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]";
200+
myFiles << "";
201+
myGdalVersionOK << 1900;
202+
myProj4Strings << "+proj=longlat +ellps=aust_SA +towgs84=-57,1,-41,0,0,0,0 +no_defs";
203+
myAuthIdStrings << "EPSG:4618";
204+
205+
// do test with WKT definitions
206+
for ( int i = 0; i < myWktStrings.size() ; i++ )
207+
{
208+
QgsDebugMsg( QString( "i=%1 wkt=%2" ).arg( i ).arg( myWktStrings[i] ) );
209+
// use createFromUserInput and add the ESRI:: prefix to force morphFromESRI
210+
CPLSetConfigOption( "GDAL_FIX_ESRI_WKT", configOld );
211+
myCrs.createFromUserInput( "ESRI::" + myWktStrings[i] );
212+
msg = testESRIWkt( i, myCrs );
213+
if ( GDAL_VERSION_NUM <= myGdalVersionOK[i] )
214+
{
215+
QEXPECT_FAIL( "", QString( "expected failure with GDAL %1 : %2"
216+
).arg( GDAL_VERSION_NUM ).arg( msg ).toLocal8Bit().constData(),
217+
Continue );
218+
}
219+
QVERIFY2( msg == "", msg.toLocal8Bit().constData() );
220+
221+
// do test with shapefiles
222+
CPLSetConfigOption( "GDAL_FIX_ESRI_WKT", configOld );
223+
if ( myFiles[i] != "" )
224+
{
225+
// use ogr to open file, make sure CRS is ok
226+
// this probably could be in another test, but leaving it here since it deals with CRS
227+
QString fileStr = QString( TEST_DATA_DIR ) + QDir::separator() + myFiles[i];
228+
QgsDebugMsg( QString( "i=%1 file=%2" ).arg( i ).arg( fileStr ) );
229+
230+
QgsVectorLayer *myLayer = new QgsVectorLayer( fileStr, "", "ogr" );
231+
if ( !myLayer || ! myLayer->isValid() )
232+
{
233+
QVERIFY2( false, QString( "test %1 did not get valid vector layer from %2"
234+
).arg( i ).arg( fileStr ).toLocal8Bit().constData() );
235+
}
236+
else
237+
{
238+
myCrs = myLayer->crs();
239+
msg = testESRIWkt( i, myCrs );
240+
if ( GDAL_VERSION_NUM <= myGdalVersionOK[i] )
241+
{
242+
QEXPECT_FAIL( "", QString( "expected failure with GDAL %1 : %2 using layer %3"
243+
).arg( GDAL_VERSION_NUM ).arg( msg ).arg( fileStr ).toLocal8Bit().constData(),
244+
Continue );
245+
}
246+
QVERIFY2( msg == "", msg.toLocal8Bit().constData() );
247+
}
248+
if ( myLayer )
249+
delete myLayer;
250+
251+
}
252+
253+
}
254+
255+
// QVERIFY( bOK );
158256
}
159257
void TestQgsCoordinateReferenceSystem::createFromSrsId()
160258
{

tests/testdata/bug5598.dbf

376 Bytes
Binary file not shown.

tests/testdata/bug5598.prj

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PROJCS["Indian_1960_UTM_Zone_48N",GEOGCS["GCS_Indian_1960",DATUM["D_Indian_1960",SPHEROID["Everest_Adjustment_1937",6377276.345,300.8017]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",105.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]

tests/testdata/bug5598.shp

444 Bytes
Binary file not shown.

tests/testdata/bug5598.shx

108 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)