PostGIS 查询某点周围指定范围内的兴趣点

本文介绍如何使用PostGIS在数据库中查询给定点周围特定距离内的学校,包括使用Geometry和Geography类型的区别,以及在中国地区选择合适坐标系的方法。

问题描述

在我们生活中,想必很多人都使用过一个功能就是查找附近XX米内的美食、景点等信息。那么该功能是如何实现的呢?本文将带你一探究竟。
具体问题:查询给定点(如:113.678 34.796)周围1000米内的学校。

数据库

PostgresSQL/PostGIS

主要方法

boolean ST_DWithin(geometry g1, geometry g2, double precision distance_of_srid);
boolean ST_DWithin(geography gg1, geography gg2, double precision distance_meters, boolean use_spheroid);

官方说明是:

Returns true if the geometries are within the specified distance of one another.
For geometry: The distance is specified in units defined by the spatial reference system of the geometries. For this function to make sense, the source geometries must both be of the same coordinate projection, having the same SRID.
For geography units are in meters and measurement is defaulted to use_spheroid=true, for faster check, use_spheroid=false to measure along sphere.

翻译过来大致是说:
(1)对于geometry第三个距离参数单位和数据的坐标系有关,如果数据使用的是地理坐标系,则距离单位是度。
(2)对于geography第三个距离参数单位是米。

Geometry与Geography

GIS坐标系简介

GIS中坐标系统有两种,一种是球面坐标,也叫地理坐标;一种是平面坐标,也叫投影坐标。在应用时,球面坐标通常用于计算,平面坐标通常用于展示(当然也可以用于计算)。
投影坐标是从球坐标投影后展开得来(用一个圆柱将地球包起来,把地球当成会发光的光源,投影后,将圆柱展开得到),投影的范围越大,精度就越低。范围越小,精度就越高。除了投影扇形的大小有区别,在不同的行业,也有不同的坐标系,例如用于测绘的坐标系。

Geometry与Geography支持的坐标系

Geometry支持地理坐标和平面坐标,Geometry支持更多的函数,一些几何计算的代价更低;
Geography仅支持地理坐标,Geography支持的函数略少,计算代价更高。

那为什么还需要Geography呢,请看官方给出的解释:

4.2.2. When to use Geography Data type over Geometry data type    
    
The geography type allows you to store data in longitude/latitude coordinates,     
but at a cost: there are fewer functions defined on GEOGRAPHY than there are on GEOMETRY;     
those functions that are defined take more CPU time to execute.    
    
The type you choose should be conditioned on the expected working area of the application you are building.     
Will your data span the globe or a large continental area, or is it local to a state, county or municipality?    
    
If your data is contained in a small area, you might find that choosing an appropriate     
projection and using GEOMETRY is the best solution, in terms of performance and functionality available.    
    
If your data is global or covers a continental region, you may find that GEOGRAPHY     
allows you to build a system without having to worry about projection details.     
You store your data in longitude/latitude, and use the functions that have been defined on GEOGRAPHY.    
    
If you don't understand projections, and you don't want to learn about them,     
and you're prepared to accept the limitations in functionality available in GEOGRAPHY,     
then it might be easier for you to use GEOGRAPHY than GEOMETRY.     
Simply load your data up as longitude/latitude and go from there.      
    
Refer to Section 14.11, “PostGIS Function Support Matrix” for compare between     
what is supported for Geography vs. Geometry.     
For a brief listing and description of Geography functions,     
refer to Section 14.4, “PostGIS Geography Support Functions”   

完整示例

示例1:使用Geometry

SELECT *
FROM school_table t
WHERE ST_DWithin(ST_Transform(ST_GeomFromText('POINT(113.678425 34.796591666)',4326),XXXX),ST_Transform(t.geom,XXXX),1000)

说明:语句中“XXXX”,需根据项目的精度要求,选择不同的投影以及所在的条度带。该方法适合小区域范围的查询,如果区域范围横跨两个条度带的时候,查出的内容会出错。
解决方法:在精度要求不高的时候,可使用WGS_1984_World_Mercator投影。
示例2:使用Geography

SELECT *
FROM school_table t
WHERE ST_DWithin(ST_GeographyFromText('SRI
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值