Postgresql 地理位置操作

来源:互联网 发布:c语言嵌入式汇编 编辑:程序博客网 时间:2024/05/29 14:31

(longitude:经度 latitude:纬度 radius:范围/m)

insert/update 地理位置:

例:(业务为:上传车的位置,更改driver_status表中的curr_loc(类型:geometry(Point,4326))字段,实时记录车当前位置)
st_geomfromtext('point(longitude latitude)',4326);

update driver_status set curr_loc = st_geomfromtext('point(113.22 115.25)',4326) where driver_id=4

【注:这里 不要使用 ?注入的方式填写 经纬度,通过字符串拼接的方式】
另外,还可以通过这种方法直接注入对象:
核心代码和引用包:
import org.postgis.Point;import org.postgis.PGgeometry;long timestamp = System.currentTimeMillis();String sql = "update driver_status set curr_loc = ? where driver_id=4";Point point = new Point(longitude,latitude);point.setSrid(4326);PGgeometry geometry = new PGgeometry();geometry.setGeometry(point);statement.setString(1,geometry);

select 位置范围:

ST_DWithin(curr_loc, ST_GeomFromText('POINT(longitude latitude)', 4326)::geography, radius)
例:(业务为:查询坐标点
radius米范围内所有的车,"curr_loc"为car表的坐标字段)
select * from car where ST_DWithin(curr_loc, ST_GeomFromText('POINT(? ?)', 4326)::geography,
radius)

order by根据距离排序:

order by origin_loc <-> ST_GeomFromText('POINT(longitude latitude)', 4326) asc
例:(业务为:查询中心坐标点周围1公里内,且时间在2分钟之内 的数据,按据中心点距离升序)
select * from reserve where ST_DWithin(origin_loc, ST_GeomFromText('POINT(? ?)', 4326), 1) and
reserve_time+'2 minutes' >= now() order by origin_loc <-> ST_GeomFromText('POINT(? ?)', 4326)



原创粉丝点击