Reputation: 174
I want to slice Polygon with LineString and get coordinates of each side.
So far I got below code
import geopandas as gpd
from shapely.geometry import LineString, LinearRing, Point, Polygon
polygon_geom = Polygon(zip([0,0,1,1], [0,1,1,0]))
crs = {'init': 'epsg:4326'}
polygon = gpd.GeoDataFrame(index=[0], crs=crs, geometry=[polygon_geom])
Point1 = Point(0.5,0)
Point2 = Point(,1)
line = LineString([Point1,Point2])
The desired result I want should be something like:
POLYGON ((0 0, 0 1, 0.5 1, 0.5 0, 0 0)),
POLYGON ((0.5 0, 0.5 1, 1 1, 1 0, 0.5 0))
I tried intersect and intersection
Upvotes: 1
Views: 151
Reputation: 18812
Here are the relevant steps to get the solution:-
# Part-1 Use the original polygon geometry
# Create single-side-buffer polygons from line
left_hand_side = line.buffer(5, single_sided=True)
right_hand_side = line.buffer(-5, single_sided=True)
# Spatial difference operation between 2 polygons
left_part = polygon_geom.difference(right_hand_side)
print(left_part.wkt) #left part of the split polygon
right_part = polygon_geom.difference(left_hand_side)
print(right_part.wkt) #right part of the split polygon
The output:
POLYGON ((0 0, 0 1, 0.5 1, 0.5 0, 0 0)) POLYGON ((0.5 1, 1 1, 1 0, 0.5 0, 0.5 1))
# Part-2 Works on the polygon geodataframe
# The results are the same
left_part = polygon.difference(right_hand_side)
print(left_part[0].wkt)
right_part = polygon.difference(left_hand_side)
print(right_part[0].wkt)
Upvotes: 1