geomaps.py 8.35 KB
Newer Older
1
2
3
4
5
'''
Created on 1 august 2021
@author: cplumejeaud
ANR PORTIC : used to map geographic data of portic - ports, province and state borders of 1789
'''
6
from branca.utilities import color_brewer
7
8
from branca.element import Html

9
import folium
10
11
from folium.plugins import MarkerCluster

12
13
14
15
16
17
18
19
20
import pandas as pd
#import geopandas as gpd
import os

import branca
import json
import requests
import random

21
#from ipyleaflet import AwesomeIcon, GeoJSON, Map, Marker, LayerGroup, SearchControl
22
23


24
# Look at https://python-visualization.github.io/folium/quickstart.html#GeoJSON/TopoJSON-Overlays
25
# https://georgetsilva.github.io/posts/mapping-points-with-folium/
26
27
# https://makina-corpus.com/blog/metier/2019/python-carto
# https://www.geeksforgeeks.org/visualizing-geospatial-data-using-folium-in-python/?ref=rp 
28
29
30
31
32
33

# https://gis.stackexchange.com/questions/362582/coordinate-system-mismatch-in-folium 
# https://ipyleaflet.readthedocs.io/en/latest/api_reference/geo_json.html
# https://ipyleaflet.readthedocs.io/en/latest/api_reference/search_control.html

# For popup options, look at leaflet : https://leafletjs.com/reference-1.4.0.html#popup
34
35
36
37
38
39
40
41
42

url = (
    "https://raw.githubusercontent.com/medialab/portic-storymaps-2021/main/public/data/"
)
france_bad = f"{url}/cartoweb_france_1789_geojson.geojson"

url2 = (
    "https://gitlab.huma-num.fr/portic/gazetteer/-/raw/master/maps/"
)
43
44
45
46
47
48
49

url3 = (
    "C:/Travail/Dev/portic_humanum/gazetteer/maps"
)

world_geojson=f"{url3}/cartoweb_world_1789_29juillet2021_mixte4326_geojson.geojson"
ports_geojson=f"{url3}/ports_1789_4326.json"
50
51
cities_geojson=f"{url3}/cities_2018_4326.json"

52

53

54
"""
55
56
57
58
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"))
59
"""
60
#with open('test.json', 'r') as f:
61
with open(world_geojson, 'r') as f:
62
    data = json.load(f)
63
64
65
66
67
68
69

with open(ports_geojson, 'r') as f:
    ports = json.load(f)

with open(cities_geojson, 'r') as f:
    cities = json.load(f)

70
71

def random_color(feature):
72
    """A set of RGB colors has been randomly generated in the database for each unit of level 0 (state). """
73
74
    #print(feature['properties'])
    #print(feature['properties']['red'])
75
76
77
    return {
        'opacity': 1, 
        'dashArray': '9', 
78
        'fillOpacity': 0.5, 
79
80
        'weight': 1,
        'color': 'black',
81
82
        'fillColor': '#%02x%02x%02x' % (feature['properties']['red'], feature['properties']['green'], feature['properties']['blue']),
        #'fillColor': random.choice(['red', 'yellow', 'green', 'orange', 'blue']),
83
84
85
    }


86
#, tiles="Stamen Terrain",
87

88
m = folium.Map(location=[46.1722, -1.1481],zoom_start=6, tiles="Stamen Watercolor", height='80%', top='0.0%', crs='EPSG3857')
89
90
91
92
93
94
95
96
97
98
texte="<h1>Mapping the maritime world of 1789</h1>"
""" 	<h1>Mapping the maritime world of 1789</h1>
	<ul>
	<li>This map has been made in <a href='http://anr.portic.fr'> ANR PORTIC </a> under CC4.0-BY-SA Licence</li>
	<li>First layer is the borders of world in 1789, drawn from many sources. Ask to Christine Plumejeaud for metadata </li>
	<li>Second layer is the the set of harbors coming from navigocorpus database, for the end of 18th century. Ask to Silvia Marzagalli for metadata</li>
	<li>Data and metadata can be downloaded from our <a href='https://gitlab.huma-num.fr/portic/gazetteer/maps'> github </a> </li>
	</ul> """
intro = folium.Div(height='20%', top='0.0%')
intro.add_child(Html(data=texte), name="intro")
99
100
101
102
103
104
105
106
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)


107
108
109
110
111
112
113
114
115
116
117
#####################################################################
## First layer with countries
#####################################################################

"""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)"""

