Title: | Bindings to Selected 'liblwgeom' Functions for Simple Features |
---|---|
Description: | Access to selected functions found in 'liblwgeom' <https://github.com/postgis/postgis/tree/master/liblwgeom>, the light-weight geometry library used by 'PostGIS' <http://postgis.net/>. |
Authors: | Edzer Pebesma [aut, cre] , Colin Rundel [ctb], Andy Teucher [ctb], liblwgeom developers [cph] |
Maintainer: | Edzer Pebesma <[email protected]> |
License: | GPL-2 |
Version: | 0.2-15 |
Built: | 2024-11-18 02:52:57 UTC |
Source: | https://github.com/r-spatial/lwgeom |
Generate the minimum bounding circle
st_minimum_bounding_circle(x, nQuadSegs = 30)
st_minimum_bounding_circle(x, nQuadSegs = 30)
x |
object of class |
nQuadSegs |
number of segments per quadrant (passed to |
st_minimum_bounding_circle
uses the lwgeom_calculate_mbc
method also used by the PostGIS command ST_MinimumBoundingCircle
.
Object of the same class as x
library(sf) x = st_multipoint(matrix(c(0,1,0,1),2,2)) y = st_multipoint(matrix(c(0,0,1,0,1,1),3,2)) mbcx = st_minimum_bounding_circle(x) mbcy = st_minimum_bounding_circle(y) if (.Platform$OS.type != "windows") { plot(mbcx, axes=TRUE); plot(x, add=TRUE) plot(mbcy, axes=TRUE); plot(y, add=TRUE) } nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) state = st_union(st_geometry(nc)) if (.Platform$OS.type != "windows") { plot(st_minimum_bounding_circle(state), asp=1) plot(state, add=TRUE) }
library(sf) x = st_multipoint(matrix(c(0,1,0,1),2,2)) y = st_multipoint(matrix(c(0,0,1,0,1,1),3,2)) mbcx = st_minimum_bounding_circle(x) mbcy = st_minimum_bounding_circle(y) if (.Platform$OS.type != "windows") { plot(mbcx, axes=TRUE); plot(x, add=TRUE) plot(mbcy, axes=TRUE); plot(y, add=TRUE) } nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) state = st_union(st_geometry(nc)) if (.Platform$OS.type != "windows") { plot(st_minimum_bounding_circle(state), asp=1) plot(state, add=TRUE) }
liblwgeom geodetic functions for length, area, segmentizing, covers
st_geod_area(x) st_geod_length(x) st_geod_segmentize(x, max_seg_length) st_geod_covers(x, y, sparse = TRUE) st_geod_covered_by(x, y, sparse = TRUE) st_geod_distance(x, y, tolerance = 0, sparse = FALSE)
st_geod_area(x) st_geod_length(x) st_geod_segmentize(x, max_seg_length) st_geod_covers(x, y, sparse = TRUE) st_geod_covered_by(x, y, sparse = TRUE) st_geod_distance(x, y, tolerance = 0, sparse = FALSE)
x |
object of class |
max_seg_length |
segment length in degree, radians, or as a length unit (e.g., m) |
y |
object of class |
sparse |
logical; if |
tolerance |
double or length |
st_area
will give an error message when the area spans the equator and lwgeom
is linked to a proj.4 version older than 4.9.0 (see lwgeom_extSoftVersion)
longitude coordinates returned are rescaled to [-180,180)
this function should is used by st_distance, do not use it directly
library(sf) nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) st_geod_area(nc[1:3,]) # st_area(nc[1:3,]) l = st_sfc(st_linestring(rbind(c(7,52), c(8,53))), crs = 4326) st_geod_length(l) library(units) pol = st_polygon(list(rbind(c(0,0), c(0,60), c(60,60), c(0,0)))) x = st_sfc(pol, crs = 4326) seg = st_geod_segmentize(x[1], set_units(10, km)) plot(seg, graticule = TRUE, axes = TRUE) pole = st_polygon(list(rbind(c(0,80), c(120,80), c(240,80), c(0,80)))) pt = st_point(c(0,90)) x = st_sfc(pole, pt, crs = 4326) st_geod_covers(x[c(1,1,1)], x[c(2,2,2,2)]) pole = st_polygon(list(rbind(c(0,80), c(120,80), c(240,80), c(0,80)))) pt = st_point(c(30,70)) x = st_sfc(pole, pt, crs = 4326) st_geod_distance(x, x)
library(sf) nc = st_read(system.file("gpkg/nc.gpkg", package="sf")) st_geod_area(nc[1:3,]) # st_area(nc[1:3,]) l = st_sfc(st_linestring(rbind(c(7,52), c(8,53))), crs = 4326) st_geod_length(l) library(units) pol = st_polygon(list(rbind(c(0,0), c(0,60), c(60,60), c(0,0)))) x = st_sfc(pol, crs = 4326) seg = st_geod_segmentize(x[1], set_units(10, km)) plot(seg, graticule = TRUE, axes = TRUE) pole = st_polygon(list(rbind(c(0,80), c(120,80), c(240,80), c(0,80)))) pt = st_point(c(0,90)) x = st_sfc(pole, pt, crs = 4326) st_geod_covers(x[c(1,1,1)], x[c(2,2,2,2)]) pole = st_polygon(list(rbind(c(0,80), c(120,80), c(240,80), c(0,80)))) pt = st_point(c(30,70)) x = st_sfc(pole, pt, crs = 4326) st_geod_distance(x, x)
Provide the external dependencies versions of the libraries linked to sf
lwgeom_extSoftVersion()
lwgeom_extSoftVersion()
Make an invalid geometry valid
lwgeom_make_valid(x)
lwgeom_make_valid(x)
x |
object of class |
Compute perimeter from polygons or other geometries
st_perimeter_lwgeom(x) st_perimeter_2d(x)
st_perimeter_lwgeom(x) st_perimeter_2d(x)
x |
object of class |
numerical vector with perimeter for each feature (geometry), with unit of measure when possible
create sfc object from tiny well-known binary (twkb)
## S3 method for class 'TWKB' st_as_sfc(x, ...)
## S3 method for class 'TWKB' st_as_sfc(x, ...)
x |
list with raw vectors, of class |
... |
ignored |
https://github.com/TWKB/Specification/blob/master/twkb.md
l = structure(list(as.raw(c(0x02, 0x00, 0x02, 0x02, 0x02, 0x08, 0x08))), class = "TWKB") library(sf) # load generic st_as_sfc(l)
l = structure(list(as.raw(c(0x02, 0x00, 0x02, 0x02, 0x02, 0x08, 0x08))), class = "TWKB") library(sf) # load generic st_as_sfc(l)
Return Well-known Text representation of simple feature geometry or coordinate reference system
st_astext(x, digits = getOption("digits"), ..., EWKT = FALSE) st_asewkt(x, digits = options("digits"))
st_astext(x, digits = getOption("digits"), ..., EWKT = FALSE) st_asewkt(x, digits = options("digits"))
x |
object of class |
digits |
integer; number of decimal digits to print |
... |
ignored |
EWKT |
logical; use PostGIS Enhanced WKT (includes srid) |
The returned WKT representation of simple feature geometry conforms to the
simple features access specification and extensions (if EWKT = TRUE
),
known as EWKT, supported by
PostGIS and other simple features implementations for addition of SRID to a WKT string.
st_asewkt()
returns the Well-Known Text (WKT) representation of
the geometry with SRID meta data.
library(sf) pt <- st_sfc(st_point(c(1.0002,2.3030303)), crs = 4326) st_astext(pt, 3) st_asewkt(pt, 3)
library(sf) pt <- st_sfc(st_point(c(1.0002,2.3030303)), crs = 4326) st_astext(pt, 3) st_asewkt(pt, 3)
Check if a POLYGON or MULTIPOLYGON is clockwise, and if not make it so. According to the 'Right-hand-rule', outer rings should be clockwise, and inner holes should be counter-clockwise
st_force_polygon_cw(x)
st_force_polygon_cw(x)
x |
object with polygon geometries |
object of the same class as x
library(sf) polys <- st_sf(cw = c(FALSE, TRUE), st_as_sfc(c('POLYGON ((0 0, 1 0, 1 1, 0 0))', 'POLYGON ((1 1, 2 2, 2 1, 1 1))'))) st_force_polygon_cw(polys) st_force_polygon_cw(st_geometry(polys)) st_force_polygon_cw(st_geometry(polys)[[1]])
library(sf) polys <- st_sf(cw = c(FALSE, TRUE), st_as_sfc(c('POLYGON ((0 0, 1 0, 1 1, 0 0))', 'POLYGON ((1 1, 2 2, 2 1, 1 1))'))) st_force_polygon_cw(polys) st_force_polygon_cw(st_geometry(polys)) st_force_polygon_cw(st_geometry(polys)[[1]])
compute azimuth between sequence of points
st_geod_azimuth(x)
st_geod_azimuth(x)
x |
object of class |
library(sf) p = st_sfc(st_point(c(7,52)), st_point(c(8,53)), crs = 4326) st_geod_azimuth(p)
library(sf) p = st_sfc(st_point(c(7,52)), st_point(c(8,53)), crs = 4326) st_geod_azimuth(p)
compute geohash from (average) coordinates
st_geohash(x, precision = 0) st_geom_from_geohash( hash, precision = -1, crs = st_crs("OGC:CRS84"), raw = FALSE )
st_geohash(x, precision = 0) st_geom_from_geohash( hash, precision = -1, crs = st_crs("OGC:CRS84"), raw = FALSE )
x |
object of class |
precision |
integer; precision (length) of geohash returned. From the liblwgeom source: “where the precision is non-positive, a precision based on the bounds of the feature. Big features have loose precision. Small features have tight precision.” |
hash |
character vector with geohashes |
crs |
object of class 'crs' |
raw |
logical; if 'TRUE', return a matrix with bounding box coordinates on each row |
see http://geohash.org/ or https://en.wikipedia.org/wiki/Geohash.
'st_geohash' returns a character vector with geohashes; empty or full geometries result in 'NA'
'st_geom_from_geohash' returns a (bounding box) polygon for each geohash if 'raw' is 'FALSE', if 'raw' is 'TRUE' a matrix is returned with bounding box coordinates on each row.
library(sf) lwgeom::st_geohash(st_sfc(st_point(c(1.5,3.5)), st_point(c(0,90))), 2) lwgeom::st_geohash(st_sfc(st_point(c(1.5,3.5)), st_point(c(0,90))), 10) st_geom_from_geohash(c('9qqj7nmxncgyy4d0dbxqz0', 'up'), raw = TRUE) o = options(digits = 20) # for printing purposes st_geom_from_geohash(c('9qqj7nmxncgyy4d0dbxqz0', 'u1hzz631zyd63zwsd7zt')) st_geom_from_geohash('9qqj7nmxncgyy4d0dbxqz0', 4) st_geom_from_geohash('9qqj7nmxncgyy4d0dbxqz0', 10) options(o)
library(sf) lwgeom::st_geohash(st_sfc(st_point(c(1.5,3.5)), st_point(c(0,90))), 2) lwgeom::st_geohash(st_sfc(st_point(c(1.5,3.5)), st_point(c(0,90))), 10) st_geom_from_geohash(c('9qqj7nmxncgyy4d0dbxqz0', 'up'), raw = TRUE) o = options(digits = 20) # for printing purposes st_geom_from_geohash(c('9qqj7nmxncgyy4d0dbxqz0', 'u1hzz631zyd63zwsd7zt')) st_geom_from_geohash('9qqj7nmxncgyy4d0dbxqz0', 4) st_geom_from_geohash('9qqj7nmxncgyy4d0dbxqz0', 10) options(o)
Check if a POLYGON or MULTIPOLYGON is clockwise. According to the 'Right-hand-rule', outer rings should be clockwise, and inner holes should be counter-clockwise
st_is_polygon_cw(x)
st_is_polygon_cw(x)
x |
object with polygon geometries |
logical with length the same number of features in 'x'
library(sf) polys <- st_sf(cw = c(FALSE, TRUE), st_as_sfc(c('POLYGON ((0 0, 1 0, 1 1, 0 0))', 'POLYGON ((1 1, 2 2, 2 1, 1 1))'))) st_is_polygon_cw(polys) st_is_polygon_cw(st_geometry(polys)) st_is_polygon_cw(st_geometry(polys)[[1]])
library(sf) polys <- st_sf(cw = c(FALSE, TRUE), st_as_sfc(c('POLYGON ((0 0, 1 0, 1 1, 0 0))', 'POLYGON ((1 1, 2 2, 2 1, 1 1))'))) st_is_polygon_cw(polys) st_is_polygon_cw(st_geometry(polys)) st_is_polygon_cw(st_geometry(polys)[[1]])
get substring from linestring
st_linesubstring(x, from, to, tolerance, ...)
st_linesubstring(x, from, to, tolerance, ...)
x |
object of class |
from |
relative distance from origin (in [0,1]) |
to |
relative distance from origin (in [0,1]) |
tolerance |
tolerance parameter, when to snap to line node node |
... |
ignored |
object of class sfc
library(sf) lines = st_sfc(st_linestring(rbind(c(0,0), c(1,2), c(2,0))), crs = 4326) spl = st_linesubstring(lines, 0.2, 0.8) # should warn plot(st_geometry(lines), col = 'red', lwd = 3) plot(spl, col = 'black', lwd = 3, add = TRUE) st_linesubstring(lines, 0.49999, 0.8) # three points st_linesubstring(lines, 0.49999, 0.8, 0.001) # two points: snap start to second node
library(sf) lines = st_sfc(st_linestring(rbind(c(0,0), c(1,2), c(2,0))), crs = 4326) spl = st_linesubstring(lines, 0.2, 0.8) # should warn plot(st_geometry(lines), col = 'red', lwd = 3) plot(spl, col = 'black', lwd = 3, add = TRUE) st_linesubstring(lines, 0.49999, 0.8) # three points st_linesubstring(lines, 0.49999, 0.8, 0.001) # two points: snap start to second node
Snap geometries to a grid
st_snap_to_grid(x, size, origin)
st_snap_to_grid(x, size, origin)
x |
object with geometries to be snapped |
size |
numeric or (length) units object; grid cell size in x-, y- (and possibly z- and m-) directions |
origin |
numeric; origin of the grid |
object of the same class as x
# obtain data library(sf) x = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)[1, ] %>% st_geometry %>% st_transform(3395) # snap to a grid of 5000 m err = try(y <- st_snap_to_grid(x, 5000)) # plot data for visual comparison if (!inherits(err, "try-error")) { opar = par(mfrow = c(1, 2)) plot(x, main = "orginal data") plot(y, main = "snapped to 5000 m") par(opar) }
# obtain data library(sf) x = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)[1, ] %>% st_geometry %>% st_transform(3395) # snap to a grid of 5000 m err = try(y <- st_snap_to_grid(x, 5000)) # plot data for visual comparison if (!inherits(err, "try-error")) { opar = par(mfrow = c(1, 2)) plot(x, main = "orginal data") plot(y, main = "snapped to 5000 m") par(opar) }
Return a collection of geometries resulting by splitting a geometry
st_split(x, y)
st_split(x, y)
x |
object with geometries to be splitted |
y |
object split with (blade); if |
object of the same class as x
library(sf) l = st_as_sfc('MULTILINESTRING((10 10, 190 190), (15 15, 30 30, 100 90))') pt = st_sfc(st_point(c(30,30))) st_split(l, pt)
library(sf) l = st_as_sfc('MULTILINESTRING((10 10, 190 190), (15 15, 30 30, 100 90))') pt = st_sfc(st_point(c(30,30))) st_split(l, pt)
Return the start and end points from lines
st_startpoint(x) st_endpoint(x)
st_startpoint(x) st_endpoint(x)
x |
line of class |
see https://postgis.net/docs/ST_StartPoint.html and https://postgis.net/docs/ST_EndPoint.html.
sf
object representing start and end points
library(sf) m = matrix(c(0, 1, 2, 0, 1, 4), ncol = 2) l = st_sfc(st_linestring(m)) lwgeom::st_startpoint(l) lwgeom::st_endpoint(l) l2 = st_sfc(st_linestring(m), st_linestring(m[3:1, ])) lwgeom::st_startpoint(l2) lwgeom::st_endpoint(l2)
library(sf) m = matrix(c(0, 1, 2, 0, 1, 4), ncol = 2) l = st_sfc(st_linestring(m)) lwgeom::st_startpoint(l) lwgeom::st_endpoint(l) l2 = st_sfc(st_linestring(m), st_linestring(m[3:1, ])) lwgeom::st_startpoint(l2) lwgeom::st_endpoint(l2)
Return a collection of geometries resulting by subdividing a geometry
st_subdivide(x, max_vertices)
st_subdivide(x, max_vertices)
x |
object with geometries to be subdivided |
max_vertices |
integer; maximum size of the subgeometries (at least 8) |
object of the same class as x
library(sf) demo(nc, ask = FALSE, echo = FALSE) x = st_subdivide(nc, 10) plot(x[1])
library(sf) demo(nc, ask = FALSE, echo = FALSE) x = st_subdivide(nc, 10) plot(x[1])
Transform or convert coordinates of simple features directly with Proj.4 (bypassing GDAL)
st_transform_proj(x, crs, ...) ## S3 method for class 'sfc' st_transform_proj(x, crs, ...) ## S3 method for class 'sf' st_transform_proj(x, crs, ...) ## S3 method for class 'sfg' st_transform_proj(x, crs, ...)
st_transform_proj(x, crs, ...) ## S3 method for class 'sfc' st_transform_proj(x, crs, ...) ## S3 method for class 'sf' st_transform_proj(x, crs, ...) ## S3 method for class 'sfg' st_transform_proj(x, crs, ...)
x |
object of class sf, sfc or sfg |
crs |
character; target CRS, or, in case of a length 2 character vector, source and target CRS |
... |
ignored |
Transforms coordinates of object to new projection, using PROJ directly rather than the GDAL API used by st_transform.
If crs
is a single CRS, it forms the target CRS, and in that case the source CRS is obtained as st_crs(x)
. Since this presumes that the source CRS is accepted by GDAL (which is not always the case), a second option is to specify the source and target CRS as two proj4strings in argument crs
. In the latter case, st_crs(x)
is ignored and may well be NA
.
The st_transform_proj
method for sfg
objects assumes that the CRS of the object is available as an attribute of that name.
library(sf) p1 = st_point(c(7,52)) p2 = st_point(c(-30,20)) sfc = st_sfc(p1, p2, crs = 4326) sfc st_transform_proj(sfc, "+proj=wintri") library(sf) nc = st_read(system.file("shape/nc.shp", package="sf")) st_transform_proj(nc[1,], "+proj=wintri +over") st_transform_proj(structure(p1, proj4string = "+init=epsg:4326"), "+init=epsg:3857")
library(sf) p1 = st_point(c(7,52)) p2 = st_point(c(-30,20)) sfc = st_sfc(p1, p2, crs = 4326) sfc st_transform_proj(sfc, "+proj=wintri") library(sf) nc = st_read(system.file("shape/nc.shp", package="sf")) st_transform_proj(nc[1,], "+proj=wintri +over") st_transform_proj(structure(p1, proj4string = "+init=epsg:4326"), "+init=epsg:3857")
Splits input geometries by a vertical line and moves components falling on one side of that line by a fixed amount
st_wrap_x(x, wrap, move)
st_wrap_x(x, wrap, move)
x |
object with geometries to be split |
wrap |
x value of split line |
move |
amount by which geometries falling to the left of the line should be translated to the right |
object of the same class as x
library(sf) demo(nc, ask = FALSE, echo = FALSE) x = st_wrap_x(nc, -78, 10) plot(x[1])
library(sf) demo(nc, ask = FALSE, echo = FALSE) x = st_wrap_x(nc, -78, 10) plot(x[1])