简体   繁体   English

Postgres - 使用 postgis 计算距离

[英]Postgres - Calculate distance with postgis

After looking for days, and try all what I find, I'm here to ask how to calculate the distance beetwen two points on Postgres with PostGis.在寻找了几天并尝试了所有我发现的东西之后,我在这里询问如何使用 PostGis 计算 Postgres 上两点之间的距离。 I got a table called location.我有一张名为 location 的表。 This table got a column "coordenate" of type point.该表有一个点类型的列“coordenate”。 When the user inserts a value on the application I need to get the locations ordered by the closes distance.当用户在应用程序中插入一个值时,我需要获取按关闭距离排序的位置。 I know I need to use ST_Distance, but everytime I try to cast the POINT of coordenate I can't.我知道我需要使用 ST_Distance,但每次我尝试投射坐标点时我都做不到。 I need the result in Km.我需要以公里为单位的结果。

I try:我尝试:

SELECT ST_Distance('POINT(0.0 0.0)', ST_GeomFromText(location.coordenate)) FROM app.location as location;

To get distances in metres/kilometres you need to either transform your coordinates to a SRS that has metre as unit or, if possible, use geography instead of geometry , as ST_Distance returns the distance of two geography parameters in metres (1km = 1000m), eg要获得以米/公里为单位的距离,您需要将坐标转换为以米为单位的 SRS,或者如果可能,使用geography而不是geometry ,因为ST_Distance返回两个以米为单位的geography参数的距离(1km = 1000m),例如

SELECT 
  ST_Distance(ST_MakePoint(0.0,0.0)::geography, coordenate::geography)/1000 
FROM app.location;

Casting geometry / text to geographygeometry / text转换为geography

Demo: db<>fiddle演示: db<>fiddle

CREATE TABLE location (gid int, coordenate geometry(point,4326));
INSERT INTO location VALUES
(1,'SRID=4326;POINT(10 10)'),(2,'SRID=4326;POINT(0.1 0.1)');

SELECT 
  gid, ST_AsText(coordenate),
  ST_Distance(ST_MakePoint(0.0,0.0)::geography, coordenate::geography)/1000  
FROM location
ORDER BY coordenate::geography <-> ST_MakePoint(0.0,0.0)::geography;

gid |   st_astext    |      ?column?      
-----+----------------+--------------------
   2 | POINT(0.1 0.1) | 15.690343289660001
   1 | POINT(10 10)   | 1565.1090992178902
(2 rows)

The operator <-> means distance, so using it on the ORDER BY clause you can order the result set by distance.运算符<->表示距离,因此在ORDER BY子句上使用它可以按距离对结果集进行排序。

Casting point to geographypointgeography

The data type point is not a PostGIS data type, but a geometric data type from PostgreSQL. In order to use ST_Distance you have to cast the points to either geometry or geography.数据类型point不是PostGIS 数据类型,而是来自 PostgreSQL 的geometric data type 。为了使用ST_Distance ,您必须将点转换为几何或地理。

Demo: db<>fiddle演示: db<>fiddle

CREATE TABLE location (gid int, coordenate point);
INSERT INTO location VALUES
(1,point(10,10)),(2,point(0.1,0.1));

SELECT *,
  ST_Distance(
    ST_MakePoint(0.0,0.0)::geography, 
    ST_MakePoint(coordenate[0],coordenate[1])::geography)/1000 
FROM location
ORDER BY ST_MakePoint(coordenate[0],coordenate[1])::geography <-> ST_MakePoint(0.0,0.0)::geography;

 gid | coordenate |      ?column?      
-----+------------+--------------------
   2 | (0.1,0.1)  | 15.690343289660001
   1 | (10,10)    | 1565.1090992178902
(2 rows)

Further reading:延伸阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM