【发布时间】:2012-04-12 16:01:16
【问题描述】:
这是我第一次创建 GIS 查询。在我的数据库的一个表中,有一列带有点类型。每条记录是一台 ATM 机。我想写一个查询来获取我所在位置附近1公里范围内的ATM机。如何在SQL查询中使用ST_DWithin来查找记录?
【问题讨论】:
这是我第一次创建 GIS 查询。在我的数据库的一个表中,有一列带有点类型。每条记录是一台 ATM 机。我想写一个查询来获取我所在位置附近1公里范围内的ATM机。如何在SQL查询中使用ST_DWithin来查找记录?
【问题讨论】:
SELECT *
FROM atm_finder
WHERE ST_Distance(ST_Transform(ST_GeomFromText('POINT([Lon] [Lat])',4326),26986),ST_Transform(location,26986)) <= 1000
Where [Lon] & [Lat] - 点的 GPS 坐标。但就你第一次使用 POINT 类型而言:
SELECT AddGeometryColumn('atm_finder', 'location', 4326, 'POINT', 2);
当然,在此之前,您应该重命名字段“位置”(为了不丢失数据)并用这些数据填充新字段。
【讨论】:
SELECT ST_GeomFromText(location) FROM atm_finder 对其进行测试。所以你会看到之前的操作是否正确。
CREATE TABLE atm_finder (id integer NOT NULL, CONSTRAINT atm_finder_pkey PRIMARY KEY (id)); SELECT AddGeometryColumn('atm_finder', 'location', 4326, 'POINT', 2); INSERT INTO atm_finder (id, location) VALUES (1, ST_GeomFromText('POINT(-117.157305 32.715738)', 4326)), (2, ST_GeomFromText('POINT(-117.15046 32.715793)', 4326)), (3, ST_GeomFromText('POINT(-117.130204 32.726624)', 4326)); SELECT id FROM atm_finder WHERE ST_Distance(ST_Transform(ST_GeomFromText('POINT(-117.147605 32.715802)',4326),26986), ST_Transform(location,26986)) <= 1000; 别忘了 push + :)
我不太明白 ST_DWithin 可以如何与两点一起使用...
类似的东西。
select atm.id
from atm_finder atm
where ST_Distance(<your location>, atm.location) <= 1000
//1000 = meters, works fine with geography types
//have to check your projection with geometry types
顺便说一句,你的表的创建方式对我来说看起来很奇怪......
【讨论】: