I am working with intersection of polygon and line segments (continued from a previous question).
When a Line intersects with a Polygon, the result is a LineString;
When a LineRing intersects with a Line, the result is a MultiPoint.
While in most cases, the intersection is only in two points, the case I have encountered is multiple intersections (in figure).
The indicated yellow lines are the ones that intersect only at two points, and thus result in no errors. But when the program begins intersection of the LineString and LinearRing where there is multiple intersections, the following error shows up.
NotImplementedError: Multi-part geometries do not themselves provide the array interface
Would there be any way to work around this? I am not at liberty to share the code, but any specific, additional information can be provided.
To provide some additional context, I am appending each intersection resultant MultiPoint and MultiLineString to a 'list' of previous results, respectively. So while the previous results were only a pair of XY coordinates, multiple intersections give multiple results. The line of code that results in the error is when I try to convert the 'list' of LineString (intersection) results to a MultiLineString using MultiLineString()
For instance, multi_line
is the list of intersections resulting from line.intersection(polygon)
. This list needs to written to a MultiLineString for creating a new shapefile.
MultipleLine = MultiLineString(multi_line)
Best Answer
If you use Shapely, transform your polygon into a LinearRing or a LineString
The intersection are polylines as you say
Now transform the polygon:
New
Without your complete script, I can help you but if I understand your question you want to convert a 'list' of intersections results to a MultiLineString or a MultiPoint, but you don't need this list.
If your Shapefile contains multiple lines, a solution is to open it with a for loop, adding the lines to a list and compute the intersections with another for loop which gives you your list.
But, knowing that the result of the intersection of a MultiLineString with a Polygon is a MultiLineString and the intersection of a MultiLineString with a LinearRing is a MultiPoint, it is easiest to open your shapefile with:
Directly a MultiLineString
A list of LineStrings
A list of the coordinates of the LineStrings
And with:
You compute directly the intersections as MultilineStrings or MultiPoints with
or
If you have multiple polygons, use a MultiPolygon