[postgis-users] only insert valid/closed ring geometries

Shad Keene shadkeene at hotmail.com
Thu Apr 10 10:34:13 PDT 2008


Thanks for the help...I'm using an if statement comparing the first and last coords as suggested earlier, and then a next if they are not equal.  So far it works, but if I run into issues, I'll attempt the try catch block.  
Shad


From: lr at pcorp.us
To: postgis-users at postgis.refractions.net
Subject: RE: [postgis-users] only insert valid/closed ring geometries
Date: Thu, 10 Apr 2008 12:24:01 -0400










For this kind of situation I usually use a Try Catch block and 
then in the Catch either discard the error and move on or log 
it.
 
I haven't used perl much, but from this, it looks like it supports 
Try Catch
http://snippets.dzone.com/posts/show/928
 
Hope that helps,
Leo



From: postgis-users-bounces at postgis.refractions.net 
[mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of Shad 
Keene
Sent: Thursday, April 10, 2008 9:36 AM
To: 
postgis-users at postgis.refractions.net
Subject: [postgis-users] only 
insert valid/closed ring geometries





          I started 
using postgis recently because of the spatial capabilities, but I'm stuck 
now.


  
  I have a list of user-entered geometries that I'm inserting into a table via 
  perl.  I'm using the following code to insert the geometries into the 
  table: (ISOL basically refers to a point 
  geometry.)


 

if ($cwa_entries[4] eq "AREA") 
  {

$sth = $dbh->prepare("INSERT into cwa 
  values(GeomFromText('POLYGON(($cwa_entries[3]))', 4326), '$cwa_entries[6]', 
  '$cwa_entries[5]', nextval('cwa_id_seq'), '$cwa_entries[0]', 
  '$cwa_entries[1]', '$cwa_entries[4]')") or die "could not execute: 
  $DBI::errstr";

}

elsif ($cwa_entries[4] eq "LINE") {

$sth 
  = $dbh->prepare("INSERT into cwa 
  values(GeomFromText('LINESTRING($cwa_entries[3])', 4326), '$cwa_entries[6]', 
  '$cwa_entries[5]', nextval('cwa_id_seq'), '$cwa_entries[0]', 
  '$cwa_entries[1]', '$cwa_entries[4]')") or die "could not execute: 
  $DBI::errstr";

}

elsif ($cwa_entries[4] eq "ISOL") {

$sth 
  = $dbh->prepare("INSERT into cwa 
  values(GeomFromText('POINT($cwa_entries[3])', 4326), '$cwa_entries[6]', 
  '$cwa_entries[5]', nextval('cwa_id_seq'), '$cwa_entries[0]', 
  '$cwa_entries[1]', '$cwa_entries[4]')") or die "could not execute: 
  $DBI::errstr";

}

 

$sth->execute() or die "Could 
  not execute: $DBI::errstr";

 

Sometimes there are values 
  that are entered that do not represent valid geometries.  For instance, I 
  might get a set of coordinates that is designated a polygon, but the last 
  coordinates don't equal the first coordinates(i.e. non-closed ring).  I 
  don't have any control over the data that reaches me(i.e. it's already 
  designated as AREA or LINE or ISOLD(for point).  So how can I skip the 
  non-closed-ring geometries and only enter the valid geometries so that my 
  script doesn't quit on me and produce an error before it enters all the 
  data.  Right now, I get an error on the execute statement, and it pretty 
  much prevents the rest of the data from being inserted into the 
  database.  This may be a more perl-centered question, but I thought there 
  might be a postgresql command that has the effect of "insert only if 
  GeomFromText()...is valid", else move on to the next insert.  I did see 
  the ST_IsClosed(geometry) and the ST_IsValid(geometry) 
  statements, but I don't know how to implement them prior to the insert.  
  Thanks for any help.

Shad






Get in touch in an instant. Get Windows Live Messenger now. 

_________________________________________________________________
Going green? See the top 12 foods to eat organic.
http://green.msn.com/galleries/photos/photos.aspx?gid=164&ocid=T003MSN51N1653A
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20080410/1c63e53b/attachment.html>


More information about the postgis-users mailing list