postgis常用的函数

postgis常用的函数

常见函数http://postgis.net/docs/reference.html

 

ST_GeometryType(geometry)    ——    返回几何图形的类型

ST_NDims(geometry)    ——    返回几何图形的维数

ST_SRID(geometry)    ——    返回几何图形的空间参考标识码

点(Points)

ST_X(geometry)    ——    返回X坐标

ST_Y(geometry)    ——    返回Y坐标

线串(Linestring)

ST_Length(geometry)    ——    返回线串的长度

ST_StartPoint(geometry)    ——    将线串的第一个坐标作为点返回

ST_EndPoint(geometry)    ——    将线串的最后一个坐标作为点返回

ST_NPoints(geometry)    ——    返回线串的坐标数量

多边形(Polygon)

ST_Area(geometry)    ——     返回多边形的面积

ST_NRings(geometry)    ——    返回多边形中环的数量(通常为1个,其他是孔)

ST_ExteriorRing(geometry)    ——    以线串的形式返回多边形最外面的环

ST_InteriorRingN(geometry, n)    ——    以线串形式返回指定的内部环

ST_Perimeter(geometry)    ——    返回所有环的长度

集合(Collection)

ST_NumGeometries(geometry)    ——    返回集合中的组成部分的数量

ST_GeometryN(geometry, n)    ——    返回集合中指定的组成部分

ST_Area(geometry)    ——    返回集合中所有多边形组成部分的总面积

ST_Length(geometry)    ——    返回所有线段组成部分的总长度

几何图形输入和输出

 ①Well-known text(WKT

  • ST_GeomFromText(text, srid)    ——    返回geometry
  • ST_AsText(geometry)    ——    返回text
  • ST_AsEWKT(geometry)    ——    返回text

    ②Well-known binary(WKB

  • ST_GeomFromWKB(bytea)    ——    返回geometry
  • ST_AsBinary(geometry)    ——    返回bytea
  • ST_AsEWKB(geometry)    ——    返回bytea

    ③Geographic Mark-up Language(GML

  • ST_GeomFromGML(text)    ——    返回geometry
  • ST_ASGML(geometry)    ——    返回text

    ④Keyhole Mark-up Language(KML

  • ST_GeomFromKML(text)    ——    返回geometry
  • ST_ASKML(geometry)    ——     返回text

    ⑤GeoJson

  • ST_AsGeoJSON(geometry)    ——    返回text

    ⑥Scalable Vector Graphics(SVG

  • ST_AsSVG(geometry)    ——    返回text

postgis常用操作手册

查询所有函数:

SELECT * FROM pg_proc;

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

创建空间索引:

1

2

3

4

5

6

7

8

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

1

2

3

4

5

6

7

8

9

10

11

12

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)

几何类型转换:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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')

  

获取几何信息:

1

2

3

4

5

得到几何类型: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)

1

2

3

select count(*) from poi5 a,poi5 b

where a.pid='20080243019'

and st_dwithin(b.geom,a.geom,0.001);

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值