3
import shapely.wkt
import matplotlib.pyplot as plt

I have 2 shapely geometries, p and q:

p = shapely.wkt.loads("POLYGON((1.4 0.7, 1.8 0.7, 1.8 1.1, 1.4 1.1, 1.4 0.7))") 
q = shapely.wkt.loads("MULTIPOLYGON(((1 0.5, 2 0.5, 1.6 0.85, 1 1, 1 1, 1 0.5),(1.5 0.9, 1.6 0.9, 1.6 1, 1.5 0.9)))") 

enter image description here

(plot via: gpd.GeoSeries([p,q]).plot(color=['r','b'],alpha=0.5))

I would like to remove every vertex from q within p. How can I do that?

Expected output is r, a shapely geometry:

r = shapely.wkt.loads("MULTIPOLYGON(((1 0.5, 2 0.5, 1 1, 1 1, 1 0.5)))") 

Visualized (in yellow):

enter image description here

(via gpd.GeoSeries([p,q,r]).plot(color=['r','b','y'],alpha=0.5))

1

1 Answer 1

1

A somewhat tedious method:

from shapely.geometry import Point, Polygon, MultiPolygon
r = MultiPolygon([Polygon([Point(xy) for xy in zip(*geom.exterior.xy) if not p.contains(Point(xy))]) for geom in q.geoms])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.