Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL select query to extract latitude and longitude from a point

What SELECT query should be used to extract latitude and longitude from a point?
I cannot use PostGIS.

Example point (point type value) stored in the database:

   my_point
--------------
(50.850,4.383)

Expected result after executing the query:

  lat  |  lng
---------------
50.850 | 4.383

The query below works fine but it does not look efficient.

SELECT 
    split_part(trim(my_point::text, '()'), ',', 1)::float AS lat, 
    split_part(trim(my_point::text, '()'), ',', 2)::float AS lng
FROM my_table;
like image 611
rafis Avatar asked Aug 28 '13 15:08

rafis


1 Answers

Always Read The Fine Manuals

It is possible to access the two component numbers of a point as though the point were an array with indexes 0 and 1. For example, if t.p is a point column then SELECT p[0] FROM t retrieves the X coordinate and UPDATE t SET p1 = ... changes the Y coordinate. In the same way, a value of type box or lseg can be treated as an array of two point values.

like image 194
Richard Huxton Avatar answered Jan 04 '23 16:01

Richard Huxton