Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 544713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:41:04+00:00 2026-05-13T10:41:04+00:00

i have a postgres database with millions of rows in it it has a

  • 0

i have a postgres database with millions of rows in it it has a column called geom which contains the boundary of a property.

using a python script i am extracting the information from this table and re-inserting it into a new table.

when i insert in the new table the script bugs out with the following:

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"

The new table has a check constraint enforce_geotype_geom = ((geometrytype(geom) = ‘POLYGON’::text) OR (geom IS NULL)) whereas the old table does not, so im guessing theres dud data or non polygon (perhaps multipolygon data?) in the old table. i want to keep the new data as polygon so want to not insert anything else.

Initially i tried wrapping the query with standard python error handling with the hope that the dud geom rows would fail but the script would keep running , but the script has been written to commit at the end not each row so it doesnt work.

I think what i need to do is iterate through the old table geom rows and check what type of geometry they are so i can establish whether or not i want to keep it or throw it away before i insert into the new table

Whats the best way of going about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:41:04+00:00Added an answer on May 13, 2026 at 10:41 am

    This astonishingly useful bit of PostGIS SQL should help you figure it out… there are many geometry type tests in here:

    -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    -- 
    -- $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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.