Geographic Information Systems Asked by Nino Pierantonio on May 14, 2021
I am using my own R scripts in QGIS. Before adapting them to work in QGIS, I write them in RStudio to make sure they work properly. I then use different scripts in a batch model. In RStudio I have loaded the same files from the same directories that I have also loaded in QGIS. With my code I load shapefiles and rasterfiles, make some operations, save some output shapefiles and rasterfiles and then automatically produce a TIFF image and save it. The filename of TIFF includes part of the original filename of one of the shapefiles loaded into R. When I run my code in Rstudio, everything works as expected. When adapting the code to QGIS, the TIFF does not have part of the original filename of the desired shapefile, instead it is replaced by a "NA". The whole script produces otherwise the expected results. For the love of money I cannot figure what goes wrong there. My code is:
#load my files
shp<-readOGR("myshapefile.shp")
rast<-raster("myrasterfile.TIFF")
#run my calculations
#save the desired output shapefiles/rasterfiles
#retrieve the original filename of the shapefile loaded as "shp"
ab<-as.data.frame(shp)
abc<-rrapply(as.list(.GlobalEnv), classes = "data.frame", how = "flatten", dfaslist = FALSE) #from package rrapply which as far as I understand does not work with S4 class objects, that's why of previous command
outfile <-paste0("NewName_", names(abc[1]), ".tiff") #I have also tried with other numbers for names(abc[]) just to make sure I wasn't searching in the wrong place, but nothing changes
#set the TIFF parameters
tiff(filename=outfile,
res = 800,
compression = "lzw",
width = 5,
height = 5,
units = "in"
)
#make the plot
The saved TIFF should be named "NewName_myshapefile.tiff" and it is instead named "NewName_NA.tiff" and this is because the variable abc is empty, in fact, if I run
names(abc)
I get an NA as result. In R I do not have this issue and everything goes as expected. I am 100% positive that my model is not the source of error, if I run my script alone I get the same result.
How do I solve this?
EDIT
based on my comment to @Spacedman, here is my new code to generate the desired names for the output TIFF files:
packages <- c("rgdal", "adehabitatHR", "raster", "rasterVis", "sp", "sf", "lattice", "viridis", "spData")
package.check <- lapply(
packages,
FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
}
)
#make lists of raster and vector layers from which Iwill be matching the name to generate the output TIFF filename
rlist = list.files(getwd(), pattern = "tif$", full.names = FALSE)
shplist = list.files(getwd(), pattern = "shp$", full.names = FALSE)
#run my calculations
#save the desired output shapefiles/rasterfiles
#retrieve the original filename
numb<-grep("mypatternforthenamegoeshere", shplist)
outfile <-paste0("Newmane_", as.character(shplist[numb]), ".tiff")
tiff(filename=outfile,
res = 800,
compression = "lzw",
width = 5,
height = 5,
units = "in"
)
#make my plot
When I adapt the script to work in QGIS, amongst my input files I add a string character field where I type the pattern I want to match in the list, job done. In QGis syntax this is done by declaring at the beginning of the script:
##Make_name_based_on=string
Therefore, in my edited code above, the line:
numb<-grep("mypatternforthenamegoeshere", shplist)
will become:
numb<-grep(Make_name_based_on, shplist)
This gives me what I needed.
You can't get the filename from an sp
class object. Here are two identical shapefiles with different names, read in with readOGR
:
> library(rgdal)
> s1 = readOGR("./testa.shp")
OGR data source with driver: ESRI Shapefile
Source: "/nobackup/rowlings/Downloads/SO/sim/testa.shp", layer: "testa"
with 3 features
It has 1 fields
> s2 = readOGR("./testc.shp")
OGR data source with driver: ESRI Shapefile
Source: "/nobackup/rowlings/Downloads/SO/sim/testc.shp", layer: "testc"
with 3 features
It has 1 fields
and they are identical:
> identical(s1,s2)
[1] TRUE
which means there's no difference due to the filename.
You need to keep the shapefile name somewhere when you read it in so you can use that value when you write out the TIFF.
Answered by Spacedman on May 14, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP