skip to Main Content

When converting to GeoJSON I’m finding that lat/lon are being transposed. I don’t understand the reason why and haven’t found any suitable answers in the MySQL documentation.

  • The WKT seems to be correct as verified with https://wktmap.com/
  • The GeoJSON is incorrect – it seems that lat/lon are transposed as verified with https://geojson.io/
  • The column has the SRID set to 4326
  • The GeoJSON is set to EPSG:4326 too

I understand that GeoJSON should always be ordered lon-lat so does this suggest that I stored the data in MySQL with lon/lat transposed?

mysql> SHOW CREATE TABLE lookup_geometries G
*************************** 1. row ***************************
       Table: lookup_geometries
Create Table: CREATE TABLE `lookup_geometries` (
  `name` varchar(100) NOT NULL,
  `geometry` geometry NOT NULL /*!80003 SRID 4326 */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

mysql> SELECT geometry, ST_asWKT(geometry), ST_asGeoJSON(geometry,4,2) FROM lookup_geometries WHERE name = "Newlands Corner" G
*************************** 1. row ***************************
                  geometry: 0xE6100000010100000000FCF8F0D19D4940759ABB4C0335E0BF
        ST_asWKT(geometry): POINT(-0.5064713 51.2329694)
ST_asGeoJSON(geometry,4,2): {"crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "type": "Point", "coordinates": [51.233, -0.5065]}
1 row in set (0.00 sec)

2

Answers


  1. A similar case was reported in 2020: https://bugs.mysql.com/bug.php?id=98731

    It was closed as "not a bug" because:

    ST_GeomFromText correctly reports longitude as longitude and latitude as latitude. The polygon is created from WKT with points (lat 0 long 1), (lat 1 long 2), (lat 2 long 1), (lat 0 long 1). MySQL follows the OGC Axis Order Policy Guidance (OGC 08-38r5), so unless the format defines an explicit axis order (which WKT doesn’t, but GeoJSON does), the spatial reference system axis order is used. This means that your ST_GeomFromText statement takes lat-long order, while ST_AsGeoJSON produces long-lat order.

    If you use the axis-order parameter to change the interpretation of the WKT string, you can get the result you want:

    Examples follow this response in the bug report (see the link above).

    Login or Signup to reply.
  2. GeoJSON is always lon/lat as defined by the standard.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search