Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export coordinate system as ESPG code: to_epsg() or ExportToEPSG()

When dealing with coordinates systems in Python with fiona and osgeo, there seem to be a lot of ways to define a coordinate system by importing/exporting different crs formats , for example:

FIONA:

from fiona.crs import from_epsg,from_string,to_string

# Import crs from different formats:
wgs = from_epsg(4326)
wgs = from_string("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ")

# Export crs as proj4 string
wgs_proj4_string = to_string(wgs)

OSGEO:

from osgeo import osr

srs = osr.SpatialReference()
srs.ImportFromESRI(['GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'])
srs.ImportFromProj4("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
srs.ImportFromEPSG(4326)
#the import options are very rich

# Export to different formats
srs.ExportToProj4()
srs.ExportToWkt()
srs.ExportToXML()
#... many export options!

However, I noticed, that both libraries allow for easy definition of crs by its EPSG code, but they both lack an inverse function (exporting a crs as an ESPG code).

The closest I get the EPSG code is by:

srs.AutoIdentifyEPSG()
epsg = srs.GetAuthorityCode(None)

but it doesn't seem to be so reliable, and also other proposed solutions seem to include a great amount of tweaking or at least web service dependency.

QUESTIONS:

  1. Can somebody show me a simple, straight-forward way to export a CRS as an EPSG code in python? Something like to_epsg() in Fiona or ExportToEPSG() in osgeo?

  2. Can someone explain the theoretical background of having such a shortage of EPSG export possibilites throughout the internet, especially compared to the ease of importing by EPSG code. Isn't the whole point of EPSG codes in making coordinate systems easy to identify and use for people without advanced geospatial expertise? Shouldn't it serve like an ID for a CRS and therefore be easily retrievable?

like image 748
Marjan Moderc Avatar asked Feb 13 '17 15:02

Marjan Moderc


1 Answers

Could try pyproj CRS: https://pyproj4.github.io/pyproj/stable/examples.html#converting-crs-to-a-different-format

from pyproj import CRS
from fiona.crs import to_string, from_epsg

fiona_crs = from_epsg(28356)
proj4_crs = CRS.from_proj4(to_string(fiona_crs))
srid = proj4_crs.to_epsg()

Although, for some reason this doesn't work for EPSG 4326, for me, unfortunately (to_epsg returns None in that case), not sure why.

like image 190
ssast Avatar answered Oct 04 '22 21:10

ssast