0

Consider the following CRS as examples:

QgsCoordinateReferenceSystem.fromEpsgId(4326)
QgsCoordinateReferenceSystem.fromEpsgId(25832)
QgsCoordinateReferenceSystem.fromEpsgId(3857)

How can I determine programmatically which axes they have and which axis is latitude/longitude resp. northing/easting resp. x/y if I have a QgsPoint geometry in such system with a .x() and .y() coordinate? I.e. if I have a QgsPoint(10, 55) in a EPSG:4326 layer, how can I know programmatically if its .x() value 10 is a latitude or a longitude?

The WKT returned by toWkt() does sometimes return information about the axes but not for EPSG:4326 plus I am not sure if the order in the returned WKT text is in any way corresponding to the x (first?) and y (second?) coordinates of the geometry:

>>> QgsCoordinateReferenceSystem.fromEpsgId(4326).toWkt()
'GEOGCS["WGS 84",
  DATUM["WGS_1984",
    SPHEROID["WGS 84",6378137,298.257223563,
      AUTHORITY["EPSG","7030"]],
    AUTHORITY["EPSG","6326"]],
  PRIMEM["Greenwich",0,
    AUTHORITY["EPSG","8901"]],
  UNIT["degree",0.0174532925199433,
    AUTHORITY["EPSG","9122"]],
  AUTHORITY["EPSG","4326"]]'
>>> QgsCoordinateReferenceSystem.fromEpsgId(25832).toWkt()
'PROJCS["ETRS89 / UTM zone 32N",
GEOGCS["ETRS89",
    DATUM["European_Terrestrial_Reference_System_1989",
        SPHEROID["GRS 1980",6378137,298.257222101,
            AUTHORITY["EPSG","7019"]],
        AUTHORITY["EPSG","6258"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4258"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",9],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
    AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH],
AUTHORITY["EPSG","25832"]]'
>>> QgsCoordinateReferenceSystem.fromEpsgId(3857).toWkt()
'PROJCS["WGS 84 / Pseudo-Mercator",
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]],
PROJECTION["Mercator_1SP"],
PARAMETER["central_meridian",0],
PARAMETER["scale_factor",1],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["metre",1,
    AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH],
EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"],
AUTHORITY["EPSG","3857"]]'
5
  • 1
    I would have a try with hasAxesInverted in qgis.org/pyqgis/3.2/core/Coordinate/… for checking the order.
    – user30184
    Commented Jun 3, 2023 at 12:51
  • That works well! Do you know what the normal case is ("East, North" apparently) and where this is standardized? Commented Jun 5, 2023 at 10:48
  • It was "standardized" in many common GIS software that often did not support coordinate systems at all when they were young. The developers were only thinking about the screen coordinates, and it was natural to think that x is left-to-right (longitude or easting) and y is bottom-to-top (latitude or northing). This is sometimes called as "traditional GIS order" gdal.org/tutorials/osr_api_tut.html#crs-and-axis-order. In many GIS formats (shapefile, PostGIS Well Known Binary etc.) coordinates are also natively saved in this order independently of the CRS.
    – user30184
    Commented Jun 5, 2023 at 11:52
  • Thanks! I looked into the QGIS source and it really is that simple: it checks if the first axis is "north" (aka "y") and if so, hasAxisInverted will return True. So if the order is not x/y, then it is inverted. Commented Jun 5, 2023 at 13:15
  • 1
    Be careful also with x and y. For example the official abbreviation for the north axis in the Gauss-Krüger coordinate systems is "X" epsg.org/crs/wkt/id/21463.
    – user30184
    Commented Jun 5, 2023 at 13:23

1 Answer 1

1

You can use the QgsCoordinateReferenceSystem.hasAxisInverted() method to check against the standard case (which seems to be "east, north", i.e. "x, y").

Examples with a fairly random selection of CRSs:

EPSG code hasAxisInverted Order on epsg.org Match
25832 False e, n
31467 True n, e
3857 False e, n
7789 False x, y, z
3034 True n, e
3035 True n, e
4936 False x, y, z
3995 False x, y
9840 True n, e
5070 False e, n
9822 False e, n
4326 True n, e

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.