From 3d069c845b78d4d83c45d388ff9c0dfa4a335edd Mon Sep 17 00:00:00 2001 From: MGousseff Date: Mon, 3 Jun 2024 12:34:53 +0200 Subject: [PATCH] fix fgb import failure when fgb doesn't show layer name in metadata (slower, but no way to deduce the table name yet from fgb) --- DESCRIPTION | 2 +- R/importLCZvect.R | 36 +++++++++++++++++++++--------- inst/tinytest/test_importLCZvect.R | 12 +++++++++- submissionJourneesR.rmd | 22 ------------------ 4 files changed, 38 insertions(+), 34 deletions(-) delete mode 100644 submissionJourneesR.rmd diff --git a/DESCRIPTION b/DESCRIPTION index 0b70242..1904965 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,7 +9,7 @@ Description: This lczexplore package automatize the comparison of sets of local License: LGPL (>= 3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Imports: RColorBrewer, cowplot, dplyr, diff --git a/R/importLCZvect.R b/R/importLCZvect.R index 97b2f66..d5a4a51 100644 --- a/R/importLCZvect.R +++ b/R/importLCZvect.R @@ -44,17 +44,33 @@ importLCZvect<-function(dirPath, file="rsu_lcz.geojson", output="sfFile", column colonnes<-colonnes[sapply(colonnes,nchar)!=0] # Check if all the desired columns are present in the source file and only loads the file if the columns exist + ### DOESN'T WORK WITH flatgeobuffer nom<-gsub(pattern="(.+?)(\\.[^.]*$|$)",x=file,replacement="\\1") - query<-paste0("select * from ",nom," limit 0") - sourceCol<-st_read(dsn=fileName, query=query, quiet=!verbose) %>% names - inCol<-colonnes%in%sourceCol - badCol<-colonnes[!inCol] - colErr<-c("It seems that some of the columns you try to import do not exist in the source file, - are you sure you meant ", - paste(badCol)," ?") - if (prod(inCol)==0){ stop(colErr) } else { - if (drop== TRUE) {sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,colonnes] } else { - sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,]} + extension<-gsub(pattern="(.+?)(\\.[^.]*$|$)",x=file,replacement="\\2") + if (extension != ".fgb"){ # Some metadata for fgb files do not specify table/layer names + query<-paste0("select * from ",nom," limit 0") # So this query wouldn't work with such fgb files + sourceCol<-st_read(dsn=fileName, query=query, quiet=!verbose) %>% names + inCol<-colonnes%in%sourceCol + badCol<-colonnes[!inCol] + colErr<-c("It seems that some of the columns you try to import do not exist in the source file, + are you sure you meant ", + paste(badCol),"?") + if (prod(inCol)==0){ stop(colErr) } else { + if (drop== TRUE) {sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,colonnes] } else { + sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,]} + } + } else {if (extension == ".fgb") { + sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,] + sourceCol<-names(sfFile) + inCol<-colonnes%in%sourceCol + badCol<-colonnes[!inCol] + colErr<-c("It seems that some of the columns you try to import do not exist in the source file, + are you sure you meant ", + paste(badCol),"?") + if (prod(inCol)==0){ stop(colErr) } + + } + } # if typeLevels is empty diff --git a/inst/tinytest/test_importLCZvect.R b/inst/tinytest/test_importLCZvect.R index 7598ac9..48b09dc 100644 --- a/inst/tinytest/test_importLCZvect.R +++ b/inst/tinytest/test_importLCZvect.R @@ -105,4 +105,14 @@ expect_warning(importLCZvect(dirPath=paste0(system.file("extdata", package = "lc test<-importLCZvect(dirPath=paste0( system.file("extdata", package = "lczexplore"),"/bdtopo_2_2/Redon"),file="rsu_lcz.geojson", column="LCZ_PRIMARY", geomID="ID_RSU", confid="LCZ_UNIQUENESS_VALUE", drop=FALSE) -expect_equal("LCZ_SECONDARY"%in%names(test),TRUE) \ No newline at end of file +expect_equal("LCZ_SECONDARY"%in%names(test),TRUE) + +# Special test with a flatgeobuffer file. +test<-importLCZvect(dirPath=paste0(system.file("extdata", package = "lczexplore"),"/bdtopo_2_2/Redon"), + column="LCZ_PRIMARY",geomID="ID_RSU",confid="LCZ_UNIQUENESS_VALUE",verbose=T) +if (file.exists("test.fgb")) file.remove("test.fgb") +write_sf(test, "test.fgb") +expect_silent(test<-importLCZvect(dirPath=getwd(),file="test.fgb", + column="LCZ_PRIMARY",geomID="ID_RSU",confid="LCZ_UNIQUENESS_VALUE",verbose=T)) +rm(test) +if (file.exists("test.fgb")) file.remove("test.fgb") diff --git a/submissionJourneesR.rmd b/submissionJourneesR.rmd deleted file mode 100644 index dd6665d..0000000 --- a/submissionJourneesR.rmd +++ /dev/null @@ -1,22 +0,0 @@ --- -title: "R Notebook" -output: html_notebook ---- - -The [R plugin](https://www.jetbrains.com/help/pycharm/r-plugin-support.html) for IntelliJ-based IDEs provides -handy capabilities to work with the [R Markdown](https://www.jetbrains.com/help/pycharm/r-markdown.html) files. -To [add](https://www.jetbrains.com/help/pycharm/r-markdown.html#add-code-chunk) a new R chunk, -position the caret at any line or the code chunk, then click "+". - -The code chunk appears: -```{r} -``` - -Type any R code in the chunk, for example: -```{r} -mycars <- within(mtcars, { cyl <- ordered(cyl) }) -mycars -``` - -Now, click the **Run** button on the chunk toolbar to [execute](https://www.jetbrains.com/help/pycharm/r-markdown.html#run-r-code) the chunk code. The result should be placed under the chunk. -Click the **Knit and Open Document** to build and preview an output.