postgis 是一个很强大的空间数据库,最近查看很多资料和文档,为了解决空间边界自相加问题。
知乎有一篇写到处理这类函数,但是结果可能会出现自相交部分出现镂空,这并不是我想要的。
知乎直通车
这里先感谢公司一位大佬提供的解决方案,这里为了表达敬仰,大伙一起隔空喊一声 :帆哥,牛逼
相信看到这懂的都懂了,是不是很兴奋 :“我感觉到了,一切都回来了,大师兄~”
如图:左边的是自相交图形, 右边的是正常图形
问题:当图形格式为集合类型(FeatureCollection),该数据集中存在N个多多边形,其中又包含自相交图形,
如何得到一个叠加后正确且完整图形?
直接上答案:
复制粘贴以下SQL格式,替换GeoJSON内容即可:
SELECT ST_Union(t.geom) FROM (
WITH data AS (SELECT
#{这里放GeoJSON格式数据}::json AS fc)
SELECT
row_number() OVER () AS gid,
ST_MakeValid(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM DATA
) AS f
) AS t
解题思路:
上方图形对应数据(GeoJSON完整格式)
这里提供一个地图绘制工具非常好用 GeoJSON 来绘制图形
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[120.52757263183594, 31.495432803134843],
[120.59211730957031, 31.31199502365151],
[120.89012145996094, 31.35950051982242],
[120.74729919433594, 31.283245492650792],
[120.52757263183594, 31.495432803134843]
]
]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[120.74386596679688, 31.487235582017444],
[120.84548950195312, 31.269747790889888],
[121.01783752441405, 31.378261512889125],
[120.91827392578125, 31.487821121636433],
[120.74386596679688, 31.487235582017444]
]
]
}
}]
}
1.先从GeoJSON格式中,解析出FeatureCollection中geometry部分(PostGIS可以识别的数据格式)
WITH data AS (SELECT
'{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[120.52757263183594, 31.495432803134843],
[120.59211730957031, 31.31199502365151],
[120.89012145996094, 31.35950051982242],
[120.74729919433594, 31.283245492650792],
[120.52757263183594, 31.495432803134843]
]
]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[120.74386596679688, 31.487235582017444],
[120.84548950195312, 31.269747790889888],
[121.01783752441405, 31.378261512889125],
[120.91827392578125, 31.487821121636433],
[120.74386596679688, 31.487235582017444]
]
]
}
}]
}'::json AS fc)
SELECT
row_number() OVER () AS gid,
ST_MakeValid(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM DATA
) AS f
查询结果:
2.在以上基础上,通过ST_MakeValid将自相交图形合法化,再通过ST_Union合并出最终图形
最终SQL如下:
SELECT ST_Union(t.geom) FROM (
WITH data AS (SELECT
'{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[120.52757263183594, 31.495432803134843],
[120.59211730957031, 31.31199502365151],
[120.89012145996094, 31.35950051982242],
[120.74729919433594, 31.283245492650792],
[120.52757263183594, 31.495432803134843]
]
]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[120.74386596679688, 31.487235582017444],
[120.84548950195312, 31.269747790889888],
[121.01783752441405, 31.378261512889125],
[120.91827392578125, 31.487821121636433],
[120.74386596679688, 31.487235582017444]
]
]
}
}]
}'::json AS fc)
SELECT
row_number() OVER () AS gid,
ST_MakeValid(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM DATA
) AS f
) AS t
==============================华丽的分割线 ================================
今天再补充一个解决方案:针对前段使用高德地图,传了一个自定义一种type类型是MultiPolygon的geojson 格式,
对此改进了一下sql的写法。
直接上答案:
复制粘贴以下SQL格式,替换GeoJSON内容即可:
这里做了一些修改,里面的用到了ST_Dump
SELECT St_Union(result)
FROM (
WITH data AS (
SELECT
ST_GeomFromGeoJSON(#{这里放前端自定义的GeoJSON格式数据})
AS the_geom)
SELECT ST_MakeValid((ST_Dump(the_geom)).geom) AS result FROM DATA
) t
上图geojson数据结构:
SELECT St_Union(result)
FROM (
WITH data AS (
SELECT
ST_GeomFromGeoJSON(
'{
"type": "MultiPolygon",
"coordinates": [
[
[
[120.859146, 31.478452],
[120.854339, 31.363018],
[120.958023, 31.400535],
[120.942917, 31.480208],
[120.859146, 31.478452]
]
],
[
[
[120.8255, 31.412842],
[120.787735, 31.437451],
[120.820694, 31.334871],
[120.901718, 31.366536],
[120.756149, 31.404637],
[120.8255, 31.412842]
]
]
]
}')
AS the_geom)
SELECT ST_MakeValid((ST_Dump(the_geom)).geom) AS result FROM DATA
) t
ST_Dump非常方便,可以将MULTI几何图形扩展为单个几何图形。
ST_MakeValid 该函数尝试创建给定无效几何的有效表示形式而不会丢失任何输入顶点,
已返回有效的几何图形,无需进一步干预。
搞完收工,两种方法原理是一样的,都是先解析数据,将不合法的结构打散在做union合并
欢迎各路豪杰踊跃发言,共同进步!有更好的处理方法还请不吝赐教。