|
|
|
|
| require(maptools)
|
| require(RColorBrewer)
|
| require(rgdal)
|
| require(rgeos)
|
| library(sf)
|
| require(raster)
|
|
|
|
|
|
|
|
|
|
|
|
|
| load('dataDL/morley/acanthephyra pelagica_Atl_rcp26_jas_prediction_AGG.RData')
|
| Atl<- pred.agg[!duplicated(pred.agg[, c('latitude', 'longitude')]), c('latitude', 'longitude')]
|
|
|
| load('dataDL/morley/actinauge verrilli_Pac_rcp26_jas_prediction_AGG.RData')
|
| Pac<- pred.agg[!duplicated(pred.agg[, c('latitude', 'longitude')]), c('latitude', 'longitude')]
|
|
|
|
|
| clim <- rbind(Atl, Pac)
|
| rm(pred.agg, Atl, Pac)
|
| clim$lon <- clim$longitude
|
| clim$lat <- clim$latitude
|
| clim <- clim[,c('lat', 'lon')]
|
|
|
| clim$lon[clim$lon>180] = clim$lon[clim$lon>180] - 360
|
| clim$lon[clim$lon< -180] = clim$lon[clim$lon< -180] + 360
|
| gridsz = unique(round(diff(sort(unique(clim$lat))),3))
|
| gridsz <- gridsz[gridsz>0]
|
|
|
|
|
|
|
| wdpa = readOGR(dsn='dataDL/WDPA/WDPA_Aug2019_marine-shapefile/WDPA_Aug2019_marine-shapefile', layer='WDPA_Aug2019_marine-shapefile-polygons')
|
|
|
| nrow(wdpa)
|
| wdpa = wdpa[wdpa$MARINE != 0,]
|
| nrow(wdpa)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| y = numeric(5*length(clim$lat))
|
|
|
| for(i in 1:length(clim$lat)){ y[(5*(i-1)+1):(5*(i-1)+5)] <- c(clim$lat[i]-gridsz/2, clim$lat[i]-gridsz/2, clim$lat[i]+gridsz/2, clim$lat[i]+gridsz/2, clim$lat[i]-gridsz/2)}
|
|
|
| x = numeric(5*length(clim$lon))
|
|
|
| for(i in 1:length(clim$lon)){ x[(5*(i-1)+1):(5*(i-1)+5)] = c(clim$lon[i]+gridsz/2, clim$lon[i]-gridsz/2, clim$lon[i]-gridsz/2, clim$lon[i]+gridsz/2, clim$lon[i]+gridsz/2) }
|
|
|
|
|
|
|
| pgns = vector('list', length(clim$lat))
|
| for(i in 1:length(pgns)){
|
| inds2 = (5*(i-1)+1):(5*(i-1)+5)
|
| pgns[[i]] = Polygons(list(Polygon(cbind(x[inds2],y[inds2]))), i)
|
| }
|
| SP <- SpatialPolygons(pgns, proj4string=CRS(proj4string(wdpa)))
|
| SPdata <- data.frame(gridpolyID = as.numeric(sapply(slot(SP, 'polygons'), slot, 'ID')), lat = clim$lat, lon = clim$lon)
|
| length(SP)
|
| SPsf <- st_as_sf(SP)
|
| SPsf2 <- dplyr::bind_cols(SPsf, SPdata)
|
|
|
|
|
|
|
|
|
|
|
| saveRDS(SPsf2, file='temp/SPsf2.rds')
|
|
|
|
|
| wdpacrop <- crop(wdpa, SP)
|
| rm(wdpa)
|
|
|
|
|
|
|
| wdpasf <- st_as_sf(wdpacrop)
|
| gI <- st_intersects(SPsf2, wdpasf)
|
| dim(gI)
|
| sum(sapply(gI, length))
|
| ng = sum(sapply(gI, length)>0); ng
|
| cols = which(sapply(gI, length)>0)
|
|
|
|
|
|
|
| warnings <- character(0)
|
| messages <- character(0)
|
| withCallingHandlers({
|
| for(i in 1:ng){
|
| if(i %% 1000 == 0) print(paste(i, 'of', ng))
|
| if(i==1){
|
| out <- st_intersection(SPsf2[cols[i],], wdpasf[gI[[cols[i]]],])
|
| } else {
|
| temp <- st_intersection(SPsf2[cols[i],], wdpasf[gI[[cols[i]]],])
|
| out <- rbind(out, temp)
|
| }
|
| }
|
| }, warning=function(w){
|
| warnings <<- c(warnings, w$message)
|
| invokeRestart("muffleWarning")
|
| }, error=function(e){
|
| stop(e)
|
| }, message=function(m){
|
| messages<<-c(messages,m$message)
|
| invokeRestart("muffleMessage")
|
| })
|
|
|
| dim(out)
|
|
|
|
|
| saveRDS(out, file=paste('temp/wdpa_by_grid', gridsz, '_intersect.rds', sep=''))
|
|
|