Nearest places from a certain point

不羁岁月 提交于 2019-12-03 06:02:43

问题


I have the following table

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

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

What should be the query to get all places close to particular lat/long?

gis is installed.


回答1:


If you actually wanted to use 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);

Now to find all of the places within 1 km (or 1000 m):

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);



回答2:


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


来源:https://stackoverflow.com/questions/12644988/nearest-places-from-a-certain-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!