我想在postgis文件中显示小于10,000米及其计算的距离 [英] I want to display in postgis files that are less than 10,000meter and their calculated distance

查看:13
本文介绍了我想在postgis文件中显示小于10,000米及其计算的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的查询没有优化。

我要对距某个点一定距离的文件(每个文件包含1个长和1个长)进行排序,并显示文件与给定点之间距离的计算。

 SELECT  *, ST_DistanceSphere( st_point(lon, lat) , st_point(49.9, 6.7)) as result  from data
  where
  ST_DistanceSphere( st_point(lon, lat) , st_point(49.9 ,6.7)) < 10000

所以我想检索距离小于10,000米的点的列表,并且结果列包含计算出的距离。 返回结果的时间超过1秒。

在未优化后,或者在Postgis中,有另一种方法可以更快地获得答案...

您能帮我优化此查询吗?

我的桌子:

        drop table if exists data cascade;
        CREATE TABLE data(
idSERIAL PRIMARY KEY,
        lon float,
        lat float,
      
        );

谢谢

推荐答案

最简单、最快捷的方法是将坐标存储为geometrygeography,而不是单独的坐标对:

ALTER TABLE data ADD COLUMN geog geography (point,4326);
UPDATE data SET geog = ST_MakePoint(lon,lat);

之后,在此新列上创建一个Gist索引:

CREATE INDEX idx_data_point ON data USING gist (geog) ;

使用ST_DWithin查询距离,因为ST_DistanceSphereST_Distance不会使用空间索引!

SELECT ST_Distance(geog,ST_MakePoint(49.9 ,6.7)::geography,true)
FROM data
WHERE ST_DWithin(geog,ST_MakePoint(49.9,6.7),10000,true); 
  • 注意:ST_DWithinST_Distance函数中的true表示USE_SERSOLID=TRUE,这是geography参数的默认值。

演示:db<>fiddle

另见:Getting all Buildings in range of 5 miles from specified coordinates

这篇关于我想在postgis文件中显示小于10,000米及其计算的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