Created
July 18, 2013 03:33
-
-
Save prologic/6026526 to your computer and use it in GitHub Desktop.
Testing shapely's simplify operation while preserving topology and resulting in an invalid geometry.
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/env python | |
from json import loads | |
from shapely import speedups | |
speedups.available and speedups.enable() | |
from shapely.geometry import shape | |
from numpy import asarray | |
from matplotlib import pyplot as plt | |
def plot_polygon(g): | |
a = asarray(g.exterior) | |
plt.plot(a[:, 0], a[:, 1]) | |
def plot_multipolygon(g): | |
for g in g.geoms: | |
plot_polygon(g) | |
def plot_geometry(g): | |
if g.type == "MultiPolygon": | |
plot_multipolygon(g) | |
elif g.type == "Polygon": | |
plot_polygon(g) | |
geom = shape(loads(open("test.json", "r").read())) | |
print "geom.is_valid: {0:s}".format(repr(geom.is_valid)) | |
if not geom.is_valid: | |
geom = geom.buffer(0.0) | |
print "geom.is_valid: {0:s}".format(repr(geom.is_valid)) | |
plot_geometry(geom) | |
plt.show() | |
plt.clf() | |
geom = geom.simplify(0.05) | |
print "geom.is_valid: {0:s}".format(repr(geom.is_valid)) | |
plot_geometry(geom) | |
plt.show() | |
plt.clf() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment