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

£可爱£侵袭症+ 提交于 2021-01-28 11:36:08

问题


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 ?


回答1:


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)



来源:https://stackoverflow.com/questions/64012333/how-to-add-2-points-with-distance-between-them-srid-32636

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!