-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_helper.R
266 lines (200 loc) · 8.45 KB
/
upload_helper.R
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
library(bigrquery)
library(sf)
library(dplyr)
library(giscoR)
library(ggplot2)
# upload db
bq_auth(
path = "dev/gcs_keys/eu-wendy_key.json"
)
dataset<-"wendy_prod"
#study_area_data
dat<-read.csv("C:/Users/reto.spielhofer/OneDrive - NINA/Documents/Projects/WENDY/4_case_studies/case_studies_geoprospective.csv")
dat<-dat%>%dplyr::select(siteTYPE,siteSTATUS,windfarmNAME,siteLANG,cntrID,siteID,projID,siteN_es,lng,lat)
dat_sf<-st_as_sf(dat,coords = c("lng","lat"))
### ITALY
# for the italian case we select the larger region of Regio Calabria ( ITF65) containing both study sites and clipped by the shore line
ita_nuts<-gisco_get_nuts(year = "2021",
epsg = "4326",
cache = TRUE,
update_cache = FALSE,
cache_dir = NULL,
verbose = FALSE,
resolution = "01",
spatialtype = "RG",
country = "ITA",
nuts_id = c("ITF65","ITF64","ITF63"),
nuts_level = "3")
wt_ita<-dat_sf%>%filter(cntrID == "ITA")%>%st_buffer(0.6)
st_crs(wt_ita) <- 4326
ita_nuts_diss<-st_union(ita_nuts)%>%st_sf()
ggplot() +
geom_sf(data = ita_nuts, fill = "lightblue", color = "black") + # Plot the polygon
geom_sf(data = wt_ita, color = "red",fill=NA, size = 3) + # Plot the point on top
theme_minimal()
ita_nuts_diss$siteAREAkm2<-as.integer(st_area(ita_nuts_diss)/1000000)
ita_nuts_diss$siteNAME<-"South Calabria"
ita_nuts_diss$siteN_es <- as.integer(5)
ita_nuts_diss$projID<-"wendy"
ita_nuts_diss$siteID<-"ITA"
ita_nuts_diss$siteSTATUS<-as.integer(1)
ita_nuts_diss$siteCREATETIME<-Sys.time()
ita_nuts_diss$siteCREATOR <-"r.spielhofer"
ita_nuts_diss$respPARTNER <-"EGP"
ita_nuts_diss$siteTYPE<-"onshore"
ita_nuts_diss$siteLANG<-"en"
ita_nuts_diss$cntrID<-"ITA"
ita_nuts_diss$windfarmNAME<-"San Florio and Bagaladi Motta Montebello"
polygons<-ita_nuts_diss%>%st_drop_geometry()
polygons$geometry<-st_as_text(ita_nuts_diss$geometry)
es_tab = bq_table(project = "eu-wendy", dataset = dataset, table = 'study_site')
bq_table_upload(x = es_tab, values = polygons, create_disposition='CREATE_IF_NEEDED', write_disposition='WRITE_APPEND')
###### Spain, we just consider the three northern case studies, not the
wt_esp<-dat_sf%>%filter(cntrID == "ESP",windfarmNAME !="Motilla")%>%st_buffer(0.6)
st_crs(wt_esp) <- 4326
bbox_wf<-st_bbox(wt_esp)%>%st_as_sfc()%>%st_sf()
esp_nuts<-gisco_get_nuts(year = "2021",
epsg = "4326",
cache = TRUE,
update_cache = FALSE,
cache_dir = NULL,
verbose = FALSE,
resolution = "01",
spatialtype = "RG",
country = "ESP",
nuts_id = NULL,
nuts_level = "3")
# st_area(bbox_wf)/1000000
wt_esp<-st_join(wt_esp,esp_nuts,join = st_intersects)
nuts_uni<-wt_esp%>%distinct(wt_esp$NUTS_ID)
nuts_filter<-esp_nuts%>%filter(NUTS_ID %in% nuts_uni$`wt_esp$NUTS_ID`)
#st_write(bbox_wf,"bbox_esp.shp",append = F)
ggplot() +
geom_sf(data = nuts_filter, fill = "lightblue", color = "black") + # Plot the polygon
geom_sf(data = wt_esp, color = "red", size = 3) + # Plot the point on top
geom_sf(data = bbox_wf, fill = NA, color = "red", linetype = "dashed") +
theme_minimal()
## in spain we take the bbox of the three northern wf with a buffer of 0.5 degree
bbox_wf$siteAREAkm2<-as.integer(st_area(bbox_wf)/1000000)
bbox_wf$siteNAME<-"Greater Zaragoza area"
bbox_wf$siteN_es <- as.integer(5)
bbox_wf$projID<-"wendy"
bbox_wf$siteID<-"ESP"
bbox_wf$siteSTATUS<-as.integer(1)
bbox_wf$siteCREATETIME<-Sys.time()
bbox_wf$siteCREATOR <-"r.spielhofer"
bbox_wf$respPARTNER <-"CIRCE"
bbox_wf$siteTYPE<-"onshore"
bbox_wf$siteLANG<-"en"
bbox_wf$cntrID<-"ESP"
bbox_wf$windfarmNAME<-"El Campo, Campo Olivia and Primoral"
polygons<-bbox_wf%>%st_drop_geometry()
polygons$geometry<-st_as_text(bbox_wf$geometry)
es_tab = bq_table(project = "eu-wendy", dataset = dataset, table = 'study_site')
bq_table_upload(x = es_tab, values = polygons, create_disposition='CREATE_IF_NEEDED', write_disposition='WRITE_APPEND')
#### For greece we take the whole crete island (EL43)
grc_nuts<-gisco_get_nuts(year = "2021",
epsg = "4326",
cache = TRUE,
update_cache = FALSE,
cache_dir = NULL,
verbose = FALSE,
resolution = "01",
spatialtype = "RG",
country = NULL,
nuts_id = c("EL43"),
nuts_level = "2")
grc_nuts<-grc_nuts%>%select(geometry)
st_area(grc_nuts)/1000000
wt_grc<-dat_sf%>%filter(cntrID == "GRC")%>%st_buffer(0.5)
st_crs(wt_grc) <- 4326
# bbox_wf<-st_bbox(wt_esp)%>%st_as_sfc()
# st_area(bbox_wf)/1000000
# wt_esp<-st_join(wt_esp,esp_nuts,join = st_intersects)
ggplot() +
geom_sf(data = grc_nuts, fill = "lightblue", color = "black") + # Plot the polygon
geom_sf(data = wt_grc, color = "red", size = 3) + # Plot the point on top
#geom_sf(data = bbox_wf, fill = NA, color = "red", linetype = "dashed") +
theme_minimal()
grc_nuts$siteAREAkm2<-as.integer(st_area(grc_nuts)/1000000)
grc_nuts$siteNAME<-"Crete"
grc_nuts$siteN_es <- as.integer(5)
grc_nuts$projID<-"wendy"
grc_nuts$siteID<-"GRC"
grc_nuts$siteSTATUS<-as.integer(1)
grc_nuts$siteCREATETIME<-Sys.time()
grc_nuts$siteCREATOR <-"r.spielhofer"
grc_nuts$respPARTNER <-"MEC"
grc_nuts$siteTYPE<-"onshore"
grc_nuts$siteLANG<-"en"
grc_nuts$cntrID<-"GRC"
grc_nuts$windfarmNAME<-"Minoan"
polygons<-grc_nuts%>%st_drop_geometry()
polygons$geometry<-st_as_text(grc_nuts$geometry)
es_tab = bq_table(project = "eu-wendy", dataset = dataset, table = 'study_site')
bq_table_upload(x = es_tab, values = polygons, create_disposition='CREATE_IF_NEEDED', write_disposition='WRITE_APPEND')
all<-rbind(grc_nuts,bbox_wf,ita_nuts_diss)
st_write(all,"stud_sites.shp")
## to derive the study site name for geoprospective, get NUTS
# comm<-gisco_get_communes(
# year = "2016",
# epsg = "4326",
# cache = TRUE,
# update_cache = FALSE,
# cache_dir = NULL,
# verbose = FALSE,
# spatialtype = "RG",
# country = c("ITA","ESP","GRC")
# )
#
#
# dat_sf<-st_set_crs(dat_sf,4326)
# dat_sf<-st_join(dat_sf,comm,join = st_intersects)
dat_sf<-dat_sf%>%dplyr::select(siteTYPE,siteSTATUS,windfarmNAME,siteLANG,cntrID,siteID,projID,siteN_es,COMM_NAME)
#
# # st_crs(dat_sf)<-4326
# rect<-st_buffer(dat_sf, dist=0.2,endCapStyle = "ROUND")
# sf_stud_geom<-st_set_crs(rect,4326)
#
# ## clip all of them with coast
# onshore<-sf_stud_geom%>%filter(siteTYPE == "onshore")
# offshore<-sf_stud_geom%>%filter(siteTYPE == "offshore")
#
#
#
#
# cntr<-gisco_get_countries(year = "2020",
# epsg = "4326",
# cache = TRUE,
# update_cache = FALSE,
# cache_dir = NULL,
# verbose = FALSE,
# resolution = "01",
# spatialtype = "RG",
# country = NULL,
# region = "Europe")
# cntr<-cntr%>%filter(CNTR_ID != "RU")
#
# on_int<-sf::st_intersection(cntr,onshore)%>%dplyr::select("siteTYPE","siteSTATUS","windfarmNAME","siteLANG","cntrID","siteID","projID","siteN_es","COMM_NAME")
# plot(on_int)
#
# ## rbin offshore
# all<-rbind(on_int,offshore)
# all$siteAREAkm2<-as.integer(round(as.numeric(st_area(all))/1000000,0))
# all$siteCREATETIME<-Sys.time()
# all$siteCREATOR <-"eu-wendy-proj"
# colnames(all)[colnames(all) == "COMM_NAME"] <- "siteNAME"
st_write(all,"study_sites.shp")
# st_write(sf_stud_geom,"stud_area.shp")
polygons<-all%>%st_drop_geometry()
polygons$geometry<-st_as_text(all$geometry)
# #
# # #save it on bq
poly_table = bq_table(project = "eu-wendy", dataset = "wendy_dev", table = 'study_site')
bq_table_upload(x = poly_table, values = polygons, create_disposition='CREATE_IF_NEEDED', write_disposition='WRITE_APPEND')
mapview(rect)
## upload to wendy
## ecosystem services
dat<-read.csv("C:/Users/reto.spielhofer/OneDrive - NINA/Documents/Projects/WENDY/PGIS_ES/data_base/setup_230710/es_descr.csv")
es_tab = bq_table(project = "eu-wendy", dataset = "wendy_dev", table = 'es_descr')
bq_table_upload(x = es_tab, values = dat, create_disposition='CREATE_IF_NEEDED', write_disposition='WRITE_APPEND')