Skip to content

Commit

Permalink
fix fgb import failure when fgb doesn't show layer name in metadata (…
Browse files Browse the repository at this point in the history
…slower, but no way to deduce the table name yet from fgb)
  • Loading branch information
MGousseff committed Jun 3, 2024
1 parent f57a7a3 commit 3d069c8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 26 additions & 10 deletions R/importLCZvect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion inst/tinytest/test_importLCZvect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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")
22 changes: 0 additions & 22 deletions submissionJourneesR.rmd

This file was deleted.

0 comments on commit 3d069c8

Please sign in to comment.