Learn R Programming

sf (version 0.3-4)

geos: Geometric operations on (pairs of) simple feature geometry sets

Description

Geometric operations on (pairs of) simple feature geometry sets

Usage

st_is_valid(x)

st_dimension(x, NA_if_empty = TRUE)

st_area(x)

st_length(x, dist_fun = geosphere::distGeo)

st_is_simple(x)

st_distance(x, y, dist_fun)

st_relate(x, y)

st_intersects(x, y, sparse = TRUE, prepared = TRUE)

st_disjoint(x, y, sparse = TRUE, prepared = TRUE)

st_touches(x, y, sparse = TRUE, prepared = TRUE)

st_crosses(x, y, sparse = TRUE, prepared = TRUE)

st_within(x, y, sparse = TRUE, prepared = TRUE)

st_contains(x, y, sparse = TRUE, prepared = TRUE)

st_contains_properly(x, y, sparse = TRUE, prepared = TRUE)

st_overlaps(x, y, sparse = TRUE, prepared = TRUE)

st_equals(x, y, sparse = TRUE, prepared = FALSE)

st_covers(x, y, sparse = TRUE, prepared = TRUE)

st_covered_by(x, y, sparse = TRUE, prepared = TRUE)

st_equals_exact(x, y, par, sparse = TRUE, prepared = FALSE)

st_buffer(x, dist, nQuadSegs = 30)

st_boundary(x)

st_convex_hull(x)

st_simplify(x, preserveTopology = FALSE, dTolerance = 0)

st_triangulate(x, dTolerance = 0, bOnlyEdges = FALSE)

st_voronoi(x, envelope, dTolerance = 0, bOnlyEdges = FALSE)

st_polygonize(x)

st_linemerge(x)

st_centroid(x)

st_segmentize(x, dfMaxLength, ..., warn = TRUE)

st_combine(x)

st_intersection(x, y)

st_difference(x, y)

st_sym_difference(x, y)

st_union(x, y, ..., by_feature = FALSE)

st_line_sample(x, n, density, type = "regular")

Arguments

x
object of class sf, sfc or sfg
NA_if_empty
logical; if TRUE, return NA for empty geometries
dist_fun
function to be used for great circle distances; for unprojected (long/lat) data, this should be a distance function of package geosphere, or compatible to that; it defaults to distGeo in that case; for other data metric lengths are computed.
y
object of class sf, sfc or sfg
sparse
logical; should a sparse matrix be returned (TRUE) or a dense matrix?
prepared
logical; prepare geometry for x, before looping over y?
par
numeric; parameter used for "equals_exact" (margin) and "is_within_distance"
dist
numeric; buffer distance for all, or for each of the elements in x
nQuadSegs
integer; number of segments per quadrant (fourth of a circle)
preserveTopology
logical; carry out topology preserving simplification?
dTolerance
numeric; tolerance parameter
bOnlyEdges
logical; if TRUE, return lines, else return polygons
envelope
object of class sfc or sfg with the envelope for a voronoi diagram
dfMaxLength
numeric; max length of a line segment
...
ignored
warn
logical; generate a warning in case of long/lat data
by_feature
logical; if TRUE, union each feature, if FALSE return a single feature with the union the set of features
n
integer; number of points to choose per geometry; if missing, n will be computed as round(density * st_length(geom)).
density
numeric; density (points per distance unit) of the sampling, possibly a vector of length equal to the number of features (otherwise recycled).
type
character; indicate the sampling type, either "regular" or "random"

Value

matrix (sparse or dense); if dense: of type character for relate, numeric for distance, and logical for all others; matrix has dimension x by y; if sparse (only possible for those who return logical in case of dense): return list of length length(x) with indices of the TRUE values for matching y. st_dimension returns 0 for points, 1 for lines, 2 for surfaces and by default NA for empty geometries. st_area returns the area of a geometry, in the coordinate reference system used; in case x is in degrees longitude/latitude, areaPolygon is used for area calculation. st_length returns the length of a LINESTRING or MULTILINESTRING geometry, using the coordinate reference system used; if the coordinate reference system of x was set, the returned value has a unit of measurement. st_is_simple and st_is_valid return a logical vector st_distance returns a dense numeric matrix of dimension length(x) by length(y) st_relate returns a dense character matrix; element [i,j] has nine characters, refering to the DE9-IM relationship between x[i] and y[j], encoded as IxIy,IxBy,IxEy,BxIy,BxBy,BxEy,ExIy,ExBy,ExEy where I refers to interior, B to boundary, and E to exterior, and e.g. BxIy the dimensionality of the intersection of the the boundary of x[i] and the interior of y[j], which is one of 0,1,2,F, digits denoting dimensionality, F denoting not intersecting. st_intersects ... st_equals_exact return a sparse or dense logical matrix with rows and columns corresponding to the number of geometries (or rows) in x and y, respectively st_buffer ... st_segmentize return an sfc or an sf object with the same number of geometries as in x All functions (or methods) returning a geometry return an object of the same class as that of the first argument (x). st_intersection, st_union, st_difference and st_sym_difference return the non-empty geometries resulting from applying the operation to all geometry pairs in x and y, and return an object of class sfg, sfc or sf, where in the latter case the matching attributes of the original object(s) are added. The sfc geometry list-column returned carries an attribute idx, which is an n x 2 matrix with every row the index of the corresponding entries of x and y, respectively. st_union has in addition the ability to work on a single argument x (y missing): in this case, if by_feature is FALSE all geometries are unioned together and an sfg or single-geometry sfc object is returned, if by_feature is TRUE each feature geometry is unioned; this can for instance be used to resolve internal boundaries after polygons were combined using st_combine. st_union(x) unions geometries. Unioning a set of overlapping polygons has the effect of merging the areas (i.e. the same effect as iteratively unioning all individual polygons together). Unioning a set of LineStrings has the effect of fully noding and dissolving the input linework. In this context "fully noded" means that there will be a node or endpoint in the output for every endpoint or line segment crossing in the input. "Dissolved" means that any duplicate (e.g. coincident) line segments or portions of line segments will be reduced to a single line segment in the output. Unioning a set of Points has the effect of merging al identical points (producing a set with no duplicates).

