2

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

Akhil
  • 181
  • 1
  • 15
  • [Additional illustration for the question](http://gis.stackexchange.com/questions/127878/line-vs-polygon-intersection-coordinates) – Akhil Dec 31 '14 at 06:41
  • Edited the question. Clearer now? – Akhil Dec 31 '14 at 06:59
  • I have tried using a simple intersection using the 'intersection' function of shapely (geom intersection) and using Fiona to write the output. – Akhil Dec 31 '14 at 07:04
  • 2
    could you post the code, example input, it's output and the expected output. – ryanpattison Dec 31 '14 at 07:04

1 Answers1

4

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)
Mike T
  • 41,085
  • 18
  • 152
  • 203
  • what if I want to then divide the shape by the linestring (or the intersection segments interior to the polygon? in that case, shapely.ops.split will work IFF the intersection points are not in the set of exterior coords of the polygon – roberto tomás Jan 01 '22 at 14:31