Commit 3d223503 authored by Christine Plumejeaud's avatar Christine Plumejeaud
Browse files

First test with folium to build maps with leaflet and Python

parent 253ca070
import folium
import pandas as pd
#import geopandas as gpd
import os
import branca
import json
import requests
import random
# Look at https://python-visualization.github.io/folium/quickstart.html#GeoJSON/TopoJSON-Overlays
# https://makina-corpus.com/blog/metier/2019/python-carto
# https://www.geeksforgeeks.org/visualizing-geospatial-data-using-folium-in-python/?ref=rp
#https://gis.stackexchange.com/questions/362582/coordinate-system-mismatch-in-folium
url = (
"https://raw.githubusercontent.com/medialab/portic-storymaps-2021/main/public/data/"
)
france_bad = f"{url}/cartoweb_france_1789_geojson.geojson"
#antarctic_ice_shelf_topo = f"{url}/antarctic_ice_shelf_topo.json"
url2 = (
"https://gitlab.huma-num.fr/portic/gazetteer/-/raw/master/maps/"
)
#world_ok = f"{url2}/cartoweb_world_1789_29juillet2021_mixte3857_topo.json"
world_geojson=f"{url2}/cartoweb_world_1789_29juillet2021_mixte4326_geojson.geojson"
world_data = f"{url2}/dico_mixte.csv"
if not os.path.exists('world_1789.json'):
r = requests.get(world_geojson)
with open('world_1789.json', 'w') as f:
f.write(r.content.decode("utf-8"))
with open('world_1789.json', 'r') as f:
data = json.load(f)
#df = gpd.read_file(f)
state_data = pd.read_csv(world_data)
def random_color(feature):
return {
'opacity': 1,
'dashArray': '9',
'fillOpacity': 0.1,
'weight': 1,
'color': 'black',
'fillColor': random.choice(['red', 'yellow', 'green', 'orange']),
}
#print(df.head())
m = folium.Map(location=[46.1722, -1.1481],zoom_start=6, tiles="Stamen Terrain", height='80%', top='0.0%', crs='EPSG3857')
intro = folium.Div(height='20%', top='80.0%')
#intro.title("<i>Texte de présentation de la carte</i>")
m.get_root().add_child(intro,name='Christine',index=77)
style = {'fillColor': '#f5f5f5', 'lineColor': '#ffffbf', 'fill_opacity':'0.5','line_opacity': '.1'}
#styleMap = {'position': 'relative', 'width': '100%', 'height': '80.0%', 'left': '0.0%', 'top': '0.0%'}
#styleTitle = {'position': 'relative', 'width': '100%', 'height': '15.0%', 'left': '0.0%', 'top': '0.0%'}
#polygon = folium.GeoJson(gjson, style_function = lambda x: style).add_to(m)
folium.GeoJson(world_geojson,
name="world_1789_29juillet2021",
style_function = lambda x: style,
popup="feature.id",
tooltip="Oléron").add_to(m)
#https://ipyleaflet.readthedocs.io/en/latest/api_reference/geo_json.html
folium.GeoJson(world_geojson,
name="avec couleurs",
style_function= random_color
).add_to(m)
#hover_style={
# 'color': 'white', 'dashArray': '0', 'fillOpacity': 0.5
# },
"""folium.TopoJson(
json.loads(requests.get(world_ok).text),
"objects.cartoweb_world_1789_29juillet2021_mixte3857",
#style_function=style_function,
name="world_1789"
).add_to(m)"""
""" folium.Choropleth(
# geographical locations
geo_data = world_geojson,
name = "France_1789",
# the data set we are using
data = world_data,
columns = ["id", "id_sup"], #"unitlevel", "shortname",
# YlGn refers to yellow and green
fill_color = "#f5f5f5",
fill_opacity = 0.7,
line_opacity = .1,
key_on = "feature.id",
legend_name = "Appartenance",
).add_to(m) """
folium.LayerControl().add_to(m)
tooltip = "Click me!"
folium.Marker(
[46.1722, -1.1481], popup="<i>La Rochelle</i>", tooltip=tooltip
).add_to(m)
folium.Marker(
[45.9588, -1.3184],
popup="<b>Oléron</b>",
tooltip="Oléron",
icon=folium.Icon(color="red", icon="info-sign")
).add_to(m)
feature_list = data["features"]
for f in feature_list:
print(f['properties'])
## You can get lat/long by clicking anywhere
#m.add_child(folium.LatLngPopup())
#{'type': 'Feature', 'properties': {'id': 651, 'unitlevel': 1, 'shortname': 'Principauté de Piombino', 'id_sup': 903}, 'geometry': {'type': 'MultiPolygon', 'coordinates': [[[[10.29, 42.33], [10.31, 42.35], [10.33, 42.33]
m.save("index.html")
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment