HES 505 Fall 2023: Session 8
Image Source: Wikimedia Commons
By the end of today, you should be able to:
Understand predicates
and measures
in the context of spatial operations in sf
Define valid geometries and approaches for assessing geometries in R
Use st_*
and sf_*
to evaluate attributes of geometries and calculate measurements
sf
package relies on a simple feature data model to represent geometries
type | description |
---|---|
POINT |
single point geometry |
MULTIPOINT |
set of points |
LINESTRING |
single linestring (two or more points connected by straight lines) |
MULTILINESTRING |
set of linestrings |
POLYGON |
exterior ring with zero or more inner rings, denoting holes |
MULTIPOLYGON |
set of polygons |
GEOMETRYCOLLECTION |
set of the geometries above |
You already know how to access some elements of a simple feature
st_crs
- returns the coordinate reference system
st_bbox
- returns the bounding box for the simple feature
We can categorize sf
operations based on what they return and/or how many geometries they accept as input.
Predicates: evaluate a logical statement asserting that a property is TRUE
Measures: return a numeric value with units based on the units of the CRS
Transformations: create new geometries based on input geometries.
Input Geometries
MULTI*
object the function works on each geometry individually)linestring
is simple if it does not intersectEmpty geometries arise when an operation produces NULL
outcomes (like looking for the intersection between two non-intersecting polygons)
sf
allows empty geometries to make sure that information about the data type is retained
Similar to a data.frame
with no rows or a list
with NULL
values
Most vector operations require simple, valid geometries
st_
prefix and return TRUE/FALSE
predicate | asks… |
---|---|
is_simple |
is the geometry self-intersecting (i.e., simple)? |
is_valid |
is the geometry valid? |
is_empty |
is the geometry column of an object empty? |
is_longlat |
does the object have geographic coordinates? (FALSE if coords are projected, NA if no crs ) |
is(geometry, class) |
is the geometry of a particular class? |
any(st_is_empty(x)))
any(is.na(st_is_valid(x)))
any(na.omit(st_is_valid(x)) == FALSE)
; in case of corrupt and/or invalid geometries,st_is_valid(x, reason = TRUE)
Invalid geometries will require transformation (next week!)
logical
outcomespredicate | meaning | inverse of |
---|---|---|
contains |
None of the points of A are outside B | within |
contains_properly |
A contains B and B has no points in common with the boundary of A | |
covers |
No points of B lie in the exterior of A | covered_by |
covered_by |
Inverse of covers |
|
crosses |
A and B have some but not all interior points in common | |
disjoint |
A and B have no points in common | intersects |
equals |
A and B are topologically equal: node order or number of nodes may differ; identical to A contains B AND A within B | |
equals_exact |
A and B are geometrically equal, and have identical node order | |
intersects |
A and B are not disjoint | disjoint |
is_within_distance |
A is closer to B than a given distance | |
within |
None of the points of B are outside A | contains |
touches |
A and B have at least one boundary point in common, but no interior points | |
overlaps |
A and B have some points in common; the dimension of these is identical to that of A and B | |
relate |
given a mask pattern, return whether A and B adhere to this pattern |
Sparse geometry binary predicate list of length 1, where the predicate
was `covers'
1: 1
[,1]
[1,] TRUE
Sparse geometry binary predicate list of length 1, where the predicate
was `within'
1: (empty)
[,1]
[1,] FALSE
Unary Measures
measure | returns |
---|---|
dimension |
0 for points, 1 for linear, 2 for polygons, possibly NA for empty geometries |
area |
the area of a geometry |
length |
the length of a linear geometry |
Unary Measures
st_distance
returns the distance between pairs of geometriesUnits: [m]
[,1]
[1,] 396433.8
Units: [m]
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0 467635.7 277227.0 132998.0 0.0
[2,] 467635.7 0.0 319706.4 656056.0 514306.9
[3,] 277227.0 319706.4 0.0 377105.4 336146.8
[4,] 132998.0 656056.0 377105.4 0.0 133045.5
[5,] 0.0 514306.9 336146.8 133045.5 0.0