-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
52 lines (52 loc) · 3.51 KB
/
.Rhistory
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
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 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)
View(multi_villages)
box_dir <- "C:/Users/cbaehr/Box Sync"
temp_dir <- "C:/Users/cbaehr/Downloads/tif_files"
library(rgdal); library(rgeos); library(sf); library(sp)
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")]
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)
multi_treatment <- merge(temp, multi_villages, by.x = "V1", by.y = "VILL_CODE")
View(multi_treatment)
View(multi_treatment)
names(multi_treatment) <- c("vill_code", "end_years", "dist", "geometry")
multi_polys <- as_Spatial(multi_treatment$geometry, IDs = as.character(1:nrow(multi_treatment)))
multi_treatment <- SpatialPolygonsDataFrame(Sr=multi_polys, data = multi_treatment)
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")
box_dir <- "C:/Users/cbaehr/Box Sync"
temp_dir <- "C:/Users/cbaehr/Downloads/tif_files"
library(rgdal); library(rgeos); library(sf); library(sp)
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")]
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)
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)
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")