-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSS-map-finishing-examples.R
93 lines (56 loc) · 2.29 KB
/
RSS-map-finishing-examples.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
library(sf)
library(raster)
library(link2GI)
library(rgrass7)
# for now, force rgrass7 to use the sp-interface
library(sp)
use_sp()
library(rasterVis)
library(viridisLite)
library(RColorBrewer)
## GRASS, internal connection (start RStudio from GRASS shell)
# /~https://github.com/rsbivand/rgrass7
## GRASS, external connection
# ideas from: https://geocompr.robinlovelace.net/gis.html#rgrass
# kind of slow
gr <- findGRASS()
# find a GRASS 7 installation, and use the first one
ind <- grep("7", gr$version)[1]
# next line of code only necessary if we want to use GRASS as installed by
# OSGeo4W. Among others, this adds some paths to PATH, which are also needed
# for running GRASS.
link2GI::paramGRASSw(gr[ind, ])
grass_path <- ifelse(test = !is.null(gr$installation_type) &&
gr$installation_type[ind] == "osgeo4W",
yes = file.path(gr$instDir[ind], "apps/grass", gr$version[ind]),
no = gr$instDir)
## temporary location for HOME and GRASS "database"
td <- tempdir()
# spurious error in print.gmeta() doesn't affect subsequent commands
# /~https://github.com/rsbivand/rgrass7/issues/31
# throw-away location/mapset used to bootstrap the process
initGRASS(gisBase = grass_path,
home = td,
gisDbase = td, location = "garbage",
mapset = "PERMANENT", override = TRUE)
execGRASS('r.in.gdal', flags = c('overwrite'), parameters = list(input = 'grids/rss_utm.tif', output = 'rss', location = 'utm'))
## switch to PCS location/mapset
initGRASS(gisBase = grass_path,
home = td,
gisDbase = td, location = "utm",
mapset = "PERMANENT", override = TRUE)
execGRASS('g.region', flags = c('a', 'p'), parameters = list(raster = 'rss'))
execGRASS('r.info', parameters = list(map = 'rss'))
# https://grass.osgeo.org/grass80/manuals/r.reclass.area.html
execGRASS('r.reclass.area', flags = c('d', 'overwrite'), parameters = list(input = 'rss', output = 'rss_c', mode = 'lesser', method = 'rmarea', value = 0.5))
x <- raster(readRAST('rss_c'))
x <- ratify(x)
levelplot(
x,
att = 'ID',
scales = list(draw = FALSE),
col.regions = viridis,
margin = FALSE,
main = 'Map Finishing (1ha)'
)
writeRaster(x, file = 'grids/rss_gen.tif', datatype = 'INT1U', options = 'COMPRESS=LZW', overwrite = TRUE)