-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIWPP_maps.py
363 lines (290 loc) · 15.6 KB
/
IWPP_maps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from flask import Flask, render_template, request, jsonify
import json
import os
import folium
import pandas as pd
import IWPP_functions
import branca.colormap as cm
#file paths
absFilePath = os.path.dirname(__file__)
rootData = os.path.join(absFilePath, 'data')
mapData = os.path.join(absFilePath, 'map')
# Set initial location and zoom level
width_map=800
height_map=800
location_S_3=[51.54, -0.14]
zoom_start_S_3=11.4
location_S_4=[51.54, -0.25]
zoom_start_S_4=12
# Set lists of colours for rendering features in maps
colourlists={'red': ['yellow','red','brown'],
'green': ['yellow','green'],
'blue': ['yellow','green','blue'],
'black': ['grey','black']
}
#------------------------------------------------------------------------------------
# Remap values for thickness of lines
#------------------------------------------------------------------------------------
def remap_values(value, from_min, from_max, to_min, to_max):
# Figure out how 'wide' each range is
from_span = from_max - from_min
to_span = to_max - to_min
# Convert the left range into a 0-1 range (float)
value_scaled = float(value - from_min) / float(from_span)
# Convert the 0-1 range into a value in the right range.
return to_min + (value_scaled * to_span)
#------------------------------------------------------------------------------------
# Generates the folium map for the S_4 pages bringing the mapbox studio base
#------------------------------------------------------------------------------------
def map_base_S_3():
# Create a map
m = folium.Map(location=location_S_3,
control_scale=True,
zoom_start=zoom_start_S_3,
width=800,
height=800,)
# Add a custom tile layer to the map
folium.TileLayer(
tiles="https://api.mapbox.com/styles/v1/soyeduardorico/clqp9dr8s00v201qrcb2acjki/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1Ijoic295ZWR1YXJkb3JpY28iLCJhIjoiY2tnbDFpOGRqMDV2ZzM5cnh0bjR5Z3FsdSJ9.pStbCDRInR4xBYG9cXq3bA",
attr='Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
name='map'
).add_to(m)
return m
#------------------------------------------------------------------------------------
# Generates the folium map for the S_4 pages bringing the mapbox studio base
#------------------------------------------------------------------------------------
def map_base_S_4():
# Create a map
m = folium.Map(location=location_S_4,
control_scale=True,
zoom_start=zoom_start_S_4,
width=800,
height=800,)
# Add a custom tile layer to the map
folium.TileLayer(
tiles="https://api.mapbox.com/styles/v1/soyeduardorico/clrqxkxsk00di01pedathcp14/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1Ijoic295ZWR1YXJkb3JpY28iLCJhIjoiY2tnbDFpOGRqMDV2ZzM5cnh0bjR5Z3FsdSJ9.pStbCDRInR4xBYG9cXq3bA",
attr='Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
name='map'
).add_to(m)
return m
# def map_base_S_4():
# m = folium.Map(
# location=location_S_4,
# width=800,
# height=800,
# zoom_start=zoom_start_S_4, # Set initial zoom level
# tiles=f"https://api.mapbox.com/styles/v1/soyeduardorico/clrqxkxsk00di01pedathcp14/tiles/256/{{z}}/{{x}}/{{y}}@2x?access_token=pk.eyJ1Ijoic295ZWR1YXJkb3JpY28iLCJhIjoiY2tnbDFpOGRqMDV2ZzM5cnh0bjR5Z3FsdSJ9.pStbCDRInR4xBYG9cXq3bA",
# attr='Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
# name='map'
# )
# return m
#------------------------------------------------------------------------------------
# Adds areas from a geojson colourcoded by status to a given map
#------------------------------------------------------------------------------------
def add_areas_colourcoded (map, geometry_file_path):
m = map
with open(geometry_file_path) as l: area_data = json.load(l)
def style_function(feature):
status = feature['properties']['status']
if status == 1:
return {'fillColor': '#ffff00', 'color': '#ffff00'} # Yellow
elif status == 2:
return {'fillColor': '#ffa500', 'color': '#ffa500'} # Orange
else:
return {'fillColor': '#ff0000', 'color': '#ff0000'} # Red
# Add the GeoJSON layer
j = folium.GeoJson(
area_data,
name='developments',
style_function=style_function,
).add_to(m)
# Add a tooltip for each feature
j.add_child(folium.features.GeoJsonTooltip(fields=list(area_data['features'][0]['properties'].keys())))
return m
#------------------------------------------------------------------------------------
# Adds points from a geojson colourcoded by status to a given map
#------------------------------------------------------------------------------------
def add_points_colourcoded (map, point_file_path, label, pointsize):
m = map
# Read the GeoJSON data
with open(point_file_path) as f: compensations_data = json.load(f)
# Create a FeatureGroup
compensations = folium.FeatureGroup(name=label)
# Add the GeoJSON data to the FeatureGroup
for feature in compensations_data['features']:
if feature['geometry']['type'] == 'Point':
status = feature['properties']['status']
color = '#ffff00' if status == 1 else '#ffa500' if status == 2 else '#ff0000'
properties = feature['properties']
popup_text = '<br>'.join([f'{k}: {v}' for k, v in properties.items()]) # Create the popup text
folium.CircleMarker(
location=feature['geometry']['coordinates'][::-1], # Reverse the coordinates because GeoJSON uses (longitude, latitude) while Folium uses (latitude, longitude)
radius=pointsize, # Set the radius of the circle markers
color=color, # Set the color of the circle markers
fill=True,
fill_color=color, # Set the fill color of the circle markers
).add_child(
folium.Tooltip(popup_text) # Add a popup for each property
).add_to(compensations)
# Add the FeatureGroup to the map
compensations.add_to(m)
return m
#------------------------------------------------------------------------------------
# Adds colourcoded (colourcoded=True) or thickness(colourcoded-False) rendered features from a geojson to a given map
#------------------------------------------------------------------------------------
def add_features (map, geometry_file_path, variable, legend ,colourcoded, colour, status_list, property_list_path,property_list_path_brent):
m = map
# Read the Excel file
df = pd.read_excel(property_list_path, sheet_name=variable)
df_brent = pd.read_excel(property_list_path_brent, sheet_name=variable)
# obtains the position in the excel related to the status_list
position = IWPP_functions.status_position(property_list_path, status_list)
# reads the data related to the status to later display it
variable_to_add = df.iloc[:, position].tolist() # Get the nth column (assuming n is 0-indexed)
variable_to_add_brent = df_brent.iloc[:, position].tolist() # Get the nth column (assuming n is 0-indexed)
variable_to_add_baseline = df.iloc[:, 1].tolist() # brings the baseline data to compare
# Calculate the maximum and minimum
df = df.iloc[:, 1:] # Exclude the first column
max_value = df.max().max()
min_value = df.min().min()
# generates a new geogson with the old geometry and the new properties
map_data = IWPP_functions.update_values(geometry_file_path,variable_to_add, variable_to_add_baseline, variable_to_add_brent, legend)
if colourcoded == True:
# Define a colormap
colormap = cm.LinearColormap(colors=colourlists[colour], vmin=min_value, vmax=max_value)
# Add features to the map
j = folium.GeoJson(
map_data,
name=variable,
style_function=lambda feature: {
'color': colormap(feature['properties'][legend]),
'weight': 8,
}
).add_to(m)
else:
j = folium.GeoJson(
map_data,
name=variable,
style_function=lambda feature: {
'color': '#0000FF', # Uses blue by default
'weight': remap_values(feature['properties'][legend], min_value, max_value, 2, 20), # Set the thickness of the lines based on a value,
}
).add_to(m)
# Add a tooltip for each feature
j.add_child(folium.features.GeoJsonTooltip(fields=list(map_data['features'][0]['properties'].keys())))
return m
def add_points (map, point_file_path, variable, legend ,colourcoded, colour, status_list, property_list_path, property_list_path_brent):
m = map
# Read the Excel file
df = pd.read_excel(property_list_path, sheet_name=variable)
df_brent = pd.read_excel(property_list_path_brent, sheet_name=variable)
# obtains the position in the excel related to the status_list
position = IWPP_functions.status_position(property_list_path, status_list)
# updates the values in the geojson with the data related to the status to later display it
variable_to_add = df.iloc[:, position].tolist() # Get the nth column (assuming n is 0-indexed)
variable_to_add_brent = df_brent.iloc[:, position].tolist() # Get the nth column (assuming n is 0-indexed)
variable_to_add_baseline = df.iloc[:, 1].tolist() # brings the baseline data to compare
# Calculate the maximum and minimum
df = df.iloc[:, 1:] # Exclude the first column
max_value = df.max().max()
min_value = df.min().min()
# generates a new geogson with the old geometry and the new properties
map_data = IWPP_functions.update_values(point_file_path,variable_to_add, variable_to_add_baseline, variable_to_add_brent, legend)
# Define a colormap
# at the moment min and max are 0-1, but they should be the min and max of the property
colormap = cm.LinearColormap(colors=colourlists[colour], vmin=min_value, vmax=max_value)
# Create a FeatureGroup
point_map = folium.FeatureGroup(name=variable)
# Add the GeoJSON data to the FeatureGroup
for feature in map_data['features']:
properties = feature['properties']
popup_text = '<br>'.join([f'{k}: {v}' for k, v in properties.items()]) # Create the popup text
if colourcoded == True:
rad=10
col=colormap(feature['properties'][legend])
else:
rad = remap_values(feature['properties'][legend], min_value, max_value, 2, 20)
col='#0000FF'
folium.CircleMarker(
location=feature['geometry']['coordinates'][::-1], # Reverse the coordinates because GeoJSON uses (longitude, latitude) while Folium uses (latitude, longitude)
radius= rad, # Set the radius of the circle markers
color=col, # Set the color of the circle markers
fill=True,
fill_color=col, # Set the fill color of the circle markers
).add_child(
folium.Tooltip(popup_text) # Add a popup for each property
).add_to(point_map)
# Add the FeatureGroup to the map
point_map.add_to(m)
return m
# def add_points (map, point_file_path, variable, legend ,colourcoded, status_list=None, property_list_path=None):
# m = map
# with open(point_file_path) as l: point_data = json.load(l)
# # updates the values in the geojson with the data related to the status to later display it
# if status_list is not None:
# variable_to_add = IWPP_functions.read_data_from_database(property_list_path, variable, 0)
# variable_to_add_copy = variable_to_add.copy()
# point_data = IWPP_functions.update_values(point_file_path,variable_to_add, legend)
# # Define a colormap
# # at the moment min and max are 0-1, but they should be the min and max of the property
# colormap = cm.LinearColormap(colors=['blue', 'green', 'yellow', 'red'], vmin=0, vmax=1)
# from_min = min(variable_to_add_copy)
# from_max = max(variable_to_add_copy)
# # Create a FeatureGroup
# point_map = folium.FeatureGroup(name=variable)
# # Add the GeoJSON data to the FeatureGroup
# for feature in point_data['features']:
# properties = feature['properties']
# popup_text = '<br>'.join([f'{k}: {v}' for k, v in properties.items()]) # Create the popup text
# if colourcoded == True:
# rad=10
# col=colormap(feature['properties'][legend])
# else:
# rad = remap_values(feature['properties'][legend], from_min, from_max, 2, 20)
# col='#0000FF'
# folium.CircleMarker(
# location=feature['geometry']['coordinates'][::-1], # Reverse the coordinates because GeoJSON uses (longitude, latitude) while Folium uses (latitude, longitude)
# radius= rad, # Set the radius of the circle markers
# color=col, # Set the color of the circle markers
# fill=True,
# fill_color=col, # Set the fill color of the circle markers
# ).add_child(
# folium.Tooltip(popup_text) # Add a popup for each property
# ).add_to(point_map)
# # Add the FeatureGroup to the map
# point_map.add_to(m)
# return m
# #------------------------------------------------------------------------------------
# # Adds points from a geojson to a map. Can be colourcoded
# #------------------------------------------------------------------------------------
# def add_points(map, point_file_path, label, pointsize, colourcode):
# m = map
# # Read the GeoJSON data
# with open(point_file_path) as f: compensations_data = json.load(f)
# # Create a FeatureGroup
# compensations = folium.FeatureGroup(name=label)
# # Add the GeoJSON data to the FeatureGroup
# for feature in compensations_data['features']:
# if feature['geometry']['type'] == 'Point':
# if colourcode:
# status = feature['properties']['status']
# color = '#ffff00' if status == 1 else '#ffa500' if status == 2 else '#ff0000'
# else:
# color = '#0000ff'
# properties = feature['properties']
# popup_text = '<br>'.join([f'{k}: {v}' for k, v in properties.items()]) # Create the popup text
# folium.CircleMarker(
# location=feature['geometry']['coordinates'][::-1], # Reverse the coordinates because GeoJSON uses (longitude, latitude) while Folium uses (latitude, longitude)
# radius=pointsize, # Set the radius of the circle markers
# color=color, # Set the color of the circle markers
# fill=True,
# fill_color=color, # Set the fill color of the circle markers
# ).add_child(
# folium.Tooltip(popup_text) # Add a popup for each property
# ).add_to(compensations)
# # Add the FeatureGroup to the map
# compensations.add_to(m)
# return m
# river_segmments_path = os.path.join(mapData, 'river_segments.geojson')
# CSO_nodes_path = os.path.join(mapData, 'CSO_nodes.geojson')
# river_segment_pollution_path = os.path.join(mapData, 'River_segment_pollution.xlsx')