如何编写忽略无效几何图形的 PostGIS 更新语句?
How do I write a PostGIS update statement that ignores invalid geometries?
我写了一个快速的 PL/pgSQL 脚本,它为 centroid_longitude
和 centroid_latitude
创建了两个新列,并使用来自 PostGIS 几何列和 ST_X()
的数据,ST_Y()
,以及 ST_Centroid()
PostGIS 函数计算值。
alter table nhd_hr_nhdwaterbody ADD COLUMN centroid_latitude numeric(19,11);
alter table nhd_hr_nhdwaterbody ADD COLUMN centroid_longitude numeric(19,11);
UPDATE nhd_hr_nhdwaterbody SET centroid_longitude=ST_X(ST_Centroid(geom)), centroid_latitude=ST_Y(ST_Centroid(geom));
当我 运行 为具有数百万条目的数据库编写脚本时,出现以下错误。
ERROR: lwgeom_centroid: GEOS Error: IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
正如错误明确指出的那样,脚本有问题,因为 ST_Centroid()
要求几何有 0 个点或 >= 4,但我的一些数据没有那么多点。对于我目前的申请,我只想跳过不符合 0 分或大于或等于 4 分标准的条目。
我有没有办法修改此脚本,以便将无效几何图形的纬度和经度值设置为空值?
示例几何:
"01060000A0E610000001000000010300008001000000090000002CCEE69D9E9151C0286E1A1F3E3C46400000000000000000682D06E09F9151C0C0E9DF28403C46400000000000000000A48D6E9F9F9151C078231061423C464000000000000000009C9D35FF9E9151C0B848616C433C46400000000000000000C88E061C9D9151C020568951433C4640000000000000000008A4F59B9C9151C0B8CFD508433C4640000000000000000060C4A6B09C9151C0004FD209423C464000000000000000003884B6DB9C9151C0088727EE3F3C464000000000000000002CCEE69D9E9151C0286E1A1F3E3C46400000000000000000"
只需添加一个测试点数的 WHERE 子句(未测试):
UPDATE nhd_hr_nhdwaterbody SET
centroid_longitude=ST_X(ST_Centroid(geom)),
centroid_latitude=ST_Y(ST_Centroid(geom))
WHERE NOT ST_NumPoints(geom) between 1 and 3;
未更新的 co-ords 应保持为 NULL。
我写了一个快速的 PL/pgSQL 脚本,它为 centroid_longitude
和 centroid_latitude
创建了两个新列,并使用来自 PostGIS 几何列和 ST_X()
的数据,ST_Y()
,以及 ST_Centroid()
PostGIS 函数计算值。
alter table nhd_hr_nhdwaterbody ADD COLUMN centroid_latitude numeric(19,11);
alter table nhd_hr_nhdwaterbody ADD COLUMN centroid_longitude numeric(19,11);
UPDATE nhd_hr_nhdwaterbody SET centroid_longitude=ST_X(ST_Centroid(geom)), centroid_latitude=ST_Y(ST_Centroid(geom));
当我 运行 为具有数百万条目的数据库编写脚本时,出现以下错误。
ERROR: lwgeom_centroid: GEOS Error: IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
正如错误明确指出的那样,脚本有问题,因为 ST_Centroid()
要求几何有 0 个点或 >= 4,但我的一些数据没有那么多点。对于我目前的申请,我只想跳过不符合 0 分或大于或等于 4 分标准的条目。
我有没有办法修改此脚本,以便将无效几何图形的纬度和经度值设置为空值?
示例几何:
"01060000A0E610000001000000010300008001000000090000002CCEE69D9E9151C0286E1A1F3E3C46400000000000000000682D06E09F9151C0C0E9DF28403C46400000000000000000A48D6E9F9F9151C078231061423C464000000000000000009C9D35FF9E9151C0B848616C433C46400000000000000000C88E061C9D9151C020568951433C4640000000000000000008A4F59B9C9151C0B8CFD508433C4640000000000000000060C4A6B09C9151C0004FD209423C464000000000000000003884B6DB9C9151C0088727EE3F3C464000000000000000002CCEE69D9E9151C0286E1A1F3E3C46400000000000000000"
只需添加一个测试点数的 WHERE 子句(未测试):
UPDATE nhd_hr_nhdwaterbody SET
centroid_longitude=ST_X(ST_Centroid(geom)),
centroid_latitude=ST_Y(ST_Centroid(geom))
WHERE NOT ST_NumPoints(geom) between 1 and 3;
未更新的 co-ords 应保持为 NULL。