<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<STYLE>.hmmessage P {
PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px
}
BODY.hmmessage {
FONT-SIZE: 10pt; FONT-FAMILY: Tahoma
}
</STYLE>
<META content="MSHTML 6.00.6000.16608" name=GENERATOR></HEAD>
<BODY class=hmmessage>
<DIV dir=ltr align=left><SPAN class=257202216-10042008><FONT face=Arial
color=#0000ff>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.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=257202216-10042008><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=257202216-10042008><FONT face=Arial
color=#0000ff>I haven't used perl much, but from this, it looks like it supports
Try Catch</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=257202216-10042008><FONT face=Arial
color=#0000ff><A
href="http://snippets.dzone.com/posts/show/928">http://snippets.dzone.com/posts/show/928</A></FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=257202216-10042008><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=257202216-10042008><FONT face=Arial
color=#0000ff>Hope that helps,</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=257202216-10042008><FONT face=Arial
color=#0000ff>Leo</FONT></SPAN></DIV><FONT face=Arial color=#0000ff></FONT><BR>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma><B>From:</B> postgis-users-bounces@postgis.refractions.net
[mailto:postgis-users-bounces@postgis.refractions.net] <B>On Behalf Of </B>Shad
Keene<BR><B>Sent:</B> Thursday, April 10, 2008 9:36 AM<BR><B>To:</B>
postgis-users@postgis.refractions.net<BR><B>Subject:</B> [postgis-users] only
insert valid/closed ring geometries<BR></FONT><BR></DIV>
<DIV></DIV><BR><BR><BR> I started
using postgis recently because of the spatial capabilities, but I'm stuck
now.<BR>
<BLOCKQUOTE>
<STYLE>UNKNOWN {
br: 0px<br>
}
UNKNOWN {
br: 10pt
}
</STYLE>
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.)<BR><BR><BR> <BR><BR>if ($cwa_entries[4] eq "AREA")
{<BR><BR>$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";<BR><BR>}<BR><BR>elsif ($cwa_entries[4] eq "LINE") {<BR><BR>$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";<BR><BR>}<BR><BR>elsif ($cwa_entries[4] eq "ISOL") {<BR><BR>$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";<BR><BR>}<BR><BR> <BR><BR>$sth->execute() or die "Could
not execute: $DBI::errstr";<BR><BR> <BR><BR>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 <SPAN class=term>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.</SPAN><BR><BR>Shad</BLOCKQUOTE><BR><BR>
<DIV><FONT style="COLOR: rgb(0,0,255)" size=1><SPAN
style="FONT-STYLE: italic; FONT-FAMILY: Lucida Handwriting,Cursive"></SPAN></FONT><EM><FONT
face="Lucida Handwriting" color=#0000ff size=1><BR></FONT></EM></DIV><BR>
<HR>
Get in touch in an instant. <A
href="http://www.windowslive.com/messenger/overview.html?ocid=TXT_TAGLM_WL_Refresh_getintouch_042008"
target=_new>Get Windows Live Messenger now.</A> </BODY></HTML>