nngeo (version 0.2.8)

st_connect: Create lines between features of two layers

Description

Create lines between features of two layers

Usage

st_connect(x, y, ids = NULL, dist, progress = TRUE, ...)

Arguments

x

Object of class sf or sfc

y

Object of class sf or sfc

ids

A sparse list representation of features to connect such as returned by function st_nn. If NULL the function automatically calculates ids using st_nn

dist

Sampling distance interval for generating outline points to choose from. Required when at least one of x or y is a line or polygon layer. Should be given in CRS units for projected layers, or in meters for layers in lon/lat

progress

Display progress bar? (default TRUE)

...

Other arguments passed to st_nn when calculating ids, such as k and maxdist

Value

Object of class sfc with geometry type LINESTRING

Examples

Run this code
# NOT RUN {
# Nearest 'city' per 'town'
l = st_connect(towns, cities)
plot(st_geometry(towns), col = "darkgrey")
plot(st_geometry(l), add = TRUE)
plot(st_geometry(cities), col = "red", add = TRUE)

# Ten nearest 'towns' per 'city'
l = st_connect(cities, towns, k = 10)
plot(st_geometry(towns), col = "darkgrey")
plot(st_geometry(l), add = TRUE)
plot(st_geometry(cities), col = "red", add = TRUE)

# }
# NOT RUN {
# Nearest 'city' per 'town', search radius of 30 km
cities = st_transform(cities, 32636)
towns = st_transform(towns, 32636)
l = st_connect(cities, towns, k = nrow(towns), maxdist = 30000)
plot(st_geometry(towns), col = "darkgrey")
plot(st_geometry(l), add = TRUE)
plot(st_buffer(st_geometry(cities), units::set_units(30, km)), border = "red", add = TRUE)

# The 20-nearest towns for each water body
water = st_transform(water, 32636)
l = st_connect(water[-1, ], towns, k = 20, dist = 100)
plot(st_geometry(water[-1, ]), col = "lightblue", border = NA)
plot(st_geometry(towns), col = "darkgrey", add = TRUE)
plot(st_geometry(l), col = "red", add = TRUE)


# The 2-nearest water bodies for each town
l = st_connect(towns, water[-1, ], k = 2, dist = 100)
plot(st_geometry(water[-1, ]), col = "lightblue", border = NA)
plot(st_geometry(towns), col = "darkgrey", add = TRUE)
plot(st_geometry(l), col = "red", add = TRUE)

# }

Run the code above in your browser using DataCamp Workspace