Skip to content

Instantly share code, notes, and snippets.

@cosmosgenius
Last active April 29, 2024 08:38
Show Gist options
  • Save cosmosgenius/90f01ef03547a0cfadf399aaa4e9e641 to your computer and use it in GitHub Desktop.
Save cosmosgenius/90f01ef03547a0cfadf399aaa4e9e641 to your computer and use it in GitHub Desktop.
Render a geojson file with openstreetmap tiles to png
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