Skip to content
Advertisement

How to convert PostGIS polygon coords into lat and lng?

From a json API, I am fetching data of an area displayed as a polygon. An endpoint example can be found here:

Here is a preview of the polygon displayed on the platform I am fetching the data from:

Below is a snippet of the element containing coordinates of the polygon.

"the_geom": {
        "coordinates": [
          [
            [
              543271.0,
              6213477.0
            ],
            [
              543553.0,
              6213471.0
            ],
            [
              543556.0,
              6213013.0
            ],
            [
              543287.0,
              6213008.0
            ],
            [
              543271.0,
              6213477.0
            ]
          ]
        ],
        "type": "Polygon"
      },
      "the_geom2": {
        "coordinates": [
          543416.1695806364,
          6213244.042039478
        ],
        "type": "Point"
      },

I would like to display the polygon within an embedded Google Map on a website. I am currently importing the coordinates into a content management system using PHP.

The problem is, that coordinates of the ‘the_geom’ element is, as far as I understand, coordinates from a PostGIS database.

I would like to convert the coordinates into latitude and longitude for displayed on a Google Map.

Is that possible? If it is – what would the process be?

Advertisement

Answer

Normally when a geometry does not contain any info regarding SRS, we can assume it is EPSG:4326 (aka WGS84), which is clearly not the case. Also, if I’m not mistaken, GeoJSON geometries only intend to be encoded as WGS84. So, I dare to say that this API isn’t providing correct information.

That being said, if you have access to PostgreSQL, consider either transforming the geometries to the SRS you want with ST_Transform in an UPDATE statement or transform them on query time.

Transforming geometries in query time

Taking into account that your geometry’s SRS is EPSG:23032 (ED50 / UTM zone 32N)

SELECT ST_AsGeoJSON(ST_Transform(the_geom),4326)) As wkt;

Which will give you the following result (considering your GeoJSON geometry) ..

{
  "type": "Polygon",
  "coordinates": [
    [
      [
        9.69359591513313,
        56.0626693979362
      ],
      [
        9.6981234775552,
        56.062589963491
      ],
      [
        9.69809730445666,
        56.0584749528625
      ],
      [
        9.69377718993567,
        56.0584543863193
      ],
      [
        9.69359591513313,
        56.0626693979362
      ]
    ]
  ]
}

.. and corresponds to ..

enter image description here

I’m not very familiar with Google Maps, but in case it only expects EPSG:4326 geometries, you need to transform it (e.g. using JavaScript) before passing the GeoJSON geometry. If Google Maps is flexible enough to accept different SRS, make sure you explicitly state the geometry SRS – in this case most likely EPSG:23032.

Good luck!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement