我在处理澳大利亚人口普查收集形状时遇到问题 由澳大利亚统计局以 MapInfo 格式提供。我使用 ogr2ogr 工具将它们加载到 PostGIS 数据库中,该工具适用于大多数形状,但不是全部。
我面临的问题的一个简单示例是这样的查询(需要加载 NSW 数据集):
SELECT st_union(wkb_geometry) FROM cd06answ WHERE cd_code_2006 LIKE '1291%'
该查询的结果不是预期的形状而是 NULL。
表中没有空值,但存在无效的几何图形。例如,
SELECT cd_code_2006 FROM cd06answ
WHERE cd_code_2006 LIKE '1291%' AND NOT st_isvalid(wkb_geometry)
检索值“1291301”和“1291321”。如果我排除无效的几何图形,则 st_union 会成功。
将 Quantum GIS 连接到数据库可以渲染相关的两种形状。它们应该是几何并集的一部分,所以我需要以某种方式解决这个问题。
有没有更好的方法将 MapInfo 数据加载到 PostGIS 中?或者一些修复 PostGIS 内部数据的方法?既然数据库数据呈现正常,应该可以保存它,不是吗?
编辑:根据 Christophe 的反馈,我对 st_buffer 和 st_snaptogrid 进行了更多实验。此查询的结果:
SELECT
cd_code_2006,
st_isvalid(st_buffer(wkb_geometry,0)),
st_isvalid(st_snaptogrid(wkb_geometry, 0.00000001)),
st_isvalid(st_snaptogrid(wkb_geometry, 0.0000001))
FROM
cd06answ
WHERE
cd_code_2006 LIKE '1291%'
AND
NOT st_isvalid(wkb_geometry)
对于两个受影响的几何图形,三个 st_isvalids 中的第一个和最后一个为 true,而中间的不是。
不幸的是,这两种方法都不能修复联合,只能
SELECT st_union(st_buffer(wkb_geometry,0.4)) FROM cd06answ
WHERE cd_code_2006 LIKE '1291%'
产生几何图形,但
SELECT st_union(st_buffer(wkb_geometry,0.3)) FROM cd06answ
WHERE cd_code_2006 LIKE '1291%'
不能修复(我之前尝试过小缓冲区技巧,但没有将其提升到这个水平)。
对于修复来说,这似乎有点太多了。
I'm facing a problem dealing with Australian census collection shapes provided in MapInfo format by the Australian Bureau of Statistics. I am loading these into a PostGIS database using the ogr2ogr tool, which works for a majority of the shapes, but not all of them.
A simple example of the problem I am facing is a query like this (requires loading the NSW data set):
SELECT st_union(wkb_geometry) FROM cd06answ WHERE cd_code_2006 LIKE '1291%'
The result of this query is not the expected shape but NULL.
There are no null values in the table, but there are invalid geometries. For example
SELECT cd_code_2006 FROM cd06answ
WHERE cd_code_2006 LIKE '1291%' AND NOT st_isvalid(wkb_geometry)
retrieves the values '1291301' and '1291321'. If I exclude invalid geometries the st_union succeeds.
Connecting Quantum GIS to the database allows rendering both shapes in question. They should be part of the geometric union, so I need to fix the problem somehow.
Are there better ways to load the MapInfo data into PostGIS? Or some means of fixing the data inside PostGIS? Since the database data renders ok it should be possible to save it, shouldn't it?
EDIT: based on Christophe's feedback I experimented a bit more with st_buffer and st_snaptogrid. The result of this query:
SELECT
cd_code_2006,
st_isvalid(st_buffer(wkb_geometry,0)),
st_isvalid(st_snaptogrid(wkb_geometry, 0.00000001)),
st_isvalid(st_snaptogrid(wkb_geometry, 0.0000001))
FROM
cd06answ
WHERE
cd_code_2006 LIKE '1291%'
AND
NOT st_isvalid(wkb_geometry)
Is that for both affected geometries the first and the last of the three st_isvalids is true, the middle one isn't.
Unfortunately neither approach fixes the union, only a
SELECT st_union(st_buffer(wkb_geometry,0.4)) FROM cd06answ
WHERE cd_code_2006 LIKE '1291%'
results in a geometry, but
SELECT st_union(st_buffer(wkb_geometry,0.3)) FROM cd06answ
WHERE cd_code_2006 LIKE '1291%'
does not (I had tried the small buffer trick earlier, but didn't push it up to this level).
This seems a bit too much for a fix.
发布评论
评论(2)
尝试在这些无效几何图形上运行
st_buffer
(半径为 0,然后是 0.000000001 等)或st_snaptogrid
来“修复”它们(链接到文档 此处 和 此处)。当从 Mapinfo 或使用
ogr2ogr2
的其他来源导入时,我发现这些错误会弹出(基本上是由更高的精度和/或舍入问题引起的)。我认为 Postgis 开发人员计划包含一个特定的精度减速器功能,但如果我没记错的话,1.4 中没有这个功能。如果这没有帮助,请发布您当前的 postgis 版本以及多边形和投影的 wkt 版本。多边形无效还有其他可能的原因。
Try running
st_buffer
(with radius 0 first and then 0.000000001 etc) orst_snaptogrid
on these invalid geometries to 'repair' them (link to the docs here and here).I've seen these errors pop up when importing from Mapinfo or other sources with
ogr2ogr2
(basically caused by higher precision and/or rounding issues). I think the Postgis developers were planning to include a specific precision reducer function but if I recall correctly there isn't one in 1.4.If that does not help please post your current postgis version and a wkt version of your polygon and projection. There are other possible causes for invalidness of polygons.
你好,
您是否尝试过 ST_IsValidReason(geometry) 来了解问题所在?
/尼克拉斯
Hallo
Have you tried ST_IsValidReason(geometry) to get a clue about what is wrong?
/Nicklas