Module contributed by Matteo Ghetta - funded by Scuola Superiore Sant’Anna
Escrever rotinas R no Processamento pode ser bastante complicado por causa da sintaxe que deve ser adotada.
Cada script começa com ** Input ** e ** Output ** precedido de `` ## ``.
Antes de especificar as entradas você também pode definir o grupo de algoritmos em que o script será colocado. Se o grupo já existir, o algoritmo será adicionado ao outro, caso contrário um novo grupo será automaticamente criado:
Criar Grupo, ##My Group=group
Then you have to specify all the input types and eventually the additional parameters. You can have different inputs:
vetor, ##Layer = vector
Campo de Vetor, ##F = Field Layer (Local do nome da camada de entrada)
número, ##Num = number
Carácter, ##Str = string
booleano , ##Bol = boolean
you can also have a dropdown menu with all the parameters you want; the items must be separated with semicolons ;:
Quanto às entradas, cada saída tem de ser definida no início do script:
vetor, ##output= output vector
The script body follows an R style syntax and the Log panel can help you if something went wrong with your script.
Remember that in the script you have to load all the additional libraries:
library(sp)
Let’s take an algorithm from the online collection that creates random points from the extent of an input layer:
##Point pattern analysis=group
##Layer=vector
##Size=number 10
##Output= output vector
library(sp)
pts=spsample(Layer,Size,type="random")
Output=SpatialPointsDataFrame(pts, as.data.frame(pts))
and get through the lines:
That’s it! Just run the algorithm with a vector layer you have in the QGIS Legend, choose a number of the random point and you will get them in the QGIS Map Canvas.
The following script will perform a basic ordinary kriging and will create a raster map of the interpolated values:
##Basic statistics=group
##Layer=vector
##Field=Field Layer
##Output=output raster
require("automap")
require("sp")
require("raster")
table=as.data.frame(Layer)
coordinates(table)= ~coords.x1+coords.x2
c = Layer[[Field]]
kriging_result = autoKrige(c~1, table)
prediction = raster(kriging_result$krige_output)
Output<-prediction
from a vector and its field in input the algorithm will use the autoKrige function of the automap R package and it will first calculate the kriging model and then create a raster.
The raster is created with the raster function of the raster R package.
Let’s edit the Summary Statistics algorithm so that the output is a table file (csv).
The script body is the following:
##Basic statistics=group
##Layer=vector
##Field=Field Layer
##Stat=Output table
Summary_statistics<-data.frame(rbind(
sum(Layer[[Field]]),
length(Layer[[Field]]),
length(unique(Layer[[Field]])),
min(Layer[[Field]]),
max(Layer[[Field]]),
max(Layer[[Field]])-min(Layer[[Field]]),
mean(Layer[[Field]]),
median(Layer[[Field]]),
sd(Layer[[Field]])),row.names=c("Sum:","Count:","Unique values:","Minimum value:","Maximum value:","Range:","Mean value:","Median value:","Standard deviation:"))
colnames(Summary_statistics)<-c(Field)
Stat<-Summary_statistics
The third line specifies the Vector Field in input and the fourth line tells the algorithm that the output should be a table.
The last line will take the Stat object created in the script and convert it into a csv table.
We can take the previous example and instead of creating a table, print the result in the Result Viewer:
##Basic statistics=group
##Layer=vector
##Field=Field Layer
Summary_statistics<-data.frame(rbind(
sum(Layer[[Field]]),
length(Layer[[Field]]),
length(unique(Layer[[Field]])),
min(Layer[[Field]]),
max(Layer[[Field]]),
max(Layer[[Field]])-min(Layer[[Field]]),
mean(Layer[[Field]]),
median(Layer[[Field]]),
sd(Layer[[Field]])),row.names=c("Sum:","Count:","Unique values:","Minimum value:","Maximum value:","Range:","Mean value:","Median value:","Standard deviation:"))
colnames(Summary_statistics)<-c(Field)
>Summary_statistics
The script is exactly the same of above with just 2 edits:
Creating plots is very simple. You have to use the ##showplots parameter as the following script shows:
##Basic statistics=group
##Layer=vector
##Field=Field Layer
##showplots
qqnorm(Layer[[Field]])
qqline(Layer[[Field]])
the script takes a field of the vector layer in input and creates a QQ Plot to test the normality of the distribution.
The plot is automatically added to the Result Viewer of Processing.