1. 概述

PostGIS 是PostgreSQL数据库一个空间数据库扩展,它添加了对地理对象的支持,允许在 SQL 中运行空间查询

PostGIS官网:About PostGIS | PostGIS

PostGIS官方教程:PostGIS 简介 — Introduction to PostGIS

PostGIS相关教程:文章目录汇总 - 知乎 (zhihu.com)

本文基于官方教程描述PostGIS中的几何创建函数

数据准备可参考:

数据介绍可参考:

2. 几何创建函数

几何创建函数,即输入Geometry进行空间分析再输出Geometry,典型的有缓冲分析、叠加分析、求交等

2.1 质心

求几何体的质心,主要使用到的函数为:

  • ST_Centroid Returns the geometric center of a geometry
  • ST_PointOnSurface(g1) Computes a point guaranteed to lie in a polygon, or on a geometry

例如,求Financial District社区的质心:

SELECT ST_Centroid(geom), ST_PointOnSurface(geom) 
FROM nyc_neighborhoods
WHERE name = 'Financial District';

image-20230105161835757

2.2 缓冲区

对输入的几何体求缓冲区,主要使用到的函数有:

  • ST_Buffer(g1, radius_of_buffer, buffer_style_parameters = '') Computes a geometry covering all points within a given distance from a geometry

例如,求Financial District的10米缓冲区:

SELECT ST_Buffer(geom, 10) 
FROM nyc_neighborhoods
WHERE name = 'Financial District';

image-20230105162442053

2.3 求交

对输入的几何体求交集,主要使用到的函数有:

  • ST_Intersects(geomA, geomB) Tests if two geometries intersect (they have at least one point in common)

例如,求Financial District社区与Broad St公交站的交集:

SELECT ST_AsText(ST_Intersection(geom, 
                     (SELECT geom FROM nyc_neighborhoods 
                     WHERE name = 'Financial District')
					)) 
FROM nyc_subway_stations 
WHERE name = 'Broad St';

image-20230105164348801

2.4 求并

对输入的几何体求并集,主要使用到的函数有:

  • ST_Union(g1, g2) Computes a geometry representing the point-set union of the input geometries

例如,求两个圆的并集:

-- 对两个点进行缓冲操作得到圆
SELECT ST_AsText(ST_Union(
  ST_Buffer('POINT(0 0)', 2),
  ST_Buffer('POINT(3 0)', 2)
));

image-20230105164752718

3. 参考资料

[1]20. Geometry Constructing Functions — Introduction to PostGIS

[2]PostGIS教程十三:几何图形创建函数 - 知乎 (zhihu.com)

[3]PostGIS 3.3.3dev Manual

[4]PostGIS Cheat Sheet