-
Notifications
You must be signed in to change notification settings - Fork 0
/
treatment_buffers.R
68 lines (48 loc) · 3.32 KB
/
treatment_buffers.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
box_dir <- "C:/Users/cbaehr/Box Sync"
temp_dir <- "C:/Users/cbaehr/Downloads/tif_files"
library(rgdal); library(rgeos); library(sf); library(sp)
## read in 3km buffered village data and format
villages <- st_read(paste0(box_dir, "/cambodia_ndvi_eval/inputData/village_shapefiles/buffered_villages/buffered_villages.shp"),
stringsAsFactors=F)
villages$VILL_CODE <- as.integer(villages$VILL_CODE)
villages <- villages[, c("VILL_CODE", "geometry")]
## read in multi buffered village data and format
multi_villages <- st_read(paste0(box_dir, "/cambodia_ndvi_eval/inputData/village_shapefiles/multi_buffered_villages/multi_buffered_villages.shp"),
stringsAsFactors=F)
multi_villages$VILL_CODE <- as.integer(multi_villages$VILL_CODE)
multi_villages <- multi_villages[, c("VILL_CODE", "mrb_dist", "geometry")]
## read in PID treatment data
pid <- read.csv(paste0(box_dir, "/cambodia_ndvi_eval/inputData/pid.csv"), stringsAsFactors = F)
## collapse PID so there are no rows with duplicate village IDs
## in cases of multiple treatments per village, separate end years by "|"
temp <- tapply(pid$actual.end.yr, INDEX=list(pid$village.code),
FUN = function(x) {paste0(x, collapse = "|")})
## create trimmed PID dataset with only village code and end years
temp <- as.data.frame(cbind(names(temp), unname(temp)), stringsAsFactors=F)
## merge PID data with village polygons (buffers)
treatment <- merge(temp, villages, by.x = "V1", by.y = "VILL_CODE")
names(treatment) <- c("vill_code", "end_years", "geometry")
## convert geometry to "sp" class
polys <- as_Spatial(treatment$geometry, IDs = as.character(1:nrow(treatment)))
## convert treatment shape data to "SpatialPointsDataFrame"
treatment <- SpatialPolygonsDataFrame(Sr=polys, data = treatment)
## write treatment shape data to a shapefile
# writeOGR(treatment[, names(treatment)!="geometry"], paste0(box_dir, "/cambodia_ndvi_eval/inputData/village_shapefiles/buf_trt_villages/buf_trt_villages.shp"),
# layer = "vill_code", driver = "ESRI Shapefile")
#########
## merge PID data with village polygons (buffers)
multi_treatment <- merge(temp, multi_villages, by.x = "V1", by.y = "VILL_CODE")
names(multi_treatment) <- c("vill_code", "end_years", "dist", "geometry")
## convert geometry to "sp" class
multi_polys <- as_Spatial(multi_treatment$geometry, IDs = as.character(1:nrow(multi_treatment)))
## convert treatment shape data to "SpatialPointsDataFrame"
multi_treatment <- SpatialPolygonsDataFrame(Sr=multi_polys, data = multi_treatment)
## write treatment shape data to a shapefile
# writeOGR(multi_treatment[, names(multi_treatment)!="geometry"], paste0(box_dir, "/cambodia_ndvi_eval/inputData/village_shapefiles/multi_buf_trt_villages/multi_buf_trt_villages.shp"),
# layer = "vill_code", driver = "ESRI Shapefile")
## create a secondary shapefile with dissolved buffers for generating the grid
treatment$country <- "Cambodia"
polys <- gUnaryUnion(treatment, id = treatment@data$country)
dissolved_buffers <- SpatialPolygonsDataFrame(polys, data = as.data.frame(treatment[1,]), match.ID = F)
# writeOGR(dissolved_buffers[, names(dissolved_buffers)!="geometry"], paste0(box_dir, "/cambodia_ndvi_eval/inputData/village_shapefiles/dissolve_buf_villages/dissolve_buf_villages.shp"),
# layer = "vill_code", driver = "ESRI Shapefile", overwrite_layer = T)