简体   繁体   English

在Postgis中获取以公里为单位的距离

[英]Get distance in km in Postgis

I have 2 tables: City and River. 我有2张桌子:City和River。

City: ID, city_name, the_geom (poin)
River: id, name, the_geom (multiline)

For each river I want to know the city farthest away and its distance in km. 对于每条河,我想知道最远的城市及其距离(以公里为单位)。

For example... I have the query 例如...我有查询

Select city1.city_name, st_distance(city1.the_geom, river1.the_geom)
from city city1, river river1
where 
river1.name = 'Albany' 
order by st_distance(city1.the_geom, river1.the_geom) desc
limit 1

In this case, I get the city farthest away from the Albany River. 在这种情况下,我使城市离奥尔巴尼河最远。 Is this the best way to query? 这是查询的最佳方法吗?

I get this result: 我得到这个结果:

City_name; distance
"Topeka";  13.2534798131185

But I don't know if the result is in km... If it isn't... How can I get the result in km?? 但是我不知道结果是否以公里为单位...如果不是...我如何以公里为单位获得结果?

The units returned by st_distance are in spatial ref units - assuming you are using lat/lon (EPSG 4326) those units will be decimal degrees. st_distance返回的单位是空间参考单位-假设您使用的是经度/纬度(EPSG 4326),则这些单位将是十进制度。

You have two choices - project to a suitable coordinate system in metres, or use the geography type. 您有两种选择-以米为单位投影到合适的坐标系,或使用地理类型。

Here is your query using geography: 这是您使用地理位置的查询:

SELECT city1.city_name, 

 st_distance(city1.the_geom::geography, river1.the_geom::geography) /1000 -- convert m to km

  FROM city city1, river river1
  WHERE
         river1.name = 'Albany' 
  ORDER by st_distance(city1.the_geom::geography, river1.the_geom::geography) desc 
  LIMIT 1

(Note: I am not sure of the value of casting to geography in the order by statement). (注意:我不确定按语句顺序进行地理转换的价值)。

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

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