0

I have a table in PostgreSQL. And it has no X and Y coordinates. But, it has a geom column with the WKT. Also, I have created an empty column called LAT and other called LONG. So, what I want is to write the Latitude and Longitude of the WKT column for each point. How can I do that?

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Mar 28, 2020 at 17:01
2
  • 3
    What is the geometry type? Can you give an example? Commented Mar 28, 2020 at 18:00
  • 1
    postgis.net/docs/ST_GeomFromWKB.html can create an instance of the appropriate geometry type. Commented Mar 28, 2020 at 19:12

2 Answers 2

2

The ST_GeomFromText function converts the WKT into a geometry object that PostGIS can understand:

UPDATE MyTable
SET Lat = ST_Y(ST_GeomFromText(WKT_column)),
 Lon = ST_X(ST_GeomFromText(WKT_column));
Vince
20.5k16 gold badges49 silver badges65 bronze badges
answered Mar 30, 2020 at 10:56
0

If you have a column to store point geometry in your table e.g. "geom", then you can use the ST_MakePoint function. I suppose you have 2D geometry and you have the latitude and longitude in "lat" and "lon" column as decimal degrees. I supposed latitudes and longitudes are in WGS84 (SRID=4326). The following SQL command will update geom column:

UPDATE your_table SET geom = ST_SetSRID(ST_MakePoint(lon, lat), 4326);
Vince
20.5k16 gold badges49 silver badges65 bronze badges
answered Mar 28, 2020 at 18:45
1
  • 3
    This is the solution to a different problem. The OP is trying populate lon and lat from a geom, though it isn't clear if the geometery is a "WKT" text string of just the presentation of a geometry as WKT from the client. Either way, the solution would make use of ST_X() and ST_Y(), not ST_MakePoint(). Commented Mar 29, 2020 at 0:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.