1

I need to develop .rsx scripts for QGIS 2.0.1 Modeler to do statistical elaborations on data tables of loaded vector layers.

In this case I am not interested in accessing shapefile geometries, I'd like R to read layer table as a dataframe.

Unfortunately the only way (I know) to read data from the table of a vector layer in .rsx scripts is to state:

##vector_layer=vector

And then use the following R code

vector_df=as.data.frame(vector_later)

The OGR library used to access vector layers in R is very slow for shapefiles with many features.

Is there any other method to make R read the data table of a loaded vector layer in QGIS without accessing geometries?

Something like read.dbf() {foreign library} ....

3

What happens when you run a .rsx script ?

You must examine the Rutils.py script

It creates a temporary R script with the desired command (/.qgis2/processing/processing_script.r) and use the Python module subprocess to execute it

 command = "R CMD BATCH --vanilla " + RUtils.getRScriptFilename() + " "+ RUtils.getConsoleOutputFilename()
 proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True)

When you write

##vector_layer=vector
vector_df=as.data.frame(vector_later)

In the temporary script, it is

options("repos"="http://cran.at.r-project.org/")
tryCatch(find.package("rgdal"), error=function(e) install.packages("rgdal",     dependencies=TRUE))
tryCatch(find.package("raster"), error=function(e) install.packages("raster", dependencies=TRUE))
library("raster")
library("rgdal")
Layer = readOGR("directory",layer="vector_layer")

and the result is a Spatial Dataframe (look at Things I Forget: Reading a Shapefile in R with readOGR)

Therefore, it uses R with the rgdal package, by default, to read the original shapefile and not the ogr library of QGIS.

Thus, it is a problem of rgdal and even if you use other R packages, as for example:

##Point pattern analysis=group
##Layer=vector
library("maptools")
library("spatstat")
ppp=as(as(Layer, "SpatialPoints"),"ppp")

the resulting temporary file still use rgdal to read the shapefile:

options("repos"="http://cran.at.r-project.org/")
tryCatch(find.package("maptools"), error=function(e) install.packages("maptools", dependencies=TRUE))
tryCatch(find.package("spatstat"), error=function(e) install.packages("spatstat", dependencies=TRUE))
tryCatch(find.package("rgdal"), error=function(e) install.packages("rgdal", dependencies=TRUE))
tryCatch(find.package("raster"), error=function(e) install.packages("raster", dependencies=TRUE))
library("raster")
library("rgdal")
Layer = readOGR("directory",layer="layer")

In conclusion,If you are not satisfied, use R directly or Python.

1

I think you can save the attributes table of your shapefile as a .csv table (Save as -> Choose Format .csv) and then make a Qgis-R script that accepts a table as input:

##Folder= folder
##Table=table
setwd(Folder)
Table*2->table_out
write.table(table_out,"table_out.csv",row.names=F)

Note that Qgis-R can not create "table" output (http://hub.qgis.org/issues/8919), so as a workaround you need the make the script export the table to a user defined output folder.

0

Check dbfpy at http://dbfpy.sourceforge.net/ it's made specific for DBF files. An alternative is OGR or PyShp.

  • 1
    I think mic_cord wanted a solution in R not python – Curlew Feb 6 '14 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.