-
Notifications
You must be signed in to change notification settings - Fork 0
/
RQGIS_script.R
71 lines (49 loc) · 1.8 KB
/
RQGIS_script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
################################################
# Script for tutorial on RQGIS
# Authors: Francesca Mancini
#
#
#
# Date created: 2018-01-17
# Date modefied:
################################################
install.packages("RQGIS")
library(RQGIS)
library(raster)
library(rgdal)
# before we start we need to specify the paths
# necessary to run the QGIS-API
set_env("C:/Program Files/QGIS 2.18")
# downloading UK administrative areas
getData("ISO3")
UK <- getData(name = "GADM", country = "GBR", level = 2, path = "../RQGIS")
plot(UK)
# search for QGIS functions
find_algorithms(search_term = "([Pp]olygon)(centroid)")
# find out how to use the function
get_usage(alg = "qgis:polygoncentroids")
# two ways to specify parameters for the function
# 1
params <- get_args_man(alg = "qgis:polygoncentroids")
params$INPUT_LAYER <- UK
params$OUTPUT_LAYER <- "C:/Users/User/Documents/Study Group tutorials/RQGIS/UK_coords.shp"
centroids <- run_qgis(alg = "qgis:polygoncentroids",
params = params,
load_output = TRUE)
# spits out an error msg but the function ran fine
# 2
centroids <- run_qgis(alg = "qgis:polygoncentroids",
INPUT_LAYER = UK,
OUTPUT_LAYER = "C:/Users/User/Documents/Study Group tutorials/RQGIS/UK_coords.shp",
load_output = TRUE)
# plot
plot(UK)
plot(centroids$geometry, pch = 21, add = TRUE, bg = "lightblue", col = "black")
# select by attributes and save a new shapefile
UK_sub <- UK[UK@data$NAME_1 != "England",]
centroids_sub <- centroids[centroids$NAME_1 != "England",]
plot(UK_sub)
plot(centroids_sub$geometry, pch = 21, add = TRUE, bg = "lightblue", col = "black")
writeOGR(UK_sub, "C:/Users/User/Documents/Study Group tutorials/RQGIS",
"UK_sub.shp",
driver="ESRI Shapefile")