Search code examples
pythonsqlpostgresqlpostgis

How to add 2 points with distance between them (SRID = 32636)?


I'm using python 3.6 and postgres DB

I have a table name: test_table with the following fields: name (text), geo (geometry)

the srid of geo field is 32636

I want to write a python function which get point X (wkt) and return point Y with distance of 10 meters between the points. i.e:

The X and Y are in WKT format

How can I calculate the point Y with X is the input ?

It seems that I can't use euclidean distance , because the srid is 32636

so how can I do it ?


Solution

  • You could cast your geometry to geography and ST_Project it (in the azimuth you want). Doing so you can easily provide the distance in meters:

    CREATE TEMPORARY TABLE test_table (name text, geo geometry(point,(32636)));
    INSERT INTO test_table VALUES ('foo','SRID=32636;POINT(2076155.32235105 4828109.18280588)');
    
    SELECT 
     ST_AsText(
      ST_Transform(
       ST_Project(
         ST_Transform(geo,4326)::geography,10,radians(45.0))::geometry,
       32636)
     )
    FROM test_table;
    
                    st_astext                 
    ------------------------------------------
     POINT(2076150.11319696 4828116.26815917)
    (1 Zeile)
    

    You can check the distance using ST_Distance:

    SELECT 
      ST_Distance(
        ST_Transform(geo,4326)::geography,
        ST_Project(ST_Transform(geo,4326)::geography,10,radians(45.0))::geometry )
    FROM test_table;
    
    st_distance 
    -------------
              10
    

    NOTE: I'm using ST_Transform to get from your projected SRS to a lon/lat SRS, so that we can cast it to geography, otherwise we'd get an error:

    SELECT geo::geography FROM test_table;
    
    ERROR:  Only lon/lat coordinate systems are supported in geography.
    

    Further reading: Caculate point 50 miles away (North, 45% NE, 45% SW)