2

I can create a QgsCoordinateReferenceSystem from a EPSG ID.

How can I get a string formatted like EPSG:4326 back from the QgsCoordinateReferenceSystem?

>>> p = QgsCoordinateReferenceSystem.fromEpsgId(4326)

>>> p.toProj()
'+proj=longlat +datum=WGS84 +no_defs'

>>> p.userFriendlyIdentifier()
'EPSG:4326 - WGS 84'

Do I really have to use p.userFriendlyIdentifier().split(" ")[0] or is there a better way?

2 Answers 2

3

Found a corresponding method authid().

>>> p = QgsCoordinateReferenceSystem.fromEpsgId(4326)

>>> p.authid()
'EPSG:4326'
2

Try the userFriendlyIdentifier() method with the ShortString type:

Returns a user-friendly identifier for the CRS.

Depending on the format of the CRS, this may reflect the CRSes registered name, or for CRSes not saved in the database it may reflect the underlying WKT or Proj string definition of the CRS.

In most cases, this is the best method to use when showing a friendly identifier for the CRS to a user.

Available since QGIS 3.10.3

p = QgsCoordinateReferenceSystem.fromEpsgId(4326)

if p.isValid():
    print(p.userFriendlyIdentifier(type=QgsCoordinateReferenceSystem.ShortString)) #EPSG:4326

Also there is geographicCrsAuthId() method available:

Returns auth id of related geographic CRS.

p = QgsCoordinateReferenceSystem.fromEpsgId(4326)

if p.isValid() and p.isGeographic():
    print(p.geographicCrsAuthId()) #EPSG:4326

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.