Learn R Programming

sf (version 0.5-5)

geos_measures: Compute geometric measurements

Description

Compute Euclidian or great circle distance between pairs of geometries, the area of a geometry, or the length of a geometry.

Usage

st_area(x)

st_length(x, dist_fun = geosphere::distGeo)

st_distance(x, y, dist_fun, by_element = FALSE)

Arguments

x

object of class sf, sfc or sfg

dist_fun

function to be used for great circle distances of geographical coordinates; 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, defaults to x

by_element

logical; if TRUE, return a vector with distance between the first elements of x and y, the second, etc. if FALSE, return the dense matrix with all pairwise distances.

Value

If the coordinate reference system of x was set, these functions return values with unit of measurement; see set_units.

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. POINT or MULTIPOINT geometries return zero, POLYGON or MULTIPOLYGON are converted into LINESTRING or MULTILINESTRING, respectively.

If by_element is FALSE a dense numeric matrix of dimension length(x) by length(y); otherwise a numeric vector of length x or y, the shorter one being recycled.

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.

See Also

st_dimension

Examples

Run this code
# NOT RUN {
b0 = st_polygon(list(rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1))))
b1 = b0 + 2
b2 = b0 + c(-0.2, 2)
x = st_sfc(b0, b1, b2)
st_area(x)
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)
p = st_sfc(st_point(c(0,0)), st_point(c(0,1)), st_point(c(0,2)))
st_distance(p, p)
st_distance(p, p, by_element = TRUE)
# }

Run the code above in your browser using DataLab