Wednesday, 8 May 2019

python - Getting EPSG code in pyproj with version==2.1.3?


I'm using pyproj in python and I want to get epsg code based on an existing Proj class. An example in version=1.9.6:


from pyproj import Proj
CGCS2000metre = Proj(init='EPSG:4544')
print(CGCS2000metre.srs)


# +units=m +init=epsg:4544

As you can see, I can find epsg code 4544 in the return result. But I can't do it in version==2.1.3:


from pyproj import Proj
CGCS2000metre = Proj(init='EPSG:4544')
print(CGCS2000metre.srs)

# +proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs

I just want to get epsg code from an existing Proj.



How can I get epsg code in pyproj with version==2.1.3?



Answer



Use pyproj.CRS


from pyproj import CRS
CGCS2000metre = CRS('epsg:4544')
print(CGCS2000metre.to_epsg()) # prints "4544"

To get a CRS in the way you're already using pyproj, you can get a CRS object like this:


from pyproj import Proj
CGCS2000metre = Proj(init='EPSG:4544').crs

print(CGCS2000metre.to_epsg()) # prints None
print(CGCS2000metre.to_epsg(min_confidence=25) # prints "4544"


https://pyproj4.github.io/pyproj/stable/api/proj.html#pyproj.proj.Proj.crs


Your misunderstanding is that srs is "the string form of the user input used to create the Proj".


You might need to adjust the min_confidence. There's a Q&A to understand that parameter here: Explaining pyproj to_epsg min_confidence parameter?


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...