Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting lat-long to PostGIS geometry without querying the database

I have a table in postgresql with a PostGIS geometry(point, 4326) column (location, using SRID 4326) and I have a Python application that using SQL Alchemy updates the table (the rest of the columns) without any problem.

Now, I need to update the location column and I know I can use the proper text representation of a given location to update the column using SQL Alchemy without the need to use GEOAlchemy, for instance I can update the column with the value: '0101000020E6100000AEAC7EB61F835DC0241CC418A2F74040'

which corresponds to lat:33.9346343 long:-118.0488106

The question is: is there a way to compute in Python this '0101000020E6100000AEAC7EB61F835DC0241CC418A2F74040' having this (33.9346343 ,-118.0488106) as an input without querying the database? or any way to update the column using a proper text input?

I know I can use SQLAlchemy to execute this query:

select st_setsrid(st_makepoint(-118.0488106, 33.9346343),4326)

and obtain the value to update the column, but I want to avoid that.

Thanks in advance!

like image 275
Sergio Ayestarán Avatar asked Mar 07 '13 16:03

Sergio Ayestarán


People also ask

What is the difference between geometry and geography data in PostGIS?

PostGIS gives you a choice of two different ways to store geospatial data: Geometry, where it assumes all of your data lives on a Cartesian plane (like a map projection); Geography, where it assumes that your data is made up of points on the earth's surface, as specified by latitudes and longitudes.

What is geometry in PostGIS?

geometry is a fundamental PostGIS spatial data type used to represent a feature in planar (Euclidean) coordinate systems. All spatial operations on geometry use the units of the Spatial Reference System the geometry is in.

What is Srid PostGIS?

Basically, PostGIS opens up the ability to store your data in a single coordinate system such as WGS84 (SRID 4326), and when you need something like Area, Distance, or Length, you use a function to create that column from your data in a projected coordinate system that will give you a local interpretation of your data ...

What is Wkb_geometry?

The well-known binary representation for OGC geometry (WKBgeometry) provides a portable representation of a geometry value as a contiguous stream of bytes. It permits geometry values to be exchanged between an ODBC client and a database in binary form.


1 Answers

The solution to this problem is rather easier than it seems. To update the field using text and the input lat-long all I needed to do was defining the SRID in the text assign:

location = 'SRID=4326;POINT(-118.0488106 33.9346343)'

This will update the geometry(point,4326) column properly and when you do a select in the table the value of the column is the expected one:

"0101000020E6100000AEAC7EB61F835DC0241CC418A2F74040"

Thanks guys!

like image 119
Sergio Ayestarán Avatar answered Oct 09 '22 10:10

Sergio Ayestarán