Module contributed by Matteo Ghetta - funded by Scuola Superiore Sant’Anna
Scrivere script di R in Processing potrebbe essere leggermente complicato a causa della sintassi che deve essere adottata.
Ogni script inizia con Input e Output preceduto da ##
.
Prima di specificare gli input puoi anche impostare il gruppo degli algoritmi nel quale il tuo script sarà inserito. Se il gruppo esiste già, l’algoritmo sarà aggiunto agli altri, altrimenti sarà automaticamente creato un nuovo gruppo:
creazione del gruppo, ##My Group=group
Then you have to specify all the input types and eventually the additional parameters. You can have different inputs:
vettore, ##Layer = vector
Campo del vettore, ##F = Field Layer
(dove Layer è il nome del vettore in ingresso)
##r = raster
##t = table
numero, ##Num = number
stringa, ##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 ;
:
##type=selection point;lines;point+lines
Come per gli input, ogni output deve essere definito all’inizio dello script:
vettore, ##output= output vector
##output= output raster
##output= output table
##showplots
Per l’output di R nel Visualizzatore Risultati, inserisci all’interno dello script >
prima dell’output che vuoi visualizzare
Il corpo dello script segue la sintassi di R e il pannello di Log può aiutarti se qualcosa non funziona nel tuo script.
Ricorda che nello script devi caricare tutte le librerie aggiuntive:
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))
e ottieni attraverso le linee:
Point pattern analysis
è il gruppo dell’algoritmo
Layer
is the vettore in ingresso
Size
è il parametro numerico con un valore predefinito di 10
Output
è il vettore che sarà creato dall’algoritmo
library(sp)
carica la libreria sp (che dovrebbe essere già installata sul tuo computer e la cui installazione deve essere fatta in R)
spsample
function of the sp
library and pass it to all the input defined abovecrea il vettore in uscita con la funzione SpatialPointsDataFrame
Fatto! Esegui semplicemente l’algoritmo con un vettore a disposizione nella Legenda di QGIS, scegli il numero di punti casuali e li visualizzerai nell’Area di Mappa di QGIS.
Lo script seguente eseguirà un kriging ordinario di base e creerà una mappa raster dei valori interpolati:
##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
da un vettore e il suo campo in ingresso l’algoritmo userà la funzione autoKrige` del pacchetto di R ``automap
e inizialmente calcolerà il modello di kriging e successivamente creerà un raster.
Il raster è creato con la funzione raster
del pacchetto raster di R.
Modifichiamo l’algoritmo Summary Statistics
in modo che l’output sia un file tabella (csv).
Il corpo dello script è il seguente:
##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
La terza linea specifica il Vector Field in ingresso e la quarta linea dice all’algoritmo che l’output sarà una tabella.
L’ultima linea utilizzerà l’oggetto Stat
creato nello script e lo convertirà in una tabella csv
.
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:
>
that tells Processing to print the object
in the result viewerCreating 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.