7 Tips and Tricks
Following the update of road segments using the ALSroads
package, users may wish to edit data, filter results, or plot outcomes. Here, we include code snippets to provide users with basic data manipulation and interpretation tools.
In the following sections, users are recommended to refer to the package manuals for additional information. When using functions in R users can type ?function_name()
into the R console to find information about the input data requireed, and example applications or that function. For example, running ?measure_roads()
in the console will bring up information about the function.
7.1 Casting Roads to Linestring
Existing road networks may be multistring features; the measure_roads()
function requires that roads are linestrings. Changing multistring features to linestrings requires the st_cast()
function from the sf
package (refer to the sf vignette for detailed information).
library("sf")
<- st_cast(roads, "LINESTRING") roads
7.2 Adding an ID Column
Existing road networks frequently include FID and ID columns that help users select individual roads. In some cases, however, road networks have been edited or updated, and the ID/FID columns are missing information.
Users may wish to add a new ID column that relates explicitly to the roads on which the ALSroads
method is applied. In that case, users can add and populate a new column using the rowid_to_column
function from the tibble
package (refer to the tibble vignette for detailed information).
library("tribble")
<- tibble::rowid_to_column(roads, "NEW_ID") roads
7.3 Selecting Specific Roads
Users may wish to update only selected roads from the existing road network, not the entire network. Users can select specific roads using the filter
function from the dplyr
package (refer to the dplyr vignette for detailed information).
Users can select roads using an ID column where row values are unique. In this case, we use the “NEW_ID” column (see section 7.2).
Users can select a single road:
library("dplyr")
<- roads %>%
road_subset ::filter(NEW_ID %in% c(10)) dplyr
Multiple roads:
library("dplyr")
<- roads %>%
road_subset ::filter(NEW_ID %in% c(5,10,15)) dplyr
Or, a series of roads (all roads between 10 and 50 will be selected):
library("dplyr")
<- roads %>%
road_subset ::filter(NEW_ID %in% c(10:50)) dplyr
7.4 Plotting Data
7.4.1 Interactive Spatial Plotting with mapView()
:
Users working within R can produce interactive spatial plots using the function mapView()
from the package mapview
(refer to the mapview vignette). The mapView()
function produces an interactive view of spatial object(s) on top of a base map.
library("mapview")
library("leaflet")
= "https://servicesmatriciels.mern.gouv.qc.ca:443/erdas-iws/ogc/wmts/Inventaire_Ecoforestier/Inventaire_Ecoforestier/default/GoogleMapsCompatibleExt2:epsg:3/{z}/{y}/{x}.jpg"
url
<- mapview::mapview(list(existing_road, updated_road),
m layer.name = c("Existing", "Updated"),
color = c("red", "blue"), map.type = "Esri.WorldImagery")
::addTiles(m@map, url) leaflet
7.4.2 Spatial Plotting with plot()
:
For generic plotting, users can use the plot()
function, which is included in base R.
plot(updated_road[1]) #[1] specifies that only the first attribute will plot
7.4.3 Plotting with ggplot()
:
ggplot2
is a plotting package that supports the creation of complex plots. ggplot2
is commonly used, in R, to produce publication-quality plots. For detailed information on using ggplot2
, we recommend users refer to the ggplot2 website. See section 4.4 for more examples of plotting road attributes using ggplot2
.
library("ggplot2")
ggplot(data=updated_roads, aes(x=CLASS, y= SCORE, fill=CLASS)) +
geom_boxplot(size = 0.25, color="black", coef = 1, outlier.size = 0.5)+
labs(y = "Road Score", x = "Road class")+
theme(text = element_text(size = 16))
7.4.4 Plotting LiDAR data:
The lidR
package takes advantage of the rgl
package to provide an interactive 3D viewer with points coloured by Z coordinates. The lidR
package provides several functions for plotting LiDAR data, that include: overlay plotting and tree-top plotting. We strongly recommend that users interested in plotting LiDAR data refer to the lidR book.
library("lidR")
plot(las)
7.4.5 3D Plotting Raster Data:
rayshader
is an open source package for producing 2D and 3D data visualizations in R. rayshader
uses elevation data in a base R matrix and a combination of raytracing, hillshading algorithms, and overlays to generate 2D and 3D maps (refer to the rayshader website for more information).
library(rayshader)
<- raster_to_matrix(dtm)
elmat <- elmat %>%
map sphere_shade(texture = "imhof1", progbar = FALSE) %>%
add_water(detect_water(elmat), color = "imhof1") %>%
add_shadow(ray_shade(elmat, progbar = FALSE), 0.5) %>%
add_shadow(ambient_shade(elmat, progbar = FALSE), 0)
plot_3d(map, elmat, zscale = 20, windowsize = c(800, 800))
7.5 Saving Data
Following the extraction of roads from an ALS point cloud and the update of the existing road network, users will need to save the new network as a file that can be opened in R and other GIS programs, such as ArcGIS.
To save simple features (lines and polygons), users can use the ‘st_write’ function from the ‘sf’ package (refer to the sf vignette for detailed information).
library("sf")
st_write(road_subset, "path/to/folder/roads_subset.shp")
If users have produced raster data files while working with ALSroads
, for example a DTM. In that case, this data can be saved using the writeRaster
function from the raster
package (refer to the raster manual for detailed information).
library("raster")
writeRaster(DTM, "path/to/folder/DTM.tif", format = "GTiff")
7.6 Reconnecting Roads
The updated road network will include all of the roads in the existing road network. However, because the method relocates each road, the updated network may appear disconnected, with road ends no longer perfectly aligning. The st_snap_lines()
function was added to the ALSroads
package to address this and ensure that the updated road network is topologically valid.
This post-processing step involves the “snapping” of road ends together, with or without prior knowledge of road connections. If users provide the existing road network to the st_snap_lines()
, the connection of roads will be more accurate. Users can also specify tolerance (distance) between two roads where snapping will occur.
library("ALSroads")
<- st_snap_lines(roads = updated_roads, ref = existing_roads, field = "NEW_ID", tolerance = 30) snapped_roads
library("mapview")
library("leaflet")
<- "https://servicesmatriciels.mern.gouv.qc.ca:443/erdas-iws/ogc/wmts/Inventaire_Ecoforestier/Inventaire_Ecoforestier/default/GoogleMapsCompatibleExt2:epsg:3/{z}/{y}/{x}.jpg"
url
<- mapview::mapview(list(updated_roads_, snapped_roads$roads),
m layer.name = c("Snapped", "Updated"),
color = c("green", "red"),
lwd = c(3, 1.5),
map.type = "Esri.WorldImagery")
::addTiles(m@map, url) leaflet
Other post-processing functions included in the ALSroads
package are:
check_road_differences
: Check the amplitude of differences between corrected and uncorrected roads.st_check_crossings
: Check if roads cross at points other than at their ends.st_check_junctions
: Check if road endings are close enough to be considered potential junctions.
7.7 RStudio Shortcuts
ctrl+shift+C
: Comment lines in and out.alt + -
: Shortcut for<-
.ctrl-1
andcrtl-2
: Shift the cursor focus to script or console, respectively.ctrl-shift-d
: Duplicate the current line or selected text.ctrl-shift-arrow
: Highlight chunks of text.ctrl-D
: Delete Line.alt+shift+K
: View and edit keyboard shortcuts.
#> <STYLE type='text/css' scoped>
#> PRE.fansi SPAN {padding-top: .25em; padding-bottom: .25em};
#> </STYLE>