0

I am using PostGIS to save geography data in type geography(POINT, 4326).

CREATE TABLE IF NOT EXISTS address (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  geog geography(POINT, 4326) NULL
)

And I am using Psycopg in Python to access data in PostGIS.

What is the best way to get the latitude, longitude?

One possible way is use ST_AsText:

select geog, ST_AsText(geog) from address

It will return geog as a string like POINT(104.5306 32.4089) and I can parse the string to get latitude and longitude.

Is there a better way do achieve what I want?

2
  • 3
    There are many ways of solving this task with Python. But why not to use ST_X and ST_Y functions in PostGIS?
    – Taras
    Oct 30, 2023 at 15:12
  • 2
    "Best" questions are inherently subjective, so this may be closed as opinion-based. Given the simplicity of points, using ST_X() and ST_Y() for lon and lat (respectively) seems an obvious solution.
    – Vince
    Oct 30, 2023 at 15:12

1 Answer 1

3

In postresql/postgis you can store up to 4D geometries XYZM. To access the coordinates:

  • st_x = x/longitude
  • st_y = y/latitude
  • st_z = z/altitude
  • st_m = m/measure.

Please note that these functions only work with geometry, so you will need to cast your geography to geometry: geog::geometry or cast(geog as geometry)

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.