Akhil
Akhil

Reputation: 181

Line vs. Polygon Intersection Coordinates

I am working with Python, Shapely and Fiona. Considering there are two shapefiles available, a line shapefile and a polygon shapefile.

How to obtain an end result shapefile consisting of points of intersection (indicated with Q-marks) and their respective coordinates??

enter image description here

Upvotes: 2

Views: 4206

Answers (1)

Mike T
Mike T

Reputation: 43642

You need to get the intersection from the exterior of the polygon and the line. If you instead use the intersection with the polygon, the result is a line, since polygons have an area. Also, the intersection may be a line, if they are parallel, so you could also expect a GeometryCollection

Here is something as a start:

from shapely.wkt import loads

poly = loads('POLYGON ((140 270, 300 270, 350 200, 300 150, 140 150, 100 200, 140 270))')
line = loads('LINESTRING (370 290, 270 120)')

intersection = poly.exterior.intersection(line)

if intersection.is_empty:
    print("shapes don't intersect")
elif intersection.geom_type.startswith('Multi') or intersection.geom_type == 'GeometryCollection':
    for shp in intersection:
        print(shp)
else:
    print(intersection)

Upvotes: 4

Related Questions