-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_wrangle.R
150 lines (96 loc) · 3.61 KB
/
geo_wrangle.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
pacman::p_load(tidyverse,
sf,
glue,
janitor,
fs,
rlist)
path <- "data/shapefiles"
# areas_description_tbl <- read_delim("data/portal_upload/areas_tbl.csv",
# delim = ";")
name_layers <- function(shp_paths){
shp_paths %>%
path_file() %>%
path_ext_remove() %>%
make_clean_names() %>%
str_c("_sf")
}
trans_write <- function(sf_obj, subfolder = "portal_write"){
# doesn't work with pipes!
filename_stem <- deparse(substitute(sf_obj)) %>%
str_replace_all("_", "-")
filenames <- c(glue("data/{subfolder}/{filename_stem}.geojson"), glue("data/{subfolder}/{filename_stem}.gpkg"))
filenames %>% walk(~st_transform(sf_obj,
crs = 4326) %>%
st_write(dsn = .x, append = FALSE))
}
# get the raw data for processing
unzip_from_onedrive <- function(path = "data/shapefiles",
delete_zipfile = TRUE){
zipfile <- fs::dir_ls(path, glob = "*.zip")
filepaths <- unzip(zipfile,
junkpaths = TRUE,
exdir = path,
overwrite = TRUE)
if (delete_zipfile){
fs::file_delete(zipfile)
}
return(filepaths)
}
# RAW SHAPEFILES ---
# Get the unprocessed shapefiles from downloaded onedrive zipped file
filepaths <- unzip_from_onedrive(path = path)
# filepaths <- dir_ls(path, glob = "*.shp") # if not unzipping
shp_paths <- filepaths %>%
str_subset("shp$")
layer_name <- name_layers(shp_paths)
geo_list <- map(shp_paths, ~st_read(.x, promote_to_multi = TRUE)) %>%
set_names(layer_name)
list2env(geo_list, .GlobalEnv)
# GEOMETRICALLY SOUND FILES FOR POLISHING ---
scratch_path <- fs::path("data", "scratch")
layers_list <- fs::dir_ls(scratch_path, glob = "*.geojson") %>%
map(~st_read(.))
layer_names <- name_layers(layers_list %>% names())
raw_files_list <- layers_list %>% set_names(layer_names)
list2env(raw_files_list, .GlobalEnv)
lnrs_areas_for_description <- areas_for_description_single_4326_sf %>%
select(id)
# %>%
# inner_join(areas_description_tbl, by = join_by(id == area_id))
lnrs_areas_for_description %>% glimpse()
lnrs_areas_important <- areas_important_bd_single_fixed_4326_sf %>%
select(geometry)
lnrs_areas_important
lnrs_areas_pi <- pi_singlepart_4326_sf %>%
select(geometry)
lnrs_areas_priority <- priority_areas_4326_sf %>%
select(id, Name)
lnrs_areas_priority <-
st_read("data/scratch/areas_priority_clean.gpkg") %>%
select(id, Name) %>%
inner_join(areas_tbl, by = join_by(id == area_id))
trans_write(lnrs_areas_priority)
trans_write(lnrs_areas_pi)
trans_write(lnrs_areas_for_description)
trans_write(lnrs_areas_important)
# The successful strategy is to convert to single part "Multipart to Singlepart" in QGIS, CRS to 4326 and export to geoJSON
# This should be the same as st_cast(..., "POLYGON") but that doesn't seem to work
trans_write(picasted)
# https://r-spatial.org/r/2017/03/19/invalid.html
any(is.na(st_is_valid(pi_valid)))
any(na.omit(st_is_valid(pi_valid)) == FALSE)
identical(pi_valid_buff, pi_valid_mp)
trans_write(pi_valid_buff)
valid_sf_list <- map(geo_list, st_make_valid)
multipoly_sf <- master_areas_for_priority_description_simplified_cleaned_sf %>%
mutate(geom_type = st_geometry_type(.)) %>%
filter(geom_type != "POLYGON")
trans_write(multipoly_sf, "data/fixed/multipoly.geojson")
trans_write(poly_sf, "data/fixed/poly.geojson")
#----
# Multipart to single part in qgis
# fix geometry
# filter(!st_is_empty(.))
# just 2 errors - self intersections
# but missing avon and wickwar
#----