8
22
#include <qstring.h>
9
23
//gdal and ogr includes
10
24
#include <ogr_api.h>
11
#include <ogr_spatialref.h>
25
#include <ogr_srs_api.h>
12
26
#include <cpl_error.h>
13
class ProjectionCsHandlingTest : public CppUnit::TestCase {
15
ProjectionCsHandlingTest(){}
27
class ProjectionCsHandlingTest : public CppUnit::TestCase
30
ProjectionCsHandlingTest() {}
17
32
ProjectionCsHandlingTest( std::string name ) : CppUnit::TestCase( name ) { }
19
34
static CppUnit::Test *suite()
21
36
CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite( "ProjectionCsHandlingTest" );
22
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
24
&ProjectionCsHandlingTest::testProjImportWkt ) );
25
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
26
"testProjExportToProj4",
27
&ProjectionCsHandlingTest::testProjExportToProj4 ) );
28
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
29
"testProjNad27ExportToProj4",
30
&ProjectionCsHandlingTest::testProjNad27ExportToProj4 ) );
31
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
32
"testProjNad83ExportToProj4",
33
&ProjectionCsHandlingTest::testProjNad83ExportToProj4 ) );
34
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
35
"testFetchWktAttributes",
36
&ProjectionCsHandlingTest::testFetchWktAttributes ) );
37
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
38
"testProjEpsgExportToProj4",
39
&ProjectionCsHandlingTest::testProjEpsgExportToProj4 ) );
40
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
41
"testAkAlbersExportToProj4NoMorph",
42
&ProjectionCsHandlingTest::testAkAlbersExportToProj4NoMorph ) );
43
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
44
"testAkAlbersExportToProj4Morph",
45
&ProjectionCsHandlingTest::testAkAlbersExportToProj4Morph ) ); suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
47
&ProjectionCsHandlingTest::testWktFromFile ) );
48
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
50
&ProjectionCsHandlingTest::testOgrTransform ) );
37
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
39
&ProjectionCsHandlingTest::testProjImportWkt ) );
40
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
41
"testProjExportToProj4",
42
&ProjectionCsHandlingTest::testProjExportToProj4 ) );
43
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
44
"testProjNad27ExportToProj4",
45
&ProjectionCsHandlingTest::testProjNad27ExportToProj4 ) );
46
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
47
"testProjNad83ExportToProj4",
48
&ProjectionCsHandlingTest::testProjNad83ExportToProj4 ) );
49
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
50
"testFetchWktAttributes",
51
&ProjectionCsHandlingTest::testFetchWktAttributes ) );
52
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
53
"testProjEpsgExportToProj4",
54
&ProjectionCsHandlingTest::testProjEpsgExportToProj4 ) );
55
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
56
"testAkAlbersExportToProj4NoMorph",
57
&ProjectionCsHandlingTest::testAkAlbersExportToProj4NoMorph ) );
58
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
59
"testAkAlbersExportToProj4Morph",
60
&ProjectionCsHandlingTest::testAkAlbersExportToProj4Morph ) ); suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
62
&ProjectionCsHandlingTest::testWktFromFile ) );
63
suiteOfTests->addTest( new CppUnit::TestCaller<ProjectionCsHandlingTest>(
65
&ProjectionCsHandlingTest::testOgrTransform ) );
51
66
return suiteOfTests;
54
69
// Setup the common test members, etc
58
73
// wkt for creating a spatial refernence system
59
74
wkt = "GEOGCS[\"WGS 84\", "
60
" DATUM[\"WGS_1984\", "
61
" SPHEROID[\"WGS 84\",6378137,298.257223563, "
62
" AUTHORITY[\"EPSG\",7030]], "
63
" TOWGS84[0,0,0,0,0,0,0], "
64
" AUTHORITY[\"EPSG\",6326]], "
65
" PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
66
" UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
67
" AXIS[\"Lat\",NORTH], "
68
" AXIS[\"Long\",EAST], "
69
" AUTHORITY[\"EPSG\",4326]]";
75
" DATUM[\"WGS_1984\", "
76
" SPHEROID[\"WGS 84\",6378137,298.257223563, "
77
" AUTHORITY[\"EPSG\",7030]], "
78
" TOWGS84[0,0,0,0,0,0,0], "
79
" AUTHORITY[\"EPSG\",6326]], "
80
" PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
81
" UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
82
" AXIS[\"Lat\",NORTH], "
83
" AXIS[\"Long\",EAST], "
84
" AUTHORITY[\"EPSG\",4326]]";
70
85
wktDest = "GEOGCS[\"GCS_North_American_1927\",DATUM[\"D_North_American_1927\",SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]";
71
86
wktDestNad83 = "GEOGCS[\"GCS_North_American_1983\",DATUM[\"D_North_American_1983\",SPHEROID[\"GRS_1980\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.0174532925199433]]";
72
87
wktEpsg = "GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\",SPHEROID[\"Clarke 1866\",6378206.4,294.978698213901]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]]";
74
wktAkAlbers = "PROJCS[\"Alaska_Albers_Equal_Area_Conic\",GEOGCS[\"GCS_North_American_1927\",DATUM[\"D_North_American_1927\",SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Albers\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",-154.0],PARAMETER[\"Standard_Parallel_1\",55.0],PARAMETER[\"Standard_Parallel_2\",65.0],PARAMETER[\"Latitude_Of_Origin\",50.0],UNIT[\"Meter\",1.0]]";
89
wktAkAlbers = "PROJCS[\"Alaska_Albers_Equal_Area_Conic\",GEOGCS[\"GCS_North_American_1927\",DATUM[\"D_North_American_1927\",SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],CRS[\"Albers\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",-154.0],PARAMETER[\"Standard_Parallel_1\",55.0],PARAMETER[\"Standard_Parallel_2\",65.0],PARAMETER[\"Latitude_Of_Origin\",50.0],UNIT[\"Meter\",1.0]]";
78
93
// Test creation of a OGRSpatialReference object from wkt
80
void testProjImportWkt() {
95
void testProjImportWkt()
81
97
// create a spatial reference system object
82
std::cout << "\n\nCreating and OGRSpatialReference object from WKT" << std::endl;
83
OGRSpatialReference myInputSpatialRefSys;
84
char *pWkt = (char*)wkt.ascii();
85
CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE);
98
std::cout << "\n\nCreating and OGRSpatialReference object from Wkt" << std::endl;
99
OGRSpatialReferenceH myInputSpatialRefSys = OSRNewSpatialReference( NULL );
100
char *pWkt = ( char* )wkt.ascii();
101
CPPUNIT_ASSERT( OSRImportFromWkt( myInputSpatialRefSys, &pWkt ) == OGRERR_NONE );
102
OSRDestroySpatialReference( myInputSpatialRefSys );
88
105
// Test fetch of proj4 parameters from an OGRSpatialReference object
127
144
void testProjNad83ExportToProj4()
129
std::cout << "\n\nGetting NAD83 proj4 parameters used in states.shp" << std::endl;
146
std::cout << "\n\nGetting NAD83 proj4 parameters used in states.shp" << std::endl;
130
147
// set up the spatial ref
131
148
OGRSpatialReference myInputSpatialRefSys;
132
char *pWkt = (char*)wktDestNad83.ascii();
133
CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE);
149
char *pWkt = ( char* )wktDestNad83.ascii();
150
CPPUNIT_ASSERT( myInputSpatialRefSys.importFromWkt( &pWkt ) == OGRERR_NONE );
134
151
// morph it from esri
135
std::cout << "\tMorphing from ESRI to standard form before getting proj4 parameters" << std::endl;
136
CPPUNIT_ASSERT(myInputSpatialRefSys.morphFromESRI() == OGRERR_NONE);
152
std::cout << "\tMorphing from ESRI to standard form before getting proj4 parameters" << std::endl;
153
CPPUNIT_ASSERT( myInputSpatialRefSys.morphFromESRI() == OGRERR_NONE );
137
154
// get the proj4 for the projection
139
CPPUNIT_ASSERT(myInputSpatialRefSys.exportToProj4(&proj4src) == OGRERR_NONE);
140
std::cout << "\tPROJ4: " << proj4src << std::endl;
141
CPPUNIT_ASSERT(QString(proj4src).find("datum") > -1);
156
CPPUNIT_ASSERT( myInputSpatialRefSys.exportToProj4( &proj4src ) == OGRERR_NONE );
157
std::cout << "\tPROJ4: " << proj4src << std::endl;
158
CPPUNIT_ASSERT( QString( proj4src ).find( "datum" ) > -1 );
144
161
// Test fetch of proj4 parameters from GEOGCS NAD83 OGRSpatialReference object
162
// in EpsgCrsId format
146
163
// Failure occurs if datum field is not found in the proj4 parameter string
148
165
void testProjEpsgExportToProj4()
150
std::cout << "\n\nGetting NAD83 proj4 parameters from an EPSG format WKT" << std::endl;
167
std::cout << "\n\nGetting NAD83 proj4 parameters from an EpsgCrsId format Wkt" << std::endl;
151
168
// set up the spatial ref
152
169
OGRSpatialReference myInputSpatialRefSys;
153
char *pWkt = (char*)wktEpsg.ascii();
154
CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE);
170
char *pWkt = ( char* )wktEpsg.ascii();
171
CPPUNIT_ASSERT( myInputSpatialRefSys.importFromWkt( &pWkt ) == OGRERR_NONE );
155
172
// get the proj4 for the projection
157
CPPUNIT_ASSERT(myInputSpatialRefSys.exportToProj4(&proj4src) == OGRERR_NONE);
158
std::cout << "\tPROJ4: " << proj4src << std::endl;
159
CPPUNIT_ASSERT(QString(proj4src).find("datum") > -1);
174
CPPUNIT_ASSERT( myInputSpatialRefSys.exportToProj4( &proj4src ) == OGRERR_NONE );
175
std::cout << "\tPROJ4: " << proj4src << std::endl;
176
CPPUNIT_ASSERT( QString( proj4src ).find( "datum" ) > -1 );
162
179
// Test fetch of proj4 parameters from Alaska Albers OGRSpatialReference object
185
202
void testAkAlbersExportToProj4Morph()
187
std::cout << "\n\nGetting Alaska Albers proj4 parameters from kodiak.prj" << std::endl;
204
std::cout << "\n\nGetting Alaska Albers proj4 parameters from kodiak.prj" << std::endl;
188
205
// set up the spatial ref
189
206
OGRSpatialReference myInputSpatialRefSys;
190
char *pWkt = (char*)wktAkAlbers.ascii();
191
CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE);
192
std::cout << "\tGetting proj4 paramters with morph to ESRI form" << std::endl;
193
CPPUNIT_ASSERT(myInputSpatialRefSys.morphFromESRI() == OGRERR_NONE);
207
char *pWkt = ( char* )wktAkAlbers.ascii();
208
CPPUNIT_ASSERT( myInputSpatialRefSys.importFromWkt( &pWkt ) == OGRERR_NONE );
209
std::cout << "\tGetting proj4 parameters with morph to ESRI form" << std::endl;
210
CPPUNIT_ASSERT( myInputSpatialRefSys.morphFromESRI() == OGRERR_NONE );
194
211
// get the proj4 for the unmorphed projection
196
CPPUNIT_ASSERT(myInputSpatialRefSys.exportToProj4(&proj4src) == OGRERR_NONE);
197
std::cout << "\tPROJ4: " << proj4src << std::endl;
198
CPPUNIT_ASSERT(QString(proj4src).find("datum") > -1);
213
CPPUNIT_ASSERT( myInputSpatialRefSys.exportToProj4( &proj4src ) == OGRERR_NONE );
214
std::cout << "\tPROJ4: " << proj4src << std::endl;
215
CPPUNIT_ASSERT( QString( proj4src ).find( "datum" ) > -1 );
201
218
// Test fetching of wkt properties from a spatial ref object
203
220
void testFetchWktAttributes()
205
std::cout << "\n\nFetching states.prj WKT attributes using OGRSpatialReference::GetAttrValue" << std::endl;
222
std::cout << "\n\nFetching states.prj Wkt attributes using OGRSpatialReference::GetAttrValue" << std::endl;
206
223
// set up the spatial ref - use the nad83 from states.prj
207
224
OGRSpatialReference myInputSpatialRefSys;
208
char *pWkt = (char*)wktDestNad83.ascii();
209
CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE);
225
char *pWkt = ( char* )wktDestNad83.ascii();
226
CPPUNIT_ASSERT( myInputSpatialRefSys.importFromWkt( &pWkt ) == OGRERR_NONE );
210
227
// test access to the datum
211
const char *datum = myInputSpatialRefSys.GetAttrValue("DATUM", 0);
212
CPPUNIT_ASSERT(datum != 0);
213
std::cout << "\tDatum: " << datum << std::endl;
228
const char *datum = myInputSpatialRefSys.GetAttrValue( "DATUM", 0 );
229
CPPUNIT_ASSERT( datum != 0 );
230
std::cout << "\tDatum: " << datum << std::endl;
216
// Test the WKT contained in wkt.txt in the current directory to see if
233
// Test the Wkt contained in wkt.txt in the current directory to see if
217
234
// the datum can be determined
219
236
void testWktFromFile()
221
std::ifstream wktIn("./wkt.txt");
238
std::ifstream wktIn( "./wkt.txt" );
222
239
char *buf = new char[16384];
223
wktIn.getline(buf, 16384);
240
wktIn.getline( buf, 16384 );
225
std::cout << "\n\nGetting proj4 parameters from wkt.txt" << std::endl;
226
std::cout << buf << std::endl;
242
std::cout << "\n\nGetting proj4 parameters from wkt.txt" << std::endl;
243
std::cout << buf << std::endl;
227
244
// set up the spatial ref
228
245
OGRSpatialReference myInputSpatialRefSys;
229
CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&buf)== OGRERR_NONE);
230
// std::cout << "\tGetting proj4 paramters with morph to ESRI form" << std::endl;
246
CPPUNIT_ASSERT( myInputSpatialRefSys.importFromWkt( &buf ) == OGRERR_NONE );
247
// std::cout << "\tGetting proj4 parameters with morph to ESRI form" << std::endl;
231
248
// CPPUNIT_ASSERT(myInputSpatialRefSys.morphFromESRI() == OGRERR_NONE);
232
249
// get the proj4 for the unmorphed projection
234
CPPUNIT_ASSERT(myInputSpatialRefSys.exportToProj4(&proj4src) == OGRERR_NONE);
235
std::cout << "\tPROJ4: " << proj4src << std::endl;
251
CPPUNIT_ASSERT( myInputSpatialRefSys.exportToProj4( &proj4src ) == OGRERR_NONE );
252
std::cout << "\tPROJ4: " << proj4src << std::endl;
236
253
// morph it then spew it
237
254
myInputSpatialRefSys.morphFromESRI();
238
myInputSpatialRefSys.exportToProj4(&proj4src);
239
std::cout << "\tMorphed PROJ4: " << proj4src << std::endl;
240
CPPUNIT_ASSERT(QString(proj4src).find("datum") > -1);
255
myInputSpatialRefSys.exportToProj4( &proj4src );
256
std::cout << "\tMorphed PROJ4: " << proj4src << std::endl;
257
CPPUNIT_ASSERT( QString( proj4src ).find( "datum" ) > -1 );
243
260
void testOgrTransform()
245
std::cout << "\n\nTesting OGR transform of kodiak.prj to WGS 84 Geographic" << std::endl;
262
std::cout << "\n\nTesting OGR transform of kodiak.prj to WGS 84 Geographic" << std::endl;
246
263
// set up the spatial ref
247
264
OGRSpatialReference myInputSpatialRefSys;
248
char *pWkt = (char*)wktAkAlbers.ascii();
249
CPPUNIT_ASSERT(myInputSpatialRefSys.importFromWkt(&pWkt)== OGRERR_NONE);
250
std::cout << "\tGetting proj4 paramters with morph to ESRI form" << std::endl;
251
CPPUNIT_ASSERT(myInputSpatialRefSys.morphFromESRI() == OGRERR_NONE);
252
OGRSpatialReference oTargetSRS;
253
char *pWgs84 = (char *)wkt.ascii();
254
oTargetSRS.importFromWkt(&pWgs84);
265
char *pWkt = ( char* )wktAkAlbers.ascii();
266
CPPUNIT_ASSERT( myInputSpatialRefSys.importFromWkt( &pWkt ) == OGRERR_NONE );
267
std::cout << "\tGetting proj4 parameters with morph to ESRI form" << std::endl;
268
CPPUNIT_ASSERT( myInputSpatialRefSys.morphFromESRI() == OGRERR_NONE );
269
OGRSpatialReference oTarcrs;
270
char *pWgs84 = ( char * )wkt.ascii();
271
oTarcrs.importFromWkt( &pWgs84 );
255
272
OGRCoordinateTransformation *poCT;
256
273
poCT = OGRCreateCoordinateTransformation( &myInputSpatialRefSys,
260
poCT->Transform(1, &x, &y);
261
std::cout << "Transformed 0,0 albers point = " << x << ", " << y << std::endl;
262
CPPUNIT_ASSERT((x == -154.0) || (y == 50.0));
277
poCT->Transform( 1, &x, &y );
278
std::cout << "Transformed 0,0 albers point = " << x << ", " << y << std::endl;
279
CPPUNIT_ASSERT(( x == -154.0 ) || ( y == 50.0 ) );
263
280
// get the proj4 for the morphed projection
265
CPPUNIT_ASSERT(myInputSpatialRefSys.exportToProj4(&proj4src) == OGRERR_NONE);
266
std::cout << "\tPROJ4: " << proj4src << std::endl;
282
CPPUNIT_ASSERT( myInputSpatialRefSys.exportToProj4( &proj4src ) == OGRERR_NONE );
283
std::cout << "\tPROJ4: " << proj4src << std::endl;
268
std::cout << "Testing inverse transform" << std::endl;
269
poCT = OGRCreateCoordinateTransformation( &oTargetSRS, &myInputSpatialRefSys);
285
std::cout << "Testing inverse transform" << std::endl;
286
poCT = OGRCreateCoordinateTransformation( &oTarcrs, &myInputSpatialRefSys );
272
poCT->Transform(1, &x, &y);
273
CPPUNIT_ASSERT((x == 0) || (y == 0));
274
std::cout << "Transformed -154,50 geographic point = " << x << ", " << y << std::endl;
289
poCT->Transform( 1, &x, &y );
290
CPPUNIT_ASSERT(( x == 0 ) || ( y == 0 ) );
291
std::cout << "Transformed -154,50 geographic point = " << x << ", " << y << std::endl;
279
// WKT for default projection hardcoded in QgsCoordinateTransform class
296
// Wkt for default projection hardcoded in QgsCoordinateTransform class
281
// WKT for an ESRI style GEOGCS in NAD27
298
// Wkt for an ESRI style GEOGCS in NAD27
283
// WKT for an ESRI style GEOGCS in NAD83 (from states.shp shapefile)
300
// Wkt for an ESRI style GEOGCS in NAD83 (from states.shp shapefile)
284
301
QString wktDestNad83;
285
// WKT for an EPSG style GEOGCS
302
// Wkt for an EpsgCrsId style GEOGCS
287
// WKT for an ESRI style PROJCS as read from a shapefile
304
// Wkt for an ESRI style PROJCS as read from a shapefile
288
305
QString wktAkAlbers;