从某点最近的地方(Nearest places from a certain point)

2019-08-01 00:31发布

我有以下表

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

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

应该是什么查询亲近特别是经/纬所有的地方?

GIS安装。

Answer 1:

如果你确实想使用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);


Answer 2:

select *
from places
where lat_lng <-> POINT(-125.4, 46.32) < 1
order by lat_lng <-> POINT(-125.4, 46.32)


文章来源: Nearest places from a certain point