line_spacing: Adjust line vertex spacing

Description Usage Arguments Value Note Examples

View source: R/mesh.R

Description

Function harmonises the lengths of the segments of lines, i.e. the spacing of vertices.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
line_spacing(x, s, output = c("df", "sp"), ...)

## S3 method for class 'data.frame'
line_spacing(
  x,
  s,
  output = c("df", "sp"),
  ...,
  col_x = "x",
  col_y = "y",
  col_line = "line"
)

## S3 method for class 'numeric'
line_spacing(x, s, output = c("df", "sp"), ..., y, line = NULL)

## S3 method for class 'matrix'
line_spacing(x, s, output = c("df", "sp"), ..., line = NULL)

## S3 method for class 'SpatialLines'
line_spacing(x, s, output = "sp", ...)

## S3 method for class 'SpatialLinesDataFrame'
line_spacing(x, s, output = "sp", ...)

Arguments

x

Either: a data.frame with vertex coordinates and line identifier; a numeric vector with the x coordinates of line vertices; a matrix with two columns, the x and y coordinates of line vertices; an object of class SpatialLines*.

s

numeric value giving the target spacing of line vertices (i.e. line segment lengths) in units of the input coordinates.

output

Return either: "df", a data.frame (default) or "sp", an object of class SpatialLines.

...

Arguments passed to or from other methods.

col_x

If x is a data.frame: column with the x coordinates.

col_y

If x is a data.frame: column with the y coordinates.

col_line

If x is a data.frame: column with the line identifier.

y

The y coordinates of line vertices (if x is a numeric vector).

line

numeric vector of identifiers to distinguish individual lines via index (or row) in x (only needed if x is a vector or matrix and more than one line is given).

Value

If output == "df": a data.frame (or tibble) with elements x, y, and line (or col_x, col_y, col_line) defining the harmonised line(s).

If output == "sp": a SpatialLines object of the harmonised line(s).

Note

If x is a data.frame, all input arguments referring to columns of x support quasiquotation, i.e. they can be specified by their names, quoted or unquoted, or as column position.

Examples

 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
# one line given as numeric vectors
x = c(1,1.5,2.5,3,3.2,5, 5.8, 6.5, 7, 6.7, 6, 5.5, 4.8, 4.3, 4, 4, 4.3)
y = c(1,1.8,2,2.8,3.3,4.5, 4.2, 3.8, 3, 2.7, 2.5, 2.5, 2.2, 2.5, 2.8, 3.2, 3.4)
plot(x, y, pch = 16, asp = 1)
lines(x, y)
ldf_harm <- line_spacing(x = x, y = y, s = 1)
points(ldf_harm$x, ldf_harm$y, pch = 16, col = "red")
lines(ldf_harm$x, ldf_harm$y, col = "red")

# two lines in a matrix
x2 = c(10, 12, 12.2, 11.5, 11.4, 12.5, 13.5, 14)
y2 = c(4, 4.2, 3.5, 3.3, 2.5, 2.2, 2.5, 3)
lmat <- rbind(cbind(x, y), cbind(x2, y2))
line <- c(rep(1, length(x)), rep(2, length(x2)))
plot(x, y, pch = 16, xlim = c(min(lmat[,1]), max(lmat[,1])),
     ylim = c(min(lmat[,2]), max(lmat[,2])), asp = 1)
points(x2, y2, pch = 16)
lines(x, y)
lines(x2, y2)
ldf_harm <- line_spacing(x = lmat, line = line, s = 1)
lh1 <- ldf_harm[ldf_harm$line == 1,]
lh2 <- ldf_harm[ldf_harm$line == 2,]
points(lh1$x, lh1$y, pch = 16, col = "red")
lines(lh1$x, lh1$y, col = "red")
points(lh2$x, lh2$y, pch = 16, col = "red")
lines(lh2$x, lh2$y, col = "red")

# data.frame
library(dplyr)
line1 <- data.frame(
  xcoord = x,
  ycoord = y
)
line2 <- data.frame(
  xcoord = x2,
  ycoord = y2
)
ldf <- bind_rows(line1, line2, .id = "id")
plot(line1, pch = 16, xlim = c(min(ldf$x), max(ldf$x)),
     ylim = c(min(ldf$y), max(ldf$y)), asp = 1)
points(line2, pch = 16)
lines(line1)
lines(line2)
ldf_harm <- line_spacing(ldf, s = 1, col_x = xcoord, col_y = ycoord, col_line = id)
line1_harm <- ldf_harm %>%
  filter(id == 1) %>%
  dplyr::select(xcoord, ycoord)
line2_harm <- ldf_harm %>%
  filter(id == 2) %>%
  dplyr::select(xcoord, ycoord)
points(line1_harm, pch = 16, col = "red")
points(line2_harm, pch = 16, col = "red")
lines(line1_harm, col = "red")
lines(line2_harm, col = "red")

# SpatialLines object
library(sp)
sl <- SpatialLines(list(Lines(Line(line1), 1), Lines(Line(line2), 2)))
plot(sl, asp = 1)
sl_harm <- line_spacing(sl, s = 1, output = "sp")
lines(sl_harm, col = "red")

telemac documentation built on Feb. 7, 2022, 5:06 p.m.