9

我有下表

create table places(lat_lng point, place_name varchar(50));

insert into places values (POINT(-126.4, 45.32), 'Food Bar');

获取所有接近特定纬度/经度的地方的查询应该是什么?

gis 已安装。

4

3 回答 3

10

如果你真的想使用 PostGIS:

create table places(
    lat_lng geography(Point,4326),
    place_name varchar(50)
);

-- Two ways to make a geography point
insert into places values (ST_MakePoint(-126.4, 45.32), 'Food Bar1');
insert into places values ('POINT(-126.4 45.32)', 'Food Bar2');

-- Spatial index
create index places_lat_lng_idx on places using gist(lat_lng);

现在查找 1 公里(或 1000 米)内的所有地点:

select *, ST_Distance(lat_lng, ST_MakePoint(-126.4, 45.32)::geography)
from places
where ST_DWithin(lat_lng, ST_MakePoint(-126.4, 45.32)::geography, 1000)
order by ST_Distance(lat_lng, ST_MakePoint(-126.4, 45.32)::geography);
于 2012-10-02T04:37:31.280 回答
9
select *
from places
where lat_lng <-> POINT(-125.4, 46.32) < 1
order by lat_lng <-> POINT(-125.4, 46.32)
于 2012-09-28T18:09:29.863 回答
0

在位置字段上创建索引:

CREATE INDEX ON table_name USING GIST(location);

GiST 索引能够优化“最近邻”搜索:

SELECT * FROM table_name ORDER BY location <-> point '(-74.013, 40.711)' LIMIT 10;

注意:点的第一个元素是经度,第二个元素是纬度。

于 2020-09-10T12:02:41.680 回答