Created
September 1, 2016 10:34
-
-
Save StyXman/174df434435529fb73e8edc014ea52ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import shapely.wkt | |
import shapely.wkb | |
import shapely.geometry | |
import psycopg2 | |
s= open('121897178.wkt').read() | |
sh= shapely.wkt.loads(s) | |
conn= psycopg2.connect(dbname='gis') | |
cur= conn.cursor() | |
nodes= list (sh.exterior.coords) | |
count= len (nodes) | |
i= 0 | |
while True: | |
# make a copy, remove element i, keep Polygon closed | |
new_nodes= nodes[:] | |
print (nodes[:20], i) | |
new_nodes.pop (i) | |
if i==0: | |
new_nodes[-1]= new_nodes[0] | |
shape= shapely.geometry.Polygon(new_nodes) | |
if not shape.is_valid: | |
i+= 1 | |
continue | |
try: | |
cur.execute ('select ST_StraightSkeleton(%s);', (shapely.wkb.dumps(shape),)) | |
# the Polygon is ok, so i is problematic | |
# keep it | |
i+= 1 | |
except psycopg2.InternalError as e: | |
print (e) | |
# the Polygon is not ok, so remove i | |
nodes= new_nodes | |
# we reached the end of the line | |
# -2 because we need at least 3 points (orig, other, orig) to make a Polygon | |
if i==len (nodes)-2: | |
break | |
# the cursor is no longer valid, reopen | |
cur.close() | |
conn.rollback() | |
cur= conn.cursor() | |
print (shapely.wkt.dumps (shape)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment