as.linnet.SpatialLines.Rd
Convert an object of class SpatialLines
or
SpatialLinesDataFrame
(from package sp),
representing a collection of polygonal lines,
into an object of class linnet
(from package spatstat),
representing a network.
Object of class SpatialLines
or
SpatialLinesDataFrame
to be converted.
output object of class “linnet”.
logical flag. If TRUE, the returned object must be strictly from the target class.
Ignored.
Logical value indicating whether to join different curves which have a common vertex.
This function converts an object of class SpatialLines
or SpatialLinesDataFrame
into an object of class linnet
.
It is not a method for the spatstat generic function
as.linnet
, but like other S4 coercion functions for
sp classes to spatstat classes, it may be called directly as a function.
An object of class SpatialLines
or SpatialLinesDataFrame
(from package sp) represents
a list of lists of the coordinates of lines, such as a list of all roads in
a city. An object of class linnet
in the spatstat package
represents a linear network, such as a road network.
If fuse=FALSE
, each “Line” object in X
will be treated as if it were disconnected from the others.
The result is a network that consists of many disconnected sub-networks,
equivalent to the list of “Line” objects.
If fuse=TRUE
(the default), the code will
search for identical pairs of vertices occurring in different
“Line” objects, and will treat them as identical vertices,
effectively joining the two “Line” objects at this common
vertex.
If X
belongs to class SpatialLinesDataFrame
,
the associated columns of data in the auxiliary data frame
slot(X, "data")
will be copied to the output as the marks
attached to the line segments of the network. See the Examples.
An object of class linnet
.
run <- FALSE
if(require("spatstat.geom", quietly=TRUE) &&
require("spatstat.linnet", quietly=TRUE)) run <- TRUE
#> spatstat.random 3.1-4
#> spatstat.explore 3.1-0
#>
#> Attaching package: ‘spatstat.explore’
#> The following object is masked from ‘package:lattice’:
#>
#> panel.histogram
#> spatstat.model 3.2-1
#>
#> Attaching package: ‘spatstat.model’
#> The following object is masked from ‘package:lattice’:
#>
#> panel.histogram
#> spatstat.linnet 3.0-6
if (run) {
dname <- system.file("shapes", package="maptools")
fname <- file.path(dname, "fylk-val.shp")
fylk <- readShapeSpatial(fname, proj4string=CRS("+proj=utm +zone=33 +ellps=WGS84"))
is.projected(fylk)
}
#> Warning: shapelib support is provided by GDAL through the sf and terra packages among others
#> Warning: shapelib support is provided by GDAL through the sf and terra paackages among others
#> Warning: shapelib support is provided by GDAL through the sf and terra packages among others
#> [1] TRUE
if (run) {
L <- as(fylk, "linnet")
print(max(vertexdegree(L)))
}
#> [1] 3
if (run) {
L0 <- as.linnet.SpatialLines(fylk, fuse=FALSE)
print(max(vertexdegree(L0)))
}
#> Warning: data contain duplicated points
#> [1] 2
if (run) {
## extract data associated with each network segment
head(marks(as.psp(L)))
}
#> FNODE_ TNODE_ LPOLY_ RPOLY_ LENGTH VALINJE_ VALINJE_ID LTEMA VANNBR
#> 0 3 2 2 2 1429.487 1 97 3211 13
#> 1 5 4 2 1 7369.613 2 97 3211 13
#> 1.1 5 4 2 1 7369.613 2 97 3211 13
#> 1.2 5 4 2 1 7369.613 2 97 3211 13
#> 2 7 8 1 2 11040.943 3 64 3211 13
#> 2.1 7 8 1 2 11040.943 3 64 3211 13
#> DATO LinesIndex LineIndex
#> 0 19970630 1 1
#> 1 19970630 2 1
#> 1.1 19970630 2 1
#> 1.2 19970630 2 1
#> 2 19970630 3 1
#> 2.1 19970630 3 1
if (run) {
fname <- file.path(dname, "fylk-val-ll.shp")
fylk <- readShapeSpatial(fname, proj4string=CRS("+proj=longlat +ellps=WGS84"))
is.projected(fylk)
}
#> Warning: shapelib support is provided by GDAL through the sf and terra packages among others
#> Warning: shapelib support is provided by GDAL through the sf and terra paackages among others
#> Warning: shapelib support is provided by GDAL through the sf and terra packages among others
#> [1] FALSE
if (run) {
try(L <- as(fylk, "linnet"))
}
#> Error in as.linnet.SpatialLines(from) :
#> Only projected coordinates may be converted to spatstat class objects