Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14126

Python Shapely - Intersecting GEOMETRYCOLLECTION with POLYGON

$
0
0

I am using Shapely to intersect geographical data and Pyshp to write the results to a shapefile. I don't completely understand how "intersection" works.

First, I create some points and calculate the Voronoi polygons this way:

# (1)from shapely import LineString, MultiPoint, Point, Polygon, MultiPolygonfrom shapely.geometry import shapefrom shapely.constructive import voronoi_polygonsfrom shapely import intersectionimport shapefileoutput_shapefile = "/tmp/simple_test_01.shp"points = MultiPoint([Point(1,1), Point(2,2), Point(4,3), Point(2,4), Point(2,5), Point(6,5), Point(5,4), Point(7,1)])myVoronoi = voronoi_polygons(points)#>>> myVoronoi#<GEOMETRYCOLLECTION (POLYGON ((-5 -5, -5 4.625, -4.5 4.5, 0 3, 4 -1, 4 -5, -...>n = 0with shapefile.Writer(output_shapefile, shapeType=5) as w:    w.field("ID", "N")    for geom in myVoronoi.geoms:        w.shape(geom)          w.record(n)        n += 1

When I draw the shapefile on ArcMap, I get this (green points highlighted by me, not in the shapefile):

enter image description here

I create a polygon with which I will intersect the Voronoi polygons:

# (2)polygon_to_intersect = Polygon([Point(0,0), Point(2,8), Point(4,3.7), Point(10,4.5), Point(5,-4), Point(0,0)])output_shapefile = "/tmp/simple_test_02.shp"with shapefile.Writer(output_shapefile, shapeType=5) as w:    w.field("ID", "N")    w.shape(polygon_to_intersect)      w.record(1)

On ArcMap it is like this (shown with red outline):

enter image description here

I perform the intersection:

# (3)intersected = intersection(myVoronoi, polygon_to_intersect)>>> intersected<POLYGON ((0.6 2.4, 0.75 3, 1.125 4.5, 2 8, 3.553 4.66, 3.739 4.261, 4 3.7, ...>output_shapefile = "/tmp/simple_test_03.shp"n = 0with shapefile.Writer(output_shapefile) as w:    w.field("ID", "N")    w.shape(intersected)      w.record(n)

simple_test_03.shp is a single polygon. Its vertices can be seen here:

enter image description here

I was expecting "intersection" to give me the Voronoi polygons intersected with polygon_to_intersect. However, I am getting polygon_to_intersect with new vertices, in the places where the polygon's exterior intersects the Voronoi polygons.

>>> intersected<POLYGON ((0.6 2.4, 0.75 3, 1.125 4.5, 2 8, 3.553 4.66, 3.739 4.261, 4 3.7, ...>

"intersected" is a Polygon instead of a MultiPolygon, why?

However, if I do this:

# (4)output_shapefile = "/tmp/simple_test_04.shp"multipol = []for geom in myVoronoi.geoms:    multipol.append(intersection(geom, polygon_to_intersect))n = 0with shapefile.Writer(output_shapefile) as w:    w.field("ID", "N")    for pol in multipol:        w.shape(pol)          w.record(n)        n += 1

I get this result, which is what I wanted in the first place:

enter image description here

Each colored area is a Voronoi polygon intersected with "polygon_to_intersect".

Why do I need to do (4) to get what I want, when I was expecting (3) to work that way?Is it the fact that myVoronoi is a GEOMETRYCOLLECTION instead of a POLYGON or MULTIPOLYGON?

I tried to convert myVoronoi to a MultiPolygon prior to the intersection:

# (5)output_shapefile = "/tmp/simple_test_05.shp"myVoronoi2 = MultiPolygon(myVoronoi)>>> myVoronoi2<MULTIPOLYGON (((-5 -5, -5 4.625, -4.5 4.5, 0 3, 4 -1, 4 -5, -5 -5)), ((-5 1...>intersected2 = intersection(myVoronoi2, polygon_to_intersect)>>> intersected2<POLYGON ((0.6 2.4, 0.75 3, 1.125 4.5, 2 8, 3.553 4.66, 3.739 4.261, 4 3.7, ...>n = 0with shapefile.Writer(output_shapefile) as w:    w.field("ID", "N")    w.shape(intersected2)      w.record(n)>>> intersected2 == intersectedTrue

but the result is the same as in (3).

This is a simplified example. In my real situation, "myVoronoi" has 18000+ polygons and "polygon_to_intersect" is much bigger. (3) is not working as I want and (4) works but has performance issues.


Viewing all articles
Browse latest Browse all 14126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>