pyhld/qc/drop_endpoints.py

33 lines
1.0 KiB
Python
Raw Normal View History

2024-04-19 19:29:59 +00:00
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import MultiPoint
def extract_endpoints(geometry):
"""
Extracts the start and end points of a LineString or MultiLineString.
"""
if geometry.type == 'LineString':
return MultiPoint([geometry.coords[0], geometry.coords[-1]])
elif geometry.type == 'MultiLineString':
points = []
for line in geometry:
points.append(line.coords[0])
points.append(line.coords[-1])
return MultiPoint(points)
# Load the shapefile
drop_cable_gdf = gpd.read_file('drop_cable.shp')
# Extract endpoints
drop_cable_gdf['endpoints'] = drop_cable_gdf['geometry'].apply(extract_endpoints)
# Create a GeoDataFrame for endpoints
endpoints_gdf = gpd.GeoDataFrame(geometry=drop_cable_gdf['endpoints'].explode())
# Plotting
fig, ax = plt.subplots()
drop_cable_gdf.plot(ax=ax, color='blue', label='Drop Cables')
endpoints_gdf.plot(ax=ax, color='red', marker='o', label='Endpoints', markersize=5)
plt.legend()
plt.show()