3

I constructed a simple R script for QGIS modeler (.rsx) that calculates groupwise summary statistics of a vector layer table (sum by field "subject" and "FRC"). The script correctly works. When executed, a windows is opened and you can manually choose the vector layer and the other parameters. However, if I insert this script in the graphical modeler I am able to choose the output of a previous Algorithm as input to my script, nevertheless I cannot select fields from its attribute table, as requested by my script.

How can I do that?

----Edit-----

As an example, consider I want to construct a model that calculates the % of different land use types inside polygon buffers constructed around some street addresses.

The model represented in the picture below intersect the buffers with a polygon layer representing land use areas, then calculates the area of each new feature in the intersection layer.

model

Now I want to link my R-script to the output layer "Intersection_with_area" to summarize the total area for each address and for each land use type. Here below the .rsx code:

##intersection=vector
##address=field intersection
##soiltype=field intersection
##area=field intersection
##folder=string
##filename=string

library(doBy)
setwd(as.character(folder))

inters_df=as.data.frame(intersection)

summ=summaryBy(list(area,c(address,soiltype)),FUN=sum, data=inters_df, keep.names=T)
write.csv(summ,as.character(nomefile))

I am unable to select the fields "address", "area" and "soiltype" from the vector layer "Intersection_with_area" that is in my model.

2 Answers 2

2

If you are trying to export the output to a user defined folder, the correct way to do it is by stating "##myfolder=folder"

As for the problems with the Modeler, your R script would have to produce a vector, a raster or an HTML file, in order to the output to be picked up as input for another tool. At the moment it's not possible to create .csv outputs. In your case, it would have to be something like this:

            ##showplots
            ##intersect=vector
            ##subject=field intersect
            ##FRC=field intersect
            ##length=field intersect
            library(doBy)
            inters_df=as.data.frame(intersect)  

            summ=summaryBy(list(length,c(subject,FRC)),FUN=sum, data=inters_df,keep.names=T)      

            >summ

EDIT:

As for the second problem:

It is not possible to link a "Table field" to the output of "Intersection_with_area" because Qgis/Processing can't read which fields are contained in that shapefile. But you can add three "Table field" linked to "landuse" - Address, Soiltype and Area. Then in the Modeler double click on your R script and point its input fields to the "Table fields" you just added before. That way, when you run the Model, you will be prompted to fill them in.

6
  • Thank you @filipe. My R script is at the end of the model chain, so it's ok for me the output be a .csv. My problem is in the selection of vectors as inputs to my script. I've just edited my question, since I verified that I am able to choose a vector from a previous Algorithm as ##intersect , but then I am not able to select its table fields as ##subject and ##FRC
    – mic_cord
    Commented Oct 3, 2013 at 8:18
  • I edited my first answer to answer your second question. Commented Oct 7, 2013 at 16:08
  • Thank you again @Filipe, however I want my R-script to receive as input not a "Vector Layer" Input, but the output of a previous algorithm. I am not able to add a "Table Field" linked to an output of an algorithm in the model. I don't know why, since it is an <OutputVector>, and I can select it as input to another algorithm..
    – mic_cord
    Commented Oct 9, 2013 at 8:54
  • I'm not sure I understand the problem. Can you provide a more detailed description of what you are trying to do? Namely what's the input file, what do you want to do with it and what your R script is suppose to do. Commented Oct 11, 2013 at 15:52
  • Dear @Filipe, I edited my question. I hope the example is now clear..
    – mic_cord
    Commented Oct 14, 2013 at 8:28
2

I've taken another look at this and I think I found a better solution:

First, I created an R script similar to the one you created. It summarizes a numerical field (e.g. area) by two factor fields (e.g buffer ID and landuse type). The output is a .dbf which is written to a location defined by the user in "folder". In the window it will say "folder" is optional, but that's not correct (QGIS bug).

##Layer=vector
##Numeric_field=Field Layer
##Factor1=Field Layer
##Factor2=Field Layer
##Folder=folder
library(doBy)
library(plyr)
library(foreign)
setwd(Folder)
table1<-data.frame(num=Layer[[Numeric_field]],f1=Layer[[Factor1]],f2=Layer[[Factor2]])
output<-summaryBy(num~f1+f2, data=table1,FUN=c(sum,mean,var))
output1<-rename(output,c("num.sum"="Sum","num.mean"="Mean","num.var"="Variance","f1"=Factor1,"f2"=Factor2))
write.dbf(output1,file=paste("summarize_output", ".dbf", sep = ""))

The second step is to incorporate this script in a model:

enter image description here

You need to add four "Table field" parameters and set the Landuse layer as the parent algorithm:

enter image description here

Finally, you have to link each "Table field" parameter to the corresponding field in the R script:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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