David,<div><br></div><div>I don't have my importer script anymore but the function I built for correcting the polygons is still around somewhere. I just need to get official approval to release the code to the public though I doubt there will be a problem with that. FYI the code is written in Perl. Below is the basic logic I used from what I can remember without looking at the code.</div>
<div><br></div><div><ol><li>Check the validity of the multipolygon.</li><li>If Invalid, split the multipolygon into an array of individual polygons with no inner polygons. Inner polygons also go into the array.</li><li>Check all polygons for validity.</li>
<ol><li>If valid, continue to the next polygon until finished.</li><li>If invalid check if start and end point are the same, if not make them the same.</li><li>If still invalid check for a reason (ST_INVALIDREASON) to see if there is a self-intersection and at what coordinate. If this is the case attempt to do a split at the self-intersection to create a new polygon. This is rather complex and doesn't work nicely in many cases. You may have to recurse through this as sometimes there are multiply self-intersections in a single polygon so be sure to check if the current intersection coordinate is the same as the old one or not to prevent getting into a possible infinite loop.</li>
<li>If still invalid, check for "spurs", which are what I referred to as the zero area piers and peninsulas. This involves ripping the polygon into individual points and iterating through the array looking for a part that loops back on itself.</li>
<li>As a last resort do a buffer(geom,0) on the geometry.</li></ol><li>Take the polygons and do a quick and dirty check if it is valid as a multipolygon. This works most of the time as there are rarely inner-rings in the data set though they do happen.</li>
<li>If that was not valid, check the polygons for duplicates using ST_EQUALS. Sometimes they have duplicated polygons that cause some issues.</li><li>If still not valid loop through all of the individual polygons by size, starting with the largest.</li>
<ol><li>For each polygon check the other polygons to see if they fall completely within the polygon by size starting with the largest. Be sure to flag a polygon as used and when checking additional polygons that you are comparing against the new multipolygon to prevent issues causes by a bulls eye within the middle of an inner ring.</li>
</ol><li>If there are still issues, take your new multipolygons and attempt to merge them with ST_UNION to each other. This can fix some issues where the outer rings overlap each other.</li><li>If still invalid attempt a ST_BUFFER(multigeom,0) and flag it for a manual inspection as ST_BUFFER occasionally has issues with multipolygons and will have some of the rings missing.</li>
</ol><div>You will need to work out your own rules for how to handle naming issues that may exist in the data set. Also keep in mind that in some cases you may find two distinct entries for a single political entity and you will want to do an ST_UNION on them so you only have one entry in your database for a country (or not depending upon your needs).</div>
</div><div>--<br>Nathan Gerber<br>
<br><br><div class="gmail_quote">On Thu, Dec 30, 2010 at 11:26 PM, David Blackman <span dir="ltr"><<a href="mailto:whizziwig@gmail.com">whizziwig@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Do you still have the script? Would you be willing to share it? I'd be<br>
happy to put in the work to update & host it if I can get it working.<br>
<br>
thanks,<br>
--dave<br>
<div><div></div><div class="h5"><br>
On Thu, Dec 30, 2010 at 8:50 PM, Nathan Gerber <<a href="mailto:ngerber999@gmail.com">ngerber999@gmail.com</a>> wrote:<br>
> I had to build a rather complex fixing script as the problems are quite<br>
> varied. Some could be fixed with a simple st_buffer(geom,0) while others<br>
> required more advanced cleaning. Below is an incomplete list of problems I<br>
> found while sorting through the data:<br>
><br>
> Figure eights or as I like to call them, Loop-di-dos |><|<br>
> Self-Intersections at river heads due to the rather simplistic boundary<br>
> simplification algorithms used.<br>
> Boundaries that back tracked along themselves.<br>
> Boundaries that contained a line off the boundary that represented a<br>
> peninsula or pier that had zero area.<br>
> Overlapping borders that had to be cleaned for my data set so that no single<br>
> point could belong in two countries.<br>
> There were also a few inconsistencies with naming conventions that had to be<br>
> manually corrected based upon some research (thank you wikipedia).<br>
> Missing state/province and/or county/muncipality borders that left a few<br>
> holes at the sub-country level in a few areas.<br>
> Some of my missing data and inconsistencies may have been corrected since I<br>
> pulled the data a year or so ago.<br>
><br>
> I'd offer to send you my cleaned up data set but unfortunately it has been<br>
> updated with some proprietary data for Canada and Mexico.<br>
> --<br>
> Nathan Gerber<br>
><br>
><br>
> On Thu, Dec 30, 2010 at 7:03 PM, David Blackman <<a href="mailto:david@whizziwig.com">david@whizziwig.com</a>> wrote:<br>
>><br>
>> Hi all--<br>
>><br>
>> I'm trying to import the 10m-admin-1 data from<br>
>> <a href="http://www.naturalearthdata.com/" target="_blank">http://www.naturalearthdata.com/</a> to postgis 1.5.2. It's generating a<br>
>> lot of invalid geometry that I don't know how to fix.<br>
>><br>
>> My import command looks like this:<br>
>><br>
>> shp2pgsql -W LATIN1 -I 10m-admin-1-states-provinces-shp ><br>
>> 10m-admin-1-states-provinces-shp.sql<br>
>> (note, in postgis2, this entirely fails on invalid input characters,<br>
>> none of the character sets I tried worked)<br>
>><br>
>> and of the import, 148 rows have invalid geometry, with errors like:<br>
>> blackmad=# select fips_1 FROM<br>
>> public."10m-admin-1-states-provinces-shp" WHERE ST_IsValid(the_geom) =<br>
>> false;<br>
>> NOTICE: Holes are nested at or near point 101.662 3.04074<br>
>> NOTICE: Self-intersection at or near point 120.185 22.9625<br>
>> NOTICE: IllegalArgumentException: Invalid number of points in<br>
>> LinearRing found 2 - must be 0 or >= 4<br>
>> NOTICE: Ring Self-intersection at or near point -47.3025 -16.0401<br>
>> NOTICE: Too few points in geometry component at or near point -65.458<br>
>> -22.1012<br>
>><br>
>> This cleangeometry.sql script @<br>
>> <a href="http://www.sogis1.so.ch/sogis/dl/postgis/cleanGeometry.sql" target="_blank">http://www.sogis1.so.ch/sogis/dl/postgis/cleanGeometry.sql</a> fixes most,<br>
>> but the LinearRing errors cause the script to choke.<br>
>><br>
>> Can someone advise me on how to fix these errors, or where the problem<br>
>> lies (in the data or the import tool)?<br>
>><br>
>> thanks<br>
>> --dave<br>
>> _______________________________________________<br>
>> postgis-users mailing list<br>
>> <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>
>> <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users" target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>
><br>
><br>
> _______________________________________________<br>
> postgis-users mailing list<br>
> <a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>
> <a href="http://postgis.refractions.net/mailman/listinfo/postgis-users" target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>
><br>
><br>
</div></div></blockquote></div><br></div>