postgis常用操作手册

查询所有函数:

SELECT * FROM pg_proc;

更新坐标系st_setsrid,查看坐标系:st_srid

创建空间索引:

CREATE INDEX [indexname] ON [tablename] USING GIST ( [geometryfield] );
example:create index idx_poi5_geom on poi5 using gist(geom);

使用空间查询:
select count(*) from poi5 a,poi5 b
where a.pid='20080243019'
and st_within(b.geom,st_buffer(a.geom,0.001));
执行计划:

如果不使用空间索引执行计划:

两个geometry之间关系:within、disjoint、intersects、union、intersection,difference

St_within(geom A,geom B)返回A是否处于B中
St_disjoint(geom A,geom B)返回A是否不在B中
St_intersects(geom A,geom B)返回A是否和B有接触
St_union(geom A,geom B)返回A+B两个几何的合并
St_intersection(geom A,geom B)返回A和B的交集
St_difference(geom A,geom B)返回A与B不相交的部分几何
select
st_difference(
st_buffer(
st_geomfromtext('Point(116 39)'),0.7),
st_buffer(
st_geomfromtext('Point(117 39)'),0.7)) geom

  

判断几何是否空:st_isempty(geom A)

几何类型转换:

wkt转geometry:st_geomfromtext(wkt)
select st_geomfromtext('Point(122 33)')
geometry转wkt:st_astext(geometry)
select st_astext(st_geomfromtext('Point(122 33)'))
geometry转geojson:st_asgeojson(geometry)
select st_asgeojson(st_geomfromtext('Point(122 33)'))
geojson转geometry:st_geomfromgeojson(geojson)
select 
st_geomfromgeojson(
st_asgeojson(st_geomfromtext('Point(122 33)')))
geometry转geohash:st_geohash(geometry)
select st_geohash(st_geomfromtext('Point(116 39)'))
geohash转geometry:st_geomfromgeohash
select st_geomfromgeohash('wwfmzesx7yvjugxr3nzv')

  

获取几何信息:

得到几何类型:st_geometrytype(geometry A)
根据经纬度,获取两点距离(单位:米):st_distance_sphere(point a,point b)
select
st_distance_sphere(st_geomfromtext('Point(116 39)'),
st_geomfromtext('Point(117 39)'))

  

如果两个几何在指定范围内,则返回true,否则false:st_dwithin(geom A,geom B)

select count(*) from poi5 a,poi5 b
where a.pid='20080243019'
and st_dwithin(b.geom,a.geom,0.001);

  

原文地址:https://www.cnblogs.com/lilei2blog/p/7814936.html