g_measures: Compute measurements for WKB/WKT geometries

g_measuresR Documentation

Compute measurements for WKB/WKT geometries

Description

These functions compute measurements for geometries. The input geometries may be given as a single raw vector of WKB, a list of WKB raw vectors, or a character vector containing one or more WKT strings.

Usage

g_area(geom, quiet = FALSE)

g_centroid(geom, quiet = FALSE)

g_distance(geom, other_geom, quiet = FALSE)

g_length(geom, quiet = FALSE)

g_geodesic_area(geom, srs, traditional_gis_order = TRUE, quiet = FALSE)

g_geodesic_length(geom, srs, traditional_gis_order = TRUE, quiet = FALSE)

Arguments

geom

Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings.

quiet

Logical value, TRUE to suppress warnings. Defaults to FALSE.

other_geom

Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings. Must contain the same number of geometries as geom.

srs

Character string specifying the spatial reference system for geom. May be in WKT format or any of the formats supported by srs_to_wkt().

traditional_gis_order

Logical value, TRUE to use traditional GIS order of axis mapping (the default) or FALSE to use authority compliant axis order. By default, input geom vertices are assumed to be in longitude/latitude order if srs is a geographic coordinate system. This can be overridden by setting traditional_gis_order = FALSE.

Details

These functions use the GEOS library via GDAL headers.

g_area() computes the area for a Polygon or MultiPolygon. Undefined for all other geometry types (returns zero). Returns a numeric vector, having length equal to the number of input geometries, containing computed area or '0' if undefined.

g_centroid() returns a numeric vector of length 2 containing the centroid (X, Y), or a two-column numeric matrix (X, Y) with number of rows equal to the number of input geometries. The GDAL documentation states "This method relates to the SFCOM ISurface::get_Centroid() method however the current implementation based on GEOS can operate on other geometry types such as multipoint, linestring, geometrycollection such as multipolygons. OGC SF SQL 1.1 defines the operation for surfaces (polygons). SQL/MM-Part 3 defines the operation for surfaces and multisurfaces (multipolygons)."

g_distance() returns the distance between two geometries or -1 if an error occurs. Returns the shortest distance between the two geometries. The distance is expressed into the same unit as the coordinates of the geometries. Returns a numeric vector, having length equal to the number of input geometry pairs, containing computed distance or '-1' if an error occurs.

g_length() computes the length for LineString or MultiCurve objects. Undefined for all other geometry types (returns zero). Returns a numeric vector, having length equal to the number of input geometries, containing computed length or '0' if undefined.

g_geodesic_area() computes geometry area, considered as a surface on the underlying ellipsoid of the SRS attached to the geometry. The returned area will always be in square meters, and assumes that polygon edges describe geodesic lines on the ellipsoid. If the geometry SRS is not a geographic one, geometries are reprojected to the underlying geographic SRS. By default, input geometry vertices are assumed to be in longitude/latitude order if using a geographic coordinate system. This can be overridden with the traditional_gis_order argument. Returns the area in square meters, or NA in case of error (unsupported geometry type, no SRS attached, etc.) Requires GDAL >= 3.9.

g_geodesic_length() computes the length of the curve, considered as a geodesic line on the underlying ellipsoid of the SRS attached to the geometry. The returned length will always be in meters. If the geometry SRS is not a geographic one, geometries are reprojected to the underlying geographic SRS. By default, input geometry vertices are assumed to be in longitude/latitude order if using a geographic coordinate system. This can be overridden with the traditional_gis_order argument. Returns the length in meters, or NA in case of error (unsupported geometry type, no SRS attached, etc.) Requires GDAL >= 3.10.

Note

For g_distance(), geom and other_geom must contain the same number of geometries (i.e., operates pair-wise on the inputs with no recycling), and are assumed to be in the same coordinate reference system.

Geometry validity is not checked. In case you are unsure of the validity of the input geometries, call g_is_valid() before, otherwise the result might be wrong.

Examples

g_area("POLYGON ((0 0, 10 10, 10 0, 0 0))")

g_centroid("POLYGON ((0 0, 10 10, 10 0, 0 0))")

g_distance("POINT (0 0)", "POINT (5 12)")

g_length("LINESTRING (0 0, 3 4)")

f <- system.file("extdata/ynp_fires_1984_2022.gpkg", package = "gdalraster")
lyr <- new(GDALVector, f, "mtbs_perims")

# read all features into a data frame
feat_set <- lyr$fetch(-1)
head(feat_set)

g_area(feat_set$geom) |> head()

g_centroid(feat_set$geom) |> head()

lyr$close()

gdalraster documentation built on June 8, 2025, 12:37 p.m.