Skip to content

Instantly share code, notes, and snippets.

@stiles
Created September 20, 2024 18:42
Show Gist options
  • Save stiles/599a26e1ba61ce6fd3a263e4e1813757 to your computer and use it in GitHub Desktop.
Save stiles/599a26e1ba61ce6fd3a263e4e1813757 to your computer and use it in GitHub Desktop.
Geometric center of Los Angeles
import geopandas as gpd
import matplotlib.pyplot as plt
# Load the shapefile or GeoJSON file for Los Angeles
la_boundary = gpd.read_file('https://maps.lacity.org/lahub/rest/services/Boundaries/MapServer/7/query?outFields=*&where=1%3D1&f=geojson').to_crs('EPSG:4326')
# Reproject to a suitable projected CRS (EPSG:3310 for California Albers)
la_boundary_projected = la_boundary.to_crs(epsg=3310)
# Calculate the centroid in the projected CRS
centroid_projected = la_boundary_projected.geometry.centroid
# Reproject the centroid back to the original geographic CRS (EPSG:4326) to get lat/lon coordinates for plotting
centroid_geo = centroid_projected.to_crs(epsg=4326)
# Plotting the city boundary with a light gray background
fig, ax = plt.subplots(figsize=(10, 10))
# Plot the LA boundary with a light gray background
la_boundary.plot(ax=ax, color='lightgray')
# Plot the centroid on top with a red dot
centroid_geo.plot(ax=ax, color='red', marker='o', markersize=100)
# Set the title and show the plot
ax.set_title('Geometric Center of Los Angeles')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment