Initial commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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()
|
||||
@@ -0,0 +1,49 @@
|
||||
import geopandas as gpd
|
||||
import folium
|
||||
from shapely.geometry import Point
|
||||
|
||||
def extract_endpoints(geometry):
|
||||
"""
|
||||
Extracts the start and end points of a LineString or MultiLineString.
|
||||
"""
|
||||
points = []
|
||||
if geometry.type == 'LineString':
|
||||
points.append(Point(geometry.coords[0]))
|
||||
points.append(Point(geometry.coords[-1]))
|
||||
elif geometry.type == 'MultiLineString':
|
||||
for line in geometry:
|
||||
points.append(Point(line.coords[0]))
|
||||
points.append(Point(line.coords[-1]))
|
||||
return points
|
||||
|
||||
# Load the shapefile
|
||||
drop_cable_gdf = gpd.read_file('drop_cable.shp')
|
||||
|
||||
# Check and transform CRS to EPSG:4326 if needed
|
||||
if drop_cable_gdf.crs is not None and drop_cable_gdf.crs.to_epsg() != 4326:
|
||||
drop_cable_gdf = drop_cable_gdf.to_crs(epsg=4326)
|
||||
|
||||
# Extract endpoints and create a list of points
|
||||
endpoint_list = []
|
||||
for geom in drop_cable_gdf.geometry:
|
||||
endpoint_list.extend(extract_endpoints(geom))
|
||||
|
||||
# Create a GeoDataFrame for endpoints
|
||||
endpoints_gdf = gpd.GeoDataFrame(geometry=endpoint_list, crs="EPSG:4326")
|
||||
|
||||
# Convert GeoDataFrames to GeoJSON
|
||||
drop_cable_geojson = drop_cable_gdf.to_json()
|
||||
endpoints_geojson = endpoints_gdf.to_json()
|
||||
|
||||
# Create a Folium map
|
||||
m = folium.Map(location=[drop_cable_gdf.geometry.iloc[0].centroid.y, drop_cable_gdf.geometry.iloc[0].centroid.x], zoom_start=12)
|
||||
|
||||
# Add GeoJSON layers to the map
|
||||
folium.GeoJson(drop_cable_geojson, name="Drop Cables", style_function=lambda x: {'color': 'blue', 'weight': 2}).add_to(m)
|
||||
folium.GeoJson(endpoints_geojson, name="Endpoints", style_function=lambda x: {'color': 'red', 'fillColor': 'red'}).add_to(m)
|
||||
|
||||
# Add layer control to toggle layers
|
||||
folium.LayerControl().add_to(m)
|
||||
|
||||
# Save the map to an HTML file
|
||||
m.save('map.html')
|
||||
Reference in New Issue
Block a user