# clear R memory # rm(list = ls()) # db_connect.r source("P:/db_connect.r") # import libraries library(sf) library(tidyverse) options(scipen = 1000000) options(digits = 6) # setwd #knitr:opts_knit$set(root.dir = paste0("L:/STAT/08_DS/04_Auftraege/2019/", auftragnummer)) getwd() year = 2018
gem <- st_read(paste0("L:/STAT/08_DS/03_GIS/Geodaten/", year, "/UP_GEMEINDEN_SEEN_F_KTZH_", year, ".shp"), stringsAsFactors = FALSE, crs=2056)
gem_ohneExklEnkl <- gem %>% dplyr::select(-ARPS, - BEZIRKSNAM, - AREA_ROUND, -SHAPE_AREA, -SHAPE_LEN) %>% mutate( GEMEINDENA = ifelse(ART_CODE == 2 & BFS == 196, "Uster", ifelse(ART_CODE == 2 & BFS == 58,"Eglisau", ifelse(ART_CODE == 4,"Unterengstringen", GEMEINDENA))), BFS = ifelse(ART_CODE == 2 & BFS == 196, 198, ifelse(ART_CODE == 2 & BFS == 58, 55, ifelse(ART_CODE == 4, 249, BFS))), ART_TEXT = ifelse(ART_CODE == 2, "Gemeinde", ifelse(ART_CODE == 2,"Gemeinde", ifelse(ART_CODE == 4,"Gemeinde", ART_TEXT))), ART_CODE = ifelse(ART_CODE == 2, 1, ifelse(ART_CODE == 4,1, ART_CODE)) ) %>% filter(ART_CODE == 1) %>% group_by(BFS) %>% summarize( GEMEINDENA = unique(GEMEINDENA), ART_CODE = unique(ART_CODE), ART_TEXT = unique(ART_TEXT) ) View(gem_ohneExklEnkl %>% arrange(BFS)) #check: should be == 166 nrow(gem_ohneExklEnkl) names(gem_ohneExklEnkl)
t<- gem %>% filter(GEMEINDENA %in% c("Zürichsee")) tt <- st_coordinates(st_geometry(t)) ttt <- as.data.frame(tt) # table(ttt$L1); table(ttt$L2) tttt <- ttt[which(ttt$L1 == 1),] plot(as.matrix(tttt)) lnd_poly = st_polygon(list(as.matrix(tttt[,1:2]))) lnd_geom = st_sfc(lnd_poly, crs = 2056) lnd_attrib = t %>% st_set_geometry(NULL) Zuerichsee = st_sf(lnd_attrib, geometry = lnd_geom) %>% dplyr::select(-ARPS, - BEZIRKSNAM, - AREA_ROUND, -SHAPE_AREA, -SHAPE_LEN) plot(st_geometry(t), border= "blue") plot(st_geometry(Zuerichsee), border= "red", add = TRUE) View(Zuerichsee) names(Zuerichsee)
SeeOhneZuerischsee <- gem %>% filter(ART_CODE %in% c(3) & !(GEMEINDENA %in% c("Zürichsee"))) %>% dplyr::select(-ARPS, - BEZIRKSNAM, - AREA_ROUND, -SHAPE_AREA, -SHAPE_LEN)
result <- rbind(gem_ohneExklEnkl, Zuerichsee, SeeOhneZuerischsee) %>% mutate( GDE_ID = as.integer(BFS), GDE_N = GEMEINDENA, ART_N = ART_TEXT ) %>% dplyr:: select(-ART_CODE, -GEMEINDENA, -ART_TEXT, - BFS) %>% mutate( GDE_N = ifelse(ART_N == "See", "See", GDE_N), GDE_ID = ifelse(ART_N == "See", NA, GDE_ID) ) View(result) # check should be == 173 # Gemeinde = 166 # See = 6 nrow(result)
st_write(result, paste0("L:/STAT/08_DS/03_GIS/Admin/Geodaten_Admin/Temp/GemeindeGrosseSeeOhneExklave_gen_epsg2056_F_KTZH_",year,".shp"), delete_layer = TRUE) # in order tooverwrite the geojson, the geojson has to be deleted # delete_layer = TRUE does not work path_2056 = paste0("C:/gitrepos/Ressources_Maps/", year, "/GemeindeGrosseSeeOhneExklave_gen_epsg2056_F_KTZH_", year, ".json") file.remove(path_2056) st_write(result, path_2056, driver="GeoJSON") path_4326 = paste0("C:/gitrepos/Ressources_Maps/", year, "/GemeindeGrosseSeeOhneExklave_gen_epsg4326_F_KTZH_",year, ".json") file.remove(path_4326) st_write(result %>% st_transform(4326), path_4326, driver="GeoJSON")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.