-
Notifications
You must be signed in to change notification settings - Fork 0
101 new function shape to geojson #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0dbc5e8
19e3811
5beba18
ea31501
c82d735
7474aca
6e2c37c
5bd63ac
fe6112e
a574b87
07e5caf
8998755
5ddb2ec
a9cc814
b401faf
87bfbbc
28c72b8
f2be7a5
6d4a3e8
d0a88c4
5e3474e
b6f968c
f14dc2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#' Convert shapefiles to geojson | ||
#' Deze functie zet alle shapes in een specifieke map, één bestand of een lijst van bestanden | ||
#' van .shp om naar .geojson. | ||
#' Daarnaast wordt de projectie getransformeerd naar wgs84 of een andere projectie. | ||
#' | ||
#' @param input een character string, een map, een bestand of een lijst van bestanden | ||
#' @param output een character string, de map waar de geojson bestanden worden | ||
#' opgeslagen, default is de input map | ||
#' @param output_crs een integer, de projectie van de output, default is wgs84 | ||
#' @param overwrite een boolean of character string, Vraag of de bestanden mogen | ||
#' overschreven worden, default is "ask" | ||
#' | ||
#' @details | ||
#' De functie checkt of de input een map is, als dit het geval is worden alle .shp | ||
#' bestanden in de map omgezet naar .geojson. Als de input geen map is, wordt de | ||
#' input als bestand beschouwd en wordt deze omgezet naar .geojson. | ||
#' | ||
#' Als de output niet is gespecificeerd, wordt de output gelijkgesteld aan de input. | ||
#' | ||
#' @family spatial | ||
#' @export | ||
#' @author Sander Devisscher | ||
#' | ||
#' @returns een .geojson bestand of meerdere .geojson bestanden | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Voorbeeld van hoe de shape_to_geojson functie te gebruiken | ||
#' # Sla boswachterijen_2024 op als .shp bestand in een tempdir | ||
#' boswachterijen_2024 <- fistools::boswachterijen$boswachterijen_2024 | ||
#' tempdir <- tempdir() | ||
#' sf::st_write(boswachterijen_2024, paste0(tempdir, "/boswachterijen_2024.shp")) | ||
#' | ||
#' # controleer of de shp goed opgeslagen werd | ||
#' browseURL(tempdir) | ||
#' | ||
#' # Zet de shp om naar geojson | ||
#' shape_to_geojson(input = tempdir) | ||
#' | ||
#' # Read and plot the geojson | ||
#' boswachterijen_2024_geojson <- sf::st_read(paste0(tempdir, "/boswachterijen_2024.geojson")) | ||
#' leaflet::leaflet() %>% | ||
#' leaflet::addTiles() %>% | ||
#' leaflet::addPolygons(data = boswachterijen_2024_geojson) | ||
#' } | ||
|
||
shape_to_geojson <- function(input, | ||
output, | ||
output_crs = 4326, | ||
overwrite = "ask"){ | ||
|
||
## Check if the input is a directory #### | ||
if(dir.exists(input)){ | ||
filelist <- dir(path = input, pattern = ".shp", recursive = TRUE, full.names = FALSE) | ||
## Overwrite output with input if not specified | ||
if(missing(output)){ | ||
message("Output folder is not specified, using input folder as output folder") | ||
output <- input | ||
} | ||
}else{ | ||
filelist <- input | ||
|
||
## Extract input folder #### | ||
input <- dirname(input) | ||
|
||
filelist <- gsub(pattern = input, replacement = "", filelist) | ||
|
||
## Output is not specified, but needed | ||
if(missing(output)){ | ||
message("Output folder is not specified, using input folder as output folder") | ||
output <- input | ||
} | ||
} | ||
|
||
## Check if the output folder exists #### | ||
if(!dir.exists(output)){ | ||
dir.create(output) | ||
} | ||
|
||
## Check if the output crs is an integer #### | ||
output_crs <- as.integer(output_crs) | ||
|
||
if(is.na(output_crs)){ | ||
stop("The output crs should be an integer") | ||
} | ||
|
||
## Check if the overwrite is a boolean or character string #### | ||
if(!is.logical(overwrite) & !is.character(overwrite)){ | ||
stop("The overwrite should be a boolean or character string") | ||
} | ||
|
||
## omit .shp.xml extention files | ||
filelist <- gsub(pattern = ".xml", replacement = "", filelist, fixed = TRUE) | ||
filelist <- gsub(pattern = ".shp", replacement = "", filelist) | ||
filelist <- unique(filelist) | ||
|
||
## Loop over the filelist #### | ||
for(f in filelist){ | ||
output_fn <- paste0(f, ".geojson") | ||
|
||
## Check if the output file exists #### | ||
if(file.exists(here::here(output, output_fn))){ | ||
if(overwrite == "ask"){ | ||
q_overwrite <- utils::askYesNo(paste0(output_fn, " already exists, overwrite?")) | ||
}else{ | ||
q_overwrite <- overwrite | ||
} | ||
}else{ | ||
q_overwrite <- overwrite | ||
} | ||
|
||
if(q_overwrite == FALSE & file.exists(here::here(output, output_fn))){ | ||
message(paste0(f, " already exists >> skipping")) | ||
next() | ||
} | ||
|
||
shape <- sf::st_read(here::here(input, paste0(f, ".shp"))) %>% | ||
sf::st_make_valid() | ||
|
||
## Check if the shape has a crs #### | ||
if(is.na(sf::st_crs(shape))){ | ||
message(paste0(f, " has no crs, please provide a crs & retry >> skipping")) | ||
next() | ||
} | ||
|
||
## Check if the crs is not wgs84 #### | ||
if(sf::st_crs(shape)$epsg != output_crs){ | ||
message(paste0(f, " is not output crs >> transforming")) | ||
shape <- sf::st_transform(shape, output_crs) | ||
} | ||
|
||
if(q_overwrite == TRUE & file.exists(here::here(output, output_fn))){ | ||
file.remove(here::here(output, output_fn)) | ||
} | ||
|
||
## Write the shape to geojson #### | ||
sf::st_write(shape, here::here(output, output_fn), | ||
driver = "GeoJSON", | ||
overwrite = q_overwrite) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.