-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWasps-DataGenSpacebyTime2020(V6-NewConstraintDistance).r
303 lines (172 loc) · 7.16 KB
/
Wasps-DataGenSpacebyTime2020(V6-NewConstraintDistance).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
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
### SCRIPT GENERATING DATA FOR THE OPTIMAL DESIGN ALGORITHM - CONSIDERING THE WHOLE REGION
### 500 x 500 metres & distance between CEHUM & sites
##### Data: http://maps.elie.ucl.ac.be/CCI/viewer/index.php ### Water bodies 4.0
### Webpage https://mgimond.github.io/Spatial/point-pattern-analysis-in-r.html
##### Data: http://maps.elie.ucl.ac.be/CCI/viewer/index.php ### Water bodies 4.0
### Webpage https://mgimond.github.io/Spatial/point-pattern-analysis-in-r.html
##### Load the libraries
library(raster)
library(geosphere)
library(maptools)
library(MASS)
library(rgdal)
library(SDMTools)
library(spatstat)
library(sf)
library(beepr)
library(SDraw)
library(rgeos)
#### Import grid to use as a window
shape.grid<-readOGR("e:/CONTAIN/Wasps/Wasps2020/Grid_500_SA.shp")
shape.grid
plot(shape.grid)
### Centroids of each cell
cent.grid<-coordinates(shape.grid)
#### Location of CEHUm
x.cehum<-649573.53
y.cehum<-5593520.26
points(x.cehum, y.cehum, pch=19)
#### Euclidean distance between CEHUM and each cell
distance.tot.cehum<-round(apply(cent.grid, 1, function(x) sqrt((x[1]-x.cehum)^2+(x[2]-y.cehum)^2)), digits=0)
### Define the geographical window from the grid
win.grid<-as.owin(as.vector(extent(shape.grid)))
### Create raster
res.raster<-500 ### Resolution - 500 metres in length
ext.ras<-extent(shape.grid)
### Number of columns of the raster (x)
ras.col<-round((ext.ras[2]-ext.ras[1])/res.raster, digits=0)
ras.col
### Number of rows of the raster (y)
ras.row<-round((ext.ras[4]-ext.ras[3])/res.raster, digits=0)
ras.row
### Create empty raster
ras.wasps<-raster(ext=ext.ras, nrow=ras.row, ncol=ras.col)
crs(ras.wasps)<-"+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
values(ras.wasps)<-1
### Check a plot
plot(ras.wasps)
####### Extract values to data frame
det.ab<-data.frame(dist.cehum=distance.tot.cehum)
######################################################## COVARIATES
#### Water stuff - length of water in each cell
water.shape<-readOGR("e:/CONTAIN/Wasps/Wasps2020/cursos agua region (hidroregion)_SAG.shp")
water.shape
plot(water.shape)
#### Rasterize
r.water<-rasterize(water.shape, ras.wasps, field=water.shape$LENGTH, fun='sum', background=0)
values(r.water)
summary(values(r.water))
plot(r.water)
plot(water.shape, add=TRUE)
#### Export raster
writeRaster(r.water, "e:/CONTAIN/Wasps/pop density/WaterBody-Length.grd", format="raster", overwrite=TRUE)
#### Add values to data frame
det.ab$lenght.water<-values(r.water)
########################### Population density 2020: from https://sedac.ciesin.columbia.edu/data/set/gpw-v4-population-density-rev11/data-download
pop.dens<-raster("e:/CONTAIN/Wasps/pop density/gpw_v4_population_density_rev11_2020_30_sec_6.asc")
pop.dens<-projectRaster(pop.dens, crs="+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" )
plot(pop.dens)
pop.dens
new.dens<-resample(pop.dens, ras.wasps, "bilinear")
dens2<-crop(pop.dens, extent(ras.wasps))
new.dens<-mask(new.dens, ras.wasps)
plot(new.dens)
writeRaster(new.dens, "e:/CONTAIN/Wasps/pop density/WaspsPopDens.grd", format="raster", overwrite=TRUE)
#### Add values to data frame
det.ab$pop.dens<-values(new.dens)
############################################## LAND USES - FROM MAGDA
land.cov<-readOGR("e:/CONTAIN/Wasps/Wasps2020/usos.shp")
### 8 types of land use
use.class<-levels(land.cov$Uso_final)
use.class
#### Rasterize
proj.cov<-rasterize(land.cov, ras.wasps, field=land.cov$Uso_final, fun='max', background=NA)
values(proj.cov)
summary(values(proj.cov))
plot(proj.cov)
plot(proj.cov, add=TRUE)
#### Export raster
writeRaster(proj.cov, "e:/CONTAIN/Wasps/pop density/Land-Use.grd", format="raster", overwrite=TRUE)
#### Add values to data frame
det.ab$land.use<-use.class[values(proj.cov)]
################################################ NDVI https://land.copernicus.eu/global/products/ndvi
x.min<--74.111
x.max<--71.169
y.min<--41.758
y.max<--38.104
chile.ext<-extent(x.min, x.max, y.min, y.max)
#### SEPTEMBER
ndvi1<-raster("e:/CONTAIN/Wasps/pop density/NDVI-Sept2019.nc")
ndvi1<-crop(ndvi1, chile.ext)
ndvi1
plot(ndvi1)
proj.cover<-projectRaster(ndvi1, crs="+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" )
plot(proj.cover)
proj.cover
new.cover<-resample(proj.cover, ras.wasps, "bilinear")
cover2<-crop(proj.cover, extent(ras.wasps))
new.ndvi1<-mask(new.cover, ras.wasps)
plot(new.ndvi1)
writeRaster(new.ndvi1, "e:/CONTAIN/Wasps/pop density/NDVI-Sept19LosRios.grd", format="raster", overwrite=TRUE)
#### OCTOBER
ndvi2<-raster("e:/CONTAIN/Wasps/pop density/NDVI-Oct2019.nc")
ndvi2<-crop(ndvi2, chile.ext)
ndvi2
plot(ndvi2)
proj.cover<-projectRaster(ndvi2, crs="+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" )
plot(proj.cover)
proj.cover
new.ndvi2<-resample(proj.cover, ras.wasps, "bilinear")
cover2<-crop(proj.cover, extent(ras.wasps))
new.ndvi2<-mask(new.ndvi2, ras.wasps)
plot(new.ndvi2)
writeRaster(new.ndvi2, "e:/CONTAIN/Wasps/pop density/NDVI-Oct19LosRios.grd", format="raster", overwrite=TRUE)
#### NOVEMBER
ndvi3<-raster("e:/CONTAIN/Wasps/pop density/NDVI-Nov2019.nc")
ndvi3<-crop(ndvi3, chile.ext)
ndvi3
plot(ndvi3)
proj.cover<-projectRaster(ndvi3, crs="+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" )
plot(proj.cover)
proj.cover
new.ndvi3<-resample(proj.cover, ras.wasps, "bilinear")
cover3<-crop(proj.cover, extent(ras.wasps))
new.ndvi3<-mask(new.ndvi3, ras.wasps)
plot(new.ndvi3)
writeRaster(new.ndvi3, "e:/CONTAIN/Wasps/pop density/NDVI-Nov19LosRios.grd", format="raster", overwrite=TRUE)
#### DCEMBER
ndvi4<-raster("e:/CONTAIN/Wasps/pop density/NDVI-Dec2019.nc")
ndvi4<-crop(ndvi4, chile.ext)
ndvi4
plot(ndvi4)
proj.cover<-projectRaster(ndvi4, crs="+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" )
plot(proj.cover)
proj.cover
new.ndvi4<-resample(proj.cover, ras.wasps, "bilinear")
cover4<-crop(proj.cover, extent(ras.wasps))
new.ndvi4<-mask(new.ndvi4, ras.wasps)
plot(new.ndvi4)
writeRaster(new.ndvi4, "e:/CONTAIN/Wasps/pop density/NDVI-Dec19LosRios.grd", format="raster", overwrite=TRUE)
#### JANUARY 2020
ndvi5<-raster("e:/CONTAIN/Wasps/pop density/NDVI-Jan20.nc")
ndvi5<-crop(ndvi5, chile.ext)
ndvi5
plot(ndvi5)
proj.cover<-projectRaster(ndvi5, crs="+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" )
plot(proj.cover)
proj.cover
new.ndvi5<-resample(proj.cover, ras.wasps, "bilinear")
cover5<-crop(proj.cover, extent(ras.wasps))
new.ndvi5<-mask(new.ndvi5, ras.wasps)
plot(new.ndvi5)
writeRaster(new.ndvi5, "e:/CONTAIN/Wasps/pop density/NDVI-Jan20LosRios.grd", format="raster", overwrite=TRUE)
#### Add values to data frame
ndvi.tot<-data.frame(values(new.ndvi1), values(new.ndvi2), values(new.ndvi3), values(new.ndvi4), values(new.ndvi5))
det.ab$mean.ndvi<-rowMeans(ndvi.tot)
det.ab$median.ndvi<-apply(ndvi.tot, 1, median)
det.ab$var.ndvi<-apply(ndvi.tot, 1, var)
det.ab
summary(det.ab)
### Export
write.table(det.ab, "e:/CONTAIN/Wasps/Wasps2020/Nest-Count-OptimalExperimental.csv", sep=",")
write.table(det.ab, "e:/CONTAIN/Experimental Design/Wasps/Nest-Count-OptimalExperimental.csv", sep=",")