-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66782dd
commit 0dbc5e8
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#' 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 | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Voorbeeld van hoe de shape_to_geojson functie te gebruiken | ||
#' # Zet alle shapes in de map om naar geojson | ||
#' shape_to_geojson(input = "Data/Spatial") | ||
#' } | ||
|
||
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) | ||
## Overwrite output with input if not specified | ||
if(missing(output)){ | ||
cat("Output folder is not specified, using input folder as output folder") | ||
output <- input | ||
} | ||
}else{ | ||
filelist <- list(input) | ||
|
||
## Extract input folder #### | ||
input <- dirname(input) | ||
|
||
## Output is not specified, but needed | ||
if(missing(output)){ | ||
stop("The output folder is not specified, but needed >> specify output folder") | ||
} | ||
} | ||
|
||
## 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) | ||
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 | ||
} | ||
} | ||
|
||
shape <- sf::st_read(here(input, f)) | ||
|
||
## Check if the shape has a crs #### | ||
if(is.na(sf::st_crs(shape))){ | ||
cat(paste0(f, " has no crs, please provide a crs >> skipping")) | ||
next() | ||
} | ||
|
||
## Check if the crs is not wgs84 #### | ||
if(sf::st_crs(shape)$input != output_crs){ | ||
cat(paste0(f, " is not output crs >> transforming")) | ||
shape <- sf::st_transform(shape, output_crs) | ||
} | ||
|
||
## Write the shape to geojson #### | ||
sf::st_write(shape, here::here(output, output_fn), | ||
driver = "GeoJSON", | ||
overwrite = q_overwrite) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|