Last active
April 29, 2024 08:38
-
-
Save cosmosgenius/90f01ef03547a0cfadf399aaa4e9e641 to your computer and use it in GitHub Desktop.
Render a geojson file with openstreetmap tiles to png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import geopandas as gpd | |
import matplotlib.pyplot as plt | |
import contextily as ctx | |
# Read the GeoJSON file | |
gdf = gpd.read_file("file.geojson") | |
# Plot the GeoJSON data | |
fig, ax = plt.subplots(figsize=(5, 5)) | |
gdf.plot(ax=ax, edgecolor='blue', facecolor='none') # Plot GeoJSON data | |
# Add OpenStreetMap tiles as background | |
ctx.add_basemap(ax, crs=gdf.crs, source=ctx.providers.OpenStreetMap.Mapnik) | |
plt.axis('off') # Turn off the axis | |
plt.savefig("output_image.png", dpi=1200, bbox_inches='tight') # Save as PNG | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment