Skip to content
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

101 new function shape to geojson #102

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fistools
Title: Tools & data used for wildlife management & invasive species in Flanders
Version: 1.2.15
Version: 1.2.16
Authors@R: c(
person(given = "Sander", middle = "", family = "Devisscher", "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2015-5731")),
Expand Down Expand Up @@ -37,4 +37,5 @@ Imports:
sp (>= 2.1.4),
mapview (>= 2.11.2),
osmdata (>= 0.2.5),
readr (>= 2.1.5)
readr (>= 2.1.5),
here (>= 1.0.1)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(qd_pci1)
export(qd_pci2)
export(qd_pci2_D)
export(rename_ct_files)
export(shape_to_geojson)
export(sunsetter)
export(sunsetter2)
importClassesFrom(sp,CRS)
Expand Down
147 changes: 147 additions & 0 deletions R/shape_to_geojson.R
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 <- list(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)
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)$input != 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)
}
}






3 changes: 2 additions & 1 deletion man/CRS_extracter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/apply_grtsdb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/calculate_polygon_centroid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/collect_osm_features.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions man/shape_to_geojson.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading