我是PostGIS的新手,正在尝试创建半径查询。我有一个带有geometry
字段(位置)的表,并且在单独的字段上也有纬度和经度值。
我正试图在距拉特(lat):40.753777,lon(-73.981568)半径10公里处寻找点。
具有:
SELECT postcode, lat, lon, st_asgeojson(position) geojson, ST_Distance(ST_MakePoint(lat, lon), ST_MakePoint(40.753777, -73.981568)) distance FROM addresses WHERE ST_DWithin(ST_MakePoint(lat, lon), ST_MakePoint(40.753777, -73.981568), 10000) order by id limit 10;
结果给了我很远的一点。使用lat和lon进行的地球距离相同的查询直接为我提供了更接近的结果。
SELECT postcode, lat, lon, st_asgeojson(position) geojson FROM addresses WHERE earth_box(ll_to_earth(40.753777, -73.981568), 10000) @> ll_to_earth(addresses.lat, addresses.lon) order by id limit 10;
但是我真的也不知道这是否正确,PostGIS查询怎么了?
答案 0 :(得分:0)
一些注意事项:
geometry ST_MakePoint(double precision x, double precision y); Note: x is longitude and y is latitude
所以应该是ST_MakePoint(-73.981568,40.753777)。
SELECT postcode, lat, lon, st_asgeojson(position) AS geojson, ST_Distance_Spheroid( position, ST_GeomFromText('POINT(-73.981568 40.753777)', 4326), 'SPHEROID["WGS 84",6378137,298.257223563]' ) as distance FROM addresses WHERE distance < 10000 LIMIT 10;
position
中添加一个新的geographic类型列:ALTER TABLE addresses ADD COLUMN position_geog geography(Point,4326); UPDATE addresses SET position_geog = position::geography; CREATE INDEX ON addresses USING gist(position_geog); -- and now you can use ST_DWITHIN with meters...