1

I have the following trivial code, which transforms coordinates from Geographic CS (EPSG 4230, ED50) to Projected CS (EPSG 23021 ED50 / UTM zone 31N)

#include <ogr_spatialref.h>
#include <ogr_core.h>
#include <ogr_srs_api.h>

int main(int argc, char *argv[])
{
  double coord[3] = {11.0, 60.0, 0.0};
  int res;
  
  OGRCoordinateTransformationH transform = NULL;
  OGRSpatialReference oSRS, iSRS;
  oSRS.importFromEPSG(23031);
  iSRS.SetWellKnownGeogCS( "EPSG:4230" );
  
  transform = OCTNewCoordinateTransformation(OGRSpatialReference::ToHandle(&iSRS), OGRSpatialReference::ToHandle(&oSRS));
  OCTTransform(transform, 1, coord+0, coord+1, coord+2);

  printf("%lf %lf %lf\n", coord[0], coord[1]);
}

Linking this against gday version below 3.x prints the correct result:

945515.849694 6678568.489194

However, if I use any gdal 3.x, I get the following values, which are wrong:

7952467.873008 2186283.396945

I can't see what I'm doing wrong. Is this a bug, or is there a mistake in my code?

6
  • 2
    Did the axis order change?
    – Ian Turton
    Commented Aug 8 at 16:23
  • @IanTurton Wow. It seems you're right. I get the correct result when I change X and Y coordinates. But It seems that I only have to do this for shapes that are saved in geographical CS. When I use shape files with projected coordinates, the order is as it was earlier. How can I deal with this??
    – angainor
    Commented Aug 8 at 18:28
  • 2
    Read gdal.org/tutorials/osr_api_tut.html
    – user30184
    Commented Aug 8 at 18:56
  • 2
    In particular, the "CRS and axis order" section.
    – user2856
    Commented Aug 9 at 1:37
  • Thanks for your help. I guess this means that, if I get the coordinates from a shape file, that shape file must be written with wrong order.
    – angainor
    Commented Aug 9 at 8:38

1 Answer 1

3

Read the "CRS and axis order" section of the OSR API tutorial.

Basically, until GDAL 3.0 the OGRSpatialReference class ignored the axis order (lon, lat v.s lat, lon) specified by the CRS and as of 3.0 GDAL honours the specified axis order.

To go back to the pre-3.0 behaviour, just set OGRSpatialReference::SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)

I'm not sure of the syntax for C/C++ so below is a python example:

from osgeo import osr

coord = (11.0, 60.0, 0.0)
oSRS = osr.SpatialReference()
iSRS = osr.SpatialReference()
iSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
iSRS.ImportFromEPSG(4230)
oSRS.ImportFromEPSG(23031)

transform = osr.CoordinateTransformation(iSRS, oSRS)
p = transform.TransformPoint(coord)
print(p)

Output:

(945515.8496942197, 6678568.489194222, 0.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.