Important
Traducerea este un efort al comunității, la care puteți să vă alăturați. În prezent, această pagină este tradusă 64.39%.
32.4. Anexa D: Sintaxa scrípturilor R din QGIS
Contributed by Matteo Ghetta - funded by Scuola Superiore Sant’Anna
Writing R scripts in Processing is a bit tricky because of the special syntax.
A Processing R script starts with defining its Inputs and
Outputs, each preceded with double hash characters (##
).
Before the inputs, the group to place the algoritm in can be specified. If the group already exists, the algorithm will be added to it, if not, the group will be created. In the example below, the name of the group is My group:
##My Group=group
32.4.1. Intrări
All input data and parameters have to be specified. There are several types of inputs:
vector:
##Layer = vector
vector field:
##F = Field Layer
(where Layer is the name of an input vector layer the field belongs to)raster:
##r = raster
table:
##t = table
number:
##Num = number
string:
##Str = string
boolean:
##Bol = boolean
elements in a dropdown menu. The items must be separated with semicolons
;
:##type=selection point;lines;point+lines
32.4.2. Rezultat
Ca și intrările, fiecare ieșire trebuie să fie definită la începutul script-ului:
vector:
##output= output vector
raster:
##output= output raster
table:
##output= output table
plots:
##output_plots_to_html
(##showplots in earlier versions)To show R output in the Result Viewer, put
>
in front of the command whose output you would like to show.
32.4.3. Syntax Summary for QGIS R scripts
A number of input and output parameter types are offered.
32.4.3.1. Input parameter types
Parametru |
Exemplu de sintaxă |
Obiecte returnate |
---|---|---|
vector |
Layer = vector |
sf object (or SpatialDataFrame object, if ##load_vector_using_rgdal is specified) |
vector point |
Layer = vector point |
sf object (or SpatialDataFrame object, if ##load_vector_using_rgdal is specified) |
vector line |
Layer = vector line |
sf object (or SpatialDataFrame object, if ##load_vector_using_rgdal is specified) |
vector polygon |
Layer = vector polygon |
sf object (or SpatialPolygonsDataFrame object, if ##load_vector_using_rgdal is used) |
multiple vector |
Layer = multiple vector |
sf object (or SpatialDataFrame objects if ##load_vector_using_rgdal is specified) |
tabelă |
Layer = table |
dataframe convertit din csv, obiect implicit al funcției |
field |
Field = Field Layer |
numele câmpului selectat, ex.: |
raster |
Layer = raster |
Obiect RasterBrick, obiect implicit al pachetului |
multiple raster |
Layer = multiple raster |
Obiect RasterBrick, obiect implicit al pachetului |
number |
N = number |
numărul întreg sau zecimal ales |
string |
S = string |
textul adăugat în casetă |
longstring |
LS = longstring |
șirul de caractere adăugat în casetă, ar putea fi mai lung decât șirul normal |
selection |
S = selection first;second;third |
șirul cu elementele care vor putea fi selectate din meniul derulant |
crs |
C = crs |
string of the resulting CRS chosen, in the format: |
extent |
E = extent |
Obiectul extindere din pachetul |
point |
P = point |
când faceți clic pe hartă, puteți vedea coordonatele punctului |
file |
F = file |
calea fișierului ales, ex.: „/home/matteo/file.txt” |
folder |
F = folder |
calea directorului ales, ex.: „/home/matteo/Downloads” |
A parameter can be OPTIONAL, meaning that it can be ignored.
In order to set an input as optional, you add the string optional
before the input, e.g:
##Layer = vector
##Field1 = Field Layer
##Field2 = optional Field Layer
32.4.3.2. Output parameter types
Parametru |
Exemplu de sintaxă |
---|---|
vector |
Output = output vector |
raster |
Output = output raster |
tabelă |
Output = output table |
file |
Output = output file |
Notă
You can save plots as png
from the Processing Result Viewer, or you can choose to
save the plot directly from the algorithm interface.
32.4.3.3. Corpul scriptului
The script body follows R syntax and the Log panel can help you if there is something wrong with your script.
Remember that you have to load all additional libraries in the script:
library(sp)
32.4.4. Exemple
32.4.4.1. Exemplu de ieșire vectorială
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 polygon
##Size=number 10
##Output=output vector
library(sp)
spatpoly = as(Layer, "Spatial")
pts=spsample(spatpoly,Size,type="random")
spdf=SpatialPointsDataFrame(pts, as.data.frame(pts))
Output=st_as_sf(spdf)
Explanation (per line in the script):
Analiza modelului de puncte
reprezintă grupul algoritmuluiStratul
reprezintă stratul de intrare vectorialSize
is a numerical parameter with a default value of 10Ieșire
reprezintă stratul vectorial care va fi creat de către algoritmlibrary(sp)
loads the sp libraryspatpoly = as(Layer, "Spatial")
translate to an sp objectCall the
spsample
function of thesp
library and run it using the input defined above (Layer
andSize
)Create a SpatialPointsDataFrame object using the
SpatialPointsDataFrame
functionCreate the output vector layer using the
st_as_sf
function
That’s it! Just run the algorithm with a vector layer you have in the QGIS Legend, choose the number of random point. The resulting layer will be added to your map.
32.4.4.2. Exemplu de ieșire raster
The following script will perform basic ordinary kriging to
create a raster map of interpolated values from a specified field
of the input point vector layer by using the autoKrige
function of the automap
R package.
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:
##Basic statistics=group
##Layer=vector point
##Field=Field Layer
##Output=output raster
##load_vector_using_rgdal
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
By using ##load_vector_using_rgdal
, the input vector layer
will be made available as a SpatialDataFrame
objects,
so we avoid having to translate it from an sf
object.
32.4.4.3. Exemplu de generare a unui tabel
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.
32.4.4.4. Example with console output
We can use 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 as the one above except for two edits:
no output specified (the fourth line has been removed)
the last line begins with
>
, telling Processing to make the object available through the result viewer
32.4.4.5. Exemplu de grafic
To create plots, you have to use the ##output_plots_to_html
parameter as in the following script:
##Basic statistics=group
##Layer=vector
##Field=Field Layer
##output_plots_to_html
####output_plots_to_html
qqnorm(Layer[[Field]])
qqline(Layer[[Field]])
The script uses a field (Field
) of a vector layer (Layer
) as
input, and creates a QQ Plot (to test the normality of the
distribution).
The plot is automatically added to the Processing Result Viewer.