2010-01-07 107 views
9

我有一个包含数百万行的postgres数据库,它有一个名为geom的列,其中包含属性的边界。Postgis - 我如何在插入之前检查几何类型

使用python脚本我从该表中提取信息并将其重新插入到新表中。

当我插入新表中的脚本错误了下列要求:

Traceback (most recent call last): 
    File "build_parcels.py", line 258, in <module> 
    main() 
    File "build_parcels.py", line 166, in main 
    update_cursor.executemany("insert into parcels (par_id, street_add, title_no, proprietors, au_name, ua_name, geom) VALUES (%s, %s, %s, %s, %s, %s, %s)", inserts) 
psycopg2.IntegrityError: new row for relation "parcels" violates check constraint "enforce_geotype_geom" 

新表中有一个检查约束enforce_geotype_geom =((geometrytype(GEOM)= '多边形' ::文)OR (geom IS NULL)),而旧表不会,所以即时猜测旧表中的数据或非多边形(可能是多边形数据?)。我想保留新的数据作为多边形,所以不想插入其他任何东西。

最初我试图用标准的python错误处理来包装查询,希望dud geom行会失败,但脚本会继续运行,但脚本已经写在最后不提交,因此它不起作用。

我想我需要做的是遍历旧表格几何行,并检查它们是什么类型的几何,所以我可以确定是否要保留它或丢弃之前,我插入到新表

这是怎么回事呢?

回答

7

这惊人的有用的PostGIS SQL位应该帮助你看着办吧......还有很多几何类型测试在这里:

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
-- 
-- $Id: cleanGeometry.sql 2008-04-24 10:30Z Dr. Horst Duester $ 
-- 
-- cleanGeometry - remove self- and ring-selfintersections from 
--     input Polygon geometries 
-- http://www.sogis.ch 
-- Copyright 2008 SO!GIS Koordination, Kanton Solothurn, Switzerland 
-- Version 1.0 
-- contact: horst dot duester at bd dot so dot ch 
-- 
-- This is free software; you can redistribute and/or modify it under 
-- the terms of the GNU General Public Licence. See the COPYING file. 
-- This software is without any warrenty and you use it at your own risk 
-- 
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


CREATE OR REPLACE FUNCTION cleanGeometry(geometry) 
    RETURNS geometry AS 
$BODY$DECLARE 
    inGeom ALIAS for $1; 
    outGeom geometry; 
    tmpLinestring geometry; 

Begin 

    outGeom := NULL; 

-- Clean Process for Polygon 
    IF (GeometryType(inGeom) = 'POLYGON' OR GeometryType(inGeom) = 'MULTIPOLYGON') THEN 

-- Only process if geometry is not valid, 
-- otherwise put out without change 
    if not isValid(inGeom) THEN 

-- create nodes at all self-intersecting lines by union the polygon boundaries 
-- with the startingpoint of the boundary. 
     tmpLinestring := st_union(st_multi(st_boundary(inGeom)),st_pointn(boundary(inGeom),1)); 
     outGeom = buildarea(tmpLinestring);  
     IF (GeometryType(inGeom) = 'MULTIPOLYGON') THEN  
     RETURN st_multi(outGeom); 
     ELSE 
     RETURN outGeom; 
     END IF; 
    else  
     RETURN inGeom; 
    END IF; 


------------------------------------------------------------------------------ 
-- Clean Process for LINESTRINGS, self-intersecting parts of linestrings 
-- will be divided into multiparts of the mentioned linestring 
------------------------------------------------------------------------------ 
    ELSIF (GeometryType(inGeom) = 'LINESTRING') THEN 

-- create nodes at all self-intersecting lines by union the linestrings 
-- with the startingpoint of the linestring. 
    outGeom := st_union(st_multi(inGeom),st_pointn(inGeom,1)); 
    RETURN outGeom; 
    ELSIF (GeometryType(inGeom) = 'MULTILINESTRING') THEN 
    outGeom := multi(st_union(st_multi(inGeom),st_pointn(inGeom,1))); 
    RETURN outGeom; 
    ELSIF (GeometryType(inGeom) = '<NULL>' OR GeometryType(inGeom) = 'GEOMETRYCOLLECTION') THEN 
    RETURN NULL; 
    ELSE 
    RAISE NOTICE 'The input type % is not supported %',GeometryType(inGeom),st_summary(inGeom); 
    RETURN inGeom; 
    END IF;  
End;$BODY$ 
    LANGUAGE 'plpgsql' VOLATILE; 
+0

谢谢,本来会解决混合蟒蛇/ postgres的答案,但这是令人惊讶的是能够做到这一切在postgres里面。感谢您的回答 – ADAM 2010-01-08 00:26:28

2

选项1是在每次插入之前创建一个保存点,并在INSERT失败时回滚到该安全点。

选项2是将检查约束表达式作为WHERE条件附加在产生数据的原始查询上以避免选择它。

最好的答案取决于表格的大小,错误行的相对数量以及这个应该运行的速度和频率。

+0

感谢您的回答。我喜欢选项2,但即使geom未插入,我仍然需要插入其他数据。你知道我可以做一个select语句来打印每行的geom类型吗? – ADAM 2010-01-07 21:45:17

+0

并澄清数据库有大约500万行,这只是每月运行1次来重新生成数据,并且不需要很快。我还不知道错误行的数量 – ADAM 2010-01-07 21:46:58

+1

您可以执行原始查询(按照选项2),如 SELECT par_id,street_add,title_no,proprietors,au_name,ua_name,CASE WHEN((geometrytype(geom)='POLYGON' :: text)OR(geom IS NULL))然后geom ELSE null END AS geom FROM oldtable; 将null替换为不适合的geom值。 – 2010-01-07 23:12:16

0

我认为你可以使用 ST_CollectionExtract - 给定一(多)几何返回仅由指定类型的元素组成的(多)几何。

我在插入ST_Intersection的结果时使用它,ST_Dump将任何多多边形集合分解为单个几何体。然后ST_CollectionExtract (theGeom, 3)丢弃任何东西,但多边形:

ST_CollectionExtract((st_dump(ST_Intersection(data.polygon, grid.polygon))).geom,)::geometry(polygon, 4326)

以上3第二个参数可以是:1 == POINT, 2 == LINESTRING, 3 == POLYGON

相关问题