Details

function dist_fun should follow the pattern of the distance function distGeo: the first two arguments must be 2-column point matrices, the third the semi major axis (radius, in m), the third the ellipsoid flattening. `st_contains_properly(A,B)` is true if A intersects B's interior, but not its edges or exterior; A contains A, but A does not properly contain A. st_triangulate requires GEOS version 3.4 or above st_voronoi requires GEOS version 3.4 or above in case of st_polygonize, x must be an object of class LINESTRING or MULTILINESTRING, or an sfc geometry list-column object containing these in case of st_linemerge, x must be an object of class MULTILINESTRING, or an sfc geometry list-column object containing these st_combine combines geometries without resolving borders.

Examples

Run this code
x = st_sfc(
	st_point(0:1), 
	st_linestring(rbind(c(0,0),c(1,1))), 
	st_polygon(list(rbind(c(0,0),c(1,0),c(0,1),c(0,0)))),
	st_multipoint(),
	st_linestring(),
	st_geometrycollection())
st_dimension(x)
st_dimension(x, FALSE)
dist_vincenty = function(p1, p2, a, f) geosphere::distVincentyEllipsoid(p1, p2, a, a * (1-f), f)
line = st_sfc(st_linestring(rbind(c(30,30), c(40,40))), crs = 4326)
st_length(line)
st_length(line, dist_fun = dist_vincenty)
p1 = st_point(c(0,0))
p2 = st_point(c(2,2))
pol1 = st_polygon(list(rbind(c(0,0),c(1,0),c(1,1),c(0,1),c(0,0)))) - 0.5
pol2 = pol1 + 1
pol3 = pol1 + 2
st_relate(st_sfc(p1, p2), st_sfc(pol1, pol2, pol3))
nc = st_read(system.file("shape/nc.shp", package="sf"))
plot(st_convex_hull(nc))
plot(nc, border = grey(.5))
set.seed(1)
x = st_multipoint(matrix(runif(10),,2))
box = st_polygon(list(rbind(c(0,0),c(1,0),c(1,1),c(0,1),c(0,0))))
if (sf_extSoftVersion()["GEOS"] >= "3.5.0") {
 v = st_sfc(st_voronoi(x, st_sfc(box)))
 plot(v, col = 0, border = 1, axes = TRUE)
 plot(box, add = TRUE, col = 0, border = 1) # a larger box is returned, as documented
 plot(x, add = TRUE, col = 'red', cex=2, pch=16)
 plot(st_intersection(st_cast(v), box)) # clip to smaller box
 plot(x, add = TRUE, col = 'red', cex=2, pch=16)
}
mls = st_multilinestring(list(matrix(c(0,0,0,1,1,1,0,0),,2,byrow=TRUE)))
st_polygonize(st_sfc(mls))
mls = st_multilinestring(list(rbind(c(0,0), c(1,1)), rbind(c(2,0), c(1,1))))
st_linemerge(st_sfc(mls))
plot(nc, axes = TRUE)
plot(st_centroid(nc), add = TRUE, pch = 3)
st_combine(nc)
plot(st_union(nc))
ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))),
	st_linestring(rbind(c(0,0),c(10,0))))
st_line_sample(ls, density = 1)
ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))),
 st_linestring(rbind(c(0,0),c(.1,0))), crs = 4326) 
try(st_line_sample(ls, density = 1/1000)) # error
st_line_sample(st_transform(ls, 3857), n = 5) # five points for each line
st_line_sample(st_transform(ls, 3857), n = c(1, 3)) # one and three points
st_line_sample(st_transform(ls, 3857), density = 1/1000) # one per km
st_line_sample(st_transform(ls, 3857), density = c(1/1000, 1/10000)) # one per km, one per 10 km

Run the code above in your browser using DataLab