| name | multipart-geometry-handling |
| description | Work with MultiPolygon and MultiLineString geometries from real plate boundary datasets. |
MultiPart Geometry Handling
Installation
pip install geopandas shapely pandas
Overview
Real-world plate boundary data consists of disconnected LineString segments (MultiLineString) that need special handling for distance calculations. Similarly, plates can be represented as MultiPolygons.
Understanding MultiPart Geometries
MultiLineString (Plate Boundaries)
from shapely.geometry import MultiLineString, LineString
boundary_geom = boundaries.geometry.unary_union
print(f"Type: {type(boundary_geom)}")
if isinstance(boundary_geom, MultiLineString):
for i, line in enumerate(boundary_geom.geoms):
print(f"Segment {i}: {len(line.coords)} points")
MultiPolygon (Plate Regions)
pacific_geom = pacific_plates.geometry.unary_union
print(f"Type: {type(pacific_geom)}")
if isinstance(pacific_geom, MultiPolygon):
for i, poly in enumerate(pacific_geom.geoms):
print(f"Polygon {i}: {len(poly.exterior.coords)} boundary points")
Distance Calculation with MultiLineString
The Shapely .distance() method automatically handles MultiLineString:
- Computes distance to nearest segment
- Works efficiently with large multipart geometries
distances = points_gdf.geometry.distance(boundary_geom)
min_distance = distances.min()
Point-in-MultiPolygon
GeoPandas' .within() method also handles MultiPolygon correctly:
within_mask = earthquakes.geometry.within(pacific_geom)
earthquakes_within = earthquakes[within_mask]
Validation: Verify Coverage
import geopandas as gpd
boundaries_gdf = boundaries.to_crs('EPSG:3857')
plate_gdf = pacific_plates.to_crs('EPSG:3857')
boundary_geom = boundaries_gdf.geometry.unary_union
plate_geom = plate_gdf.geometry.unary_union
def sample_points_on_line(line, num_points=100):
distances = [i/num_points for i in range(num_points)]
return [line.interpolate(d, normalized=True) for d in distances]
sample_pts = sample_points_on_line(boundary_geom, 100)
Common Operations
Flatten MultiPart to Single Collection
all_parts = list(boundary_geom.geoms)
print(f"Total boundary segments: {len(all_parts)}")
from shapely.geometry import LineString
if len(all_parts) > 1:
flat_line = LineString([pt for line in all_parts for pt in line.coords])
Measure Boundary Lengths
if hasattr(boundary_geom, 'length'):
total_length = boundary_geom.length
print(f"Total boundary length: {total_length / 1000:.0f} km")
Important Notes
.unary_union automatically handles combining multiple geometries
- Distance calculations with MultiLineString return distance to nearest segment
- Point-in-polygon tests work correctly with MultiPolygon
- Shapely 2.0+ has improved performance for multipart operations