Skip to content

Instantly share code, notes, and snippets.

@ArthurDelannoyazerty
Last active July 7, 2025 14:55
Show Gist options
  • Save ArthurDelannoyazerty/4eb5f41d0595c87b5864823be4bab641 to your computer and use it in GitHub Desktop.
Save ArthurDelannoyazerty/4eb5f41d0595c87b5864823be4bab641 to your computer and use it in GitHub Desktop.
Display geojson/geometris on a earth map and save it as html interactive page
import folium
from pathlib import Path
from folium.plugins import Draw
def display_geometries(list_geometries:list, output_filepath:Path, inverse_lat_lon:bool=True):
"""
Save the geometries on a map on a html page with an Earth background.
Args:
list_geometries (list[dict]): List of geometry dicts to plot.
output_filepath (Path): Path to save the output html file.
"""
# Create the map
m = folium.Map(
location=[20.0, 0.0],
zoom_start=3,
tiles='OpenStreetMap',
control_scale=True,
)
# Add plugins ( https://python-visualization.github.io/folium/latest/user_guide/plugins )
Draw(export=True).add_to(m)
# Add the geometries to the map
for geom in list_geometries:
geom_coords = geom['coordinates'][0]
if inverse_lat_lon:
geom_coords = [[coord[1], coord[0]] for coord in geom_coords]
else:
geom_coords = [[coord[0], coord[1]] for coord in geom_coords]
folium.Polygon(
locations=geom_coords,
color='blue',
weight=1,
fill=True,
fill_opacity=0.2,
).add_to(m)
m.save(output_filepath)
@ArthurDelannoyazerty
Copy link
Author

ArthurDelannoyazerty commented Jul 7, 2025

if __name__ == "__main__":
    geom = {
        "coordinates": [
          [
            [
              0.6740844563959456,
              49.15772552780709
            ],
            [
              -0.6327265461352454,
              48.27128798923039
            ],
            [
              -2.2699111167224544,
              48.41280364204937
            ],
            [
              -0.37576932653647077,
              46.764122933163435
            ],
            [
              1.4596393848830473,
              47.25471579943127
            ],
            [
              1.7679880484018042,
              46.65337032077778
            ],
            [
              2.656325864728899,
              47.62217908070957
            ],
            [
              1.210023800129676,
              47.878861900875876
            ],
            [
              1.6505218908710617,
              48.776971572132595
            ],
            [
              0.6740844563959456,
              49.15772552780709
            ]
          ]
        ],
        "type": "Polygon"
      }
    
    geom2 = {
        "coordinates": [
          [
            [
              5.880409472366921,
              43.28118747948824
            ],
            [
              6.247491214651035,
              42.99188955196979
            ],
            [
              7.20190374458889,
              43.36664249388036
            ],
            [
              7.958092295842789,
              44.01956214671782
            ],
            [
              7.297345159731748,
              44.40370479359095
            ],
            [
              6.988996496213048,
              43.61697124062539
            ],
            [
              5.76294347698456,
              44.46137206147387
            ],
            [
              5.880409472366921,
              43.28118747948824
            ]
          ]
        ],
        "type": "Polygon"
      }

    display_geometries([geom, geom2], Path('test_geom.html'))

Capture d'écran 2025-07-07 164749

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment