-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_folium.py
128 lines (91 loc) · 4.21 KB
/
script_folium.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
import numpy as np
import pandas as pd
import geopandas as gpd
import folium
from folium.plugins import HeatMap
from folium.plugins import MousePosition
from shapely.geometry import Polygon
from shapely.geometry import Point
from requests import Request
import requests
from owslib.wfs import WebFeatureService
from owslib.wms import WebMapService
import os
import webbrowser
import urllib.parse
def get_humanact(name):
# URL for WFS backend
url = "https://ows.emodnet-humanactivities.eu/wfs"
# Initialize
wfs = WebFeatureService(url=url)
# Fetch the last available layer (as an example) --> 'vaestoruutu:vaki2021_5km'
layer_name = name
# Specify the parameters for fetching the data
params = dict(service='WFS', version="2.0.0", request='GetFeature',
typeName=layer_name, outputFormat='json')
# Parse the URL with parameters
wfs_request_url = Request('GET', url, params=params).prepare().url
# Read data from URL
gdf = gpd.read_file(wfs_request_url)
return gdf
xmin = -30
xmax = 40
ymin = 28
ymax = 73
munitions_buffer = 5000
platforms_buffer = 5000
windfarmspoly_buffer = 0
#VESSEL
#vessel_density = gpd.read_file("data/vessel_rgb.csv")
# WIND
min_speed = 4
max_speed = 15
windspeeds = pd.read_csv("data/windspeeds.csv", names = ["geometry", "speed(m/s)"])
windspeeds["geometry"] = [Point(eval(s)) for s in windspeeds["geometry"]]
windspeeds["weight"] = [n/max(windspeeds["speed(m/s)"]) for n in windspeeds["speed(m/s)"]]
windspeeds = gpd.GeoDataFrame(windspeeds)
tiles = []
dist = abs(windspeeds.geometry[0].xy[1][0]-windspeeds.geometry[1].xy[1][0])
for i, point in enumerate(windspeeds.geometry):
x = point.xy[0][0]
y = point.xy[1][0]
tiles.append(Polygon([[x-dist/2, y+dist/2], [x+dist/2, y+dist/2], [x+dist/2, y-dist/2], [x-dist/2, y-dist/2]]))
windspeeds["geometry"] = gpd.GeoSeries(tiles, crs = "EPSG:4326")
rest_wind = windspeeds[(min_speed >= windspeeds["speed(m/s)"]) | (max_speed <= windspeeds["speed(m/s)"])].reset_index()
windspeeds = windspeeds[(min_speed <= windspeeds["speed(m/s)"]) & (max_speed >= windspeeds["speed(m/s)"])].reset_index()
data = ["munitions", "platforms", "windfarmspoly"]
buffers = {}
buffers["munitions"] = munitions_buffer
buffers["platforms"] = platforms_buffer
buffers["windfarmspoly"] = windfarmspoly_buffer
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
coordinates = world["geometry"]
#water = gpd.GeoSeries(Polygon([[xmin, ymin], [xmin, ymax], [xmax, ymax], [xmax, ymin]]), crs = 4326).difference(world.geometry.unary_union)
#safe = water.to_crs("EPSG:32634")
colors = ['red', 'purple', 'blue', 'yellow']
# MAP
m = folium.Map(tiles=None, zoom_start = 5, location = [51.505, -0.09], control_scale = True, max_bounds = True)
folium.TileLayer(tiles = "https://api.mapbox.com/styles/v1/lucmeister5/clfsc5sqd004h01o5j4l45awp/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibHVjbWVpc3RlcjUiLCJhIjoiY2xmczkzY2Q1MDN4NzNqcGo2eXR0ZjlxMiJ9.FE0wwumsjqqLfarllMWcKA",
attr='XXX Mapbox Attribution', name = "coastlines").add_to(m)
folium.TileLayer('openstreetmap').add_to(m)
safe = windspeeds["geometry"].to_crs("EPSG:32634")
safe = safe.unary_union
for i, layer in enumerate(data):
ha = get_humanact(layer).to_crs("EPSG:4326")
ha["layer"] = pd.Series([layer for x in range(len(ha.index))])
ha.explore(m=m, color=colors[i], name=layer, show = False)
circles = ha.to_crs("EPSG:32634").geometry.buffer(buffers[layer])
mp = circles.unary_union
safe = safe.difference(mp)
# safe = safe.difference(gpd.GeoSeries(windspeeds["tiles"].to_crs("EPSG:32634").unary_union, crs = "EPSG:32634"))
safe = gpd.GeoDataFrame(geometry = gpd.GeoSeries(safe), crs = "EPSG:32634")
folium.GeoJson(data=safe.geometry, style_function=lambda x:{"fillColor":"green", "color":"green"}, name = "safe").add_to(m)
w = folium.FeatureGroup(name='wind speed').add_to(m)
for i, row in windspeeds.iterrows():
b = folium.GeoJson(data=row["geometry"], style_function=lambda x:{"fillColor":"white", "color":"white", "opacity":0})
b.add_child(folium.Popup(str(row["speed(m/s)"])))
w.add_child(b)
folium.LayerControl().add_to(m)
MousePosition().add_to(m)
m.save('my_map.html')
webbrowser.open('file://' + os.path.realpath('my_map.html'))