118
#https://ipyleaflet.readthedocs.io/en/latest/api_reference/geo_json.html
119
layer_world = folium.GeoJson(world_geojson,
120
    name="Word borders in 1789", 
121
    style_function= random_color
122
123
    )
layer_world.add_to(m)
124
marker_clusterinfo = MarkerCluster().add_to(layer_world)
125

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
feature_list = data["features"]
for f in feature_list:
    #print(f['properties'])
    shortname = f['properties']['shortname']
    latitude = f['properties']['lat']
    longitude = f['properties']['long']
    popuptext = "<b>"+shortname+"</b>"
    if f['properties']['dominant']!=None :
        if f['properties']['unitlevel']!=2:
            popuptext += "<br> Dominated by : "+str(f['properties']['dominant'])
        else:
            popuptext += "<br> Belongs to : "+str(f['properties']['dominant'])
    folium.Marker(
        [latitude, longitude], 
        popup=folium.Popup(html=popuptext,  maxWidth=500), 
        tooltip=shortname,
        icon=folium.Icon(color="red", icon="info-sign")
    ).add_to(marker_clusterinfo)
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#####################################################################
## Second layer with ports
#####################################################################

layer_port = folium.FeatureGroup(name="Ports of navigocorpus database")
layer_port.add_to(m)

#https://georgetsilva.github.io/posts/mapping-points-with-folium/
marker_cluster = MarkerCluster().add_to(layer_port)



#For popup options, look at leaflet : https://leafletjs.com/reference-1.4.0.html#popup
feature_list = ports["features"]
for f in feature_list:
    #print(f['properties'])
    #uhgs_id, toponyme_standard_fr, toponyme_standard_en, state_1789_fr, state_1789_en, amiraute, province, status
    shortname = f['properties']['toponyme_standard_fr']
    latitude = f['properties']['lat']
    longitude = f['properties']['long']
    popuptext = "<b>"+shortname+"</b>"
    if latitude!= None : 
        popuptext += "<br><table>"
        if f['properties']['state_1789_fr']!=None :
            popuptext += "<tr><td> State : </td><td>"+str(f['properties']['state_1789_fr']+"</td></tr>")
        if f['properties']['province']!=None :
            popuptext += "<tr><td> Province : </td><td>"+str(f['properties']['province']+"</td></tr>")
        if f['properties']['amiraute']!=None :
            popuptext += "<tr><td> Admiralty : </td><td>"+str(f['properties']['amiraute']+"</td></tr>")
        if f['properties']['status']!=None :
            popuptext += "<tr><td> Status : </td><td>"+str(f['properties']['status']+"</td></tr>")
        if f['properties']['uhgs_id']!=None :
            popuptext += "<tr><td> UHGS_ID : </td><td>"+str(f['properties']['uhgs_id']+"</td></tr>")
        popuptext += "</table>"
        folium.CircleMarker(
            [latitude, longitude], 
            popup=folium.Popup(html=popuptext,  maxWidth=500),
            radius=10,
            #fill=True,
            color="blue",
            color_opacity=0.5,
            fill_color="blue",
            fill_opacity=0.5,
            tooltip=shortname,
            #icon=folium.Icon(color="red", icon="info-sign")
        ).add_to(marker_cluster)
191
192
193
194
195
196
197
198
199
200
201


#https://ipyleaflet.readthedocs.io/en/latest/api_reference/search_control.html
""" marker = Marker(icon=AwesomeIcon(name="check", marker_color='green', icon_color='darkred'))
m.add_control(SearchControl(
  position="topleft",
  layer=layer_world,
  zoom=4,
  property_name='shortname',
  marker=marker
)) """
202
203


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#####################################################################
## Cities layers
#####################################################################

layer_cities = folium.FeatureGroup(name="Cities (capitals) 2018")
layer_cities.add_to(m)

feature_list = cities["features"]
for f in feature_list:
    folium.vector_layers.CircleMarker(
        location=[f['properties']['lat'], f['properties']['long']],
        tooltip =f['properties']['capital_city'],
        radius = 7,
        color="black",
        fill_color="black",
        fill_opacity=0.5,
        #icon=folium.Icon(color="black", icon="info-sign")
    ).add_to(layer_cities)
222
223


224
## Add a control on layers to display
225
folium.LayerControl().add_to(m)
226
227
## You can get lat/long by clicking anywhere
m.add_child(folium.LatLngPopup())
228

229
##Export HTML
230
231
#m.save("maps/index.html")
m.save("index.html")