5

At this moment, i'm working in a shapefile visor in C++ and QT and using the GDAL/OGR library. I have this method to get the EPSG of my shapefiles:

OGRLayer layer = dataset->GetLayer(0);
OGRSpatialReference *spatialRef = layer->GetSpatialRef();

With this I get the EPSG number with:

atoi(spatialRef->GetAuthorityCode(NULL));

This work fine in all my shape files less one. In this case, the method always retun null.

I try use:

spatialRef->GetAuthorityCode("PROJCS");
spatialRef->GetAuthorityCode("GEOGCS");
spatialRef->GetAuthorityName("GEOGCS");

And all this method return "".

I check this shapefile in a gis program as QGIS and QGIS autodetected that his EPSG is 25830.

My question is this: could the projection information be readed with a different method than what I'm doing?

I wait yours suggestions.

Thank a lot.

EDIT

This is the content of .prj file:

PROJCS["ETRS89_UTM_zone_30N",GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1]]

Zharios
  • 183
  • 17

1 Answers1

1

Something like this should work:

OGRLayer  * layer = dataset->GetLayer(0);
layer->ResetReading();
OGRFeature * feat= layer->GetNextFeature(); 
OGRGeometry * geom = feat->GetGeometryRef(); 
OGRSpatialReference * spatRef = geom->getSpatialReference(); 
int EPSG =  spatRef->GetEPSGGeogCS(); 

Hope it helps!

Streamsoup
  • 624
  • 6
  • 7
  • I use your method, and work fine with all my shapes except with the referenced in this post. In this case, all EPSG from geometrys return -1. – Zharios Mar 15 '18 at 08:08
  • 1
    There's something definitely wrong with this particular EPSG in OGR, either in the underlying PROJ4 defs or in OGR/GEOS. Both GetEPSGGeogCS() and AutoIdentifyEPSG() return incorrect values (-1/7) when called on spatRef, even if the spatial reference system is initialized with proj4 parameters found on http://spatialreference.org/ref/epsg/etrs89-utm-zone-30n/, or with SetWellKnownGeogCS( "EPSG:25830" ). – Streamsoup Mar 15 '18 at 19:51