PostGIS之几何创建函数
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';
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';
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';
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) ));
3. 参考资料
[1]20. Geometry Constructing Functions — Introduction to PostGIS
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具