Skip to contents

These functions convert R objects to Esri json representations. There are three types of representations. These are, from smallest to largest, a geometry object, a feature and a feature set.

Usage

as_esri_geometry(x, crs = 4326, ..., call = rlang::caller_env())

as_esri_features(x, ..., call = rlang::caller_env())

as_esri_featureset(x, ...)

as_geometry(x, crs, ...)

as_features(x, ..., call = rlang::caller_env())

as_featureset(x, ...)

Arguments

x

an sf or sfc class object

crs

the coordinate reference system of the FeatureSet. Must be interpretable by sf::st_crs()

...

additional arguments passed on to methods.

call

The execution environment of a currently running function, e.g. call = caller_env(). The corresponding function call is retrieved and mentioned in error messages as the source of the error.

You only need to supply call when throwing a condition from a helper function which wouldn't be relevant to mention in the message.

Can also be NULL or a defused function call to respectively not display any call or hard-code a code to display.

For more information about error calls, see Including function calls in error messages.

Value

a json Esri geometry object

Details

The _esri_ infix indicates that the input object will be converted directly into the Esri JSON representation. If there is no _esri_ infix, the object will be converted into the appropriate list structure requiring only jsonify::to_json(x, unbox = TRUE) to convert to Esri JSON.

  • as_esri_geometry() converts an sfg object to a geometry object

  • as_esri_features() converts an sfc or sf object to a list of features

  • as_esri_featureset() converts an sf, sfc, or data.frame to a feature set object

Geometry object contain the coordinates for a geometry. Features are geometries that have associated attributes with them. This would be similar to a row in an sf object. FeatureSets are a list of features that have additional metadata fields such as spatialReference, geomtryType, and fields. FeatureSets correspond to an sf object.

Geometry objects are defined for 5 different types. These are:

  • Point: esriGeometryPoint

  • Multipoint: esriGeometryMultipoint

  • Polyline: esriGeometryPolyline

    • note that polyline encompasses both LINESTRING and MULTILINESTRING

  • Polygon: esriGeometryPolygon

    • note that polygon encompasses both POLYGON and MULTIPOLYGON

  • Envelope: esriGeometryEnvelope

    • envelopes correspond with bounding boxes but can have a Z dimension

Field handling:

  • Vectors that inherit Date or POSIXt are converted into milliseconds since the Unix epoch in UTC timezone using date_to_ms().

  • factors are converted to character vectors using as.character() to match the behavior in infer_esri_type() which defines type mapping for Esri field types and R vector classes.

Examples

library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
as_esri_geometry(st_point(c(0, 1, 3, 4)))
#> Registered S3 method overwritten by 'jsonify':
#>   method     from    
#>   print.json jsonlite
#> [1] "{\"hasZ\":true,\"hasM\":true,\"x\":0.0,\"y\":1.0,\"z\":3.0,\"m\":4.0,\"spatialReference\":{\"wkid\":4326}}"
as_esri_geometry(st_multipoint(x = matrix(runif(4), ncol = 2)))
#> [1] "{\"hasZ\":false,\"hasM\":false,\"points\":[[0.08075013756752014,0.6007608862128109],[0.8343330372590572,0.15720844152383507]],\"spatialReference\":{\"wkid\":4326}}"
as_esri_geometry(st_linestring(x = matrix(runif(2), ncol = 2)))
#> [1] "{\"hasZ\":false,\"hasM\":false,\"paths\":[[0.007399441208690405,0.46639349730685356]],\"spatialReference\":{\"wkid\":4326}}"
as_esri_geometry(st_linestring(x = matrix(runif(2), ncol = 2)))
#> [1] "{\"hasZ\":false,\"hasM\":false,\"paths\":[[0.4977773886639625,0.28976724459789696]],\"spatialReference\":{\"wkid\":4326}}"

# polygon
m <- matrix(
  c(0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 2, 2, 1, 2, 3, 1, 3, 2, 0, 0, 0),
  ncol = 3,
  byrow = TRUE
)
as_esri_geometry(st_polygon(list(m)))
#> [1] "{\"hasZ\":true,\"hasM\":false,\"rings\":[[[0.0,0.0,0.0],[0.0,0.0,1.0],[0.0,1.0,1.0],[1.0,2.0,2.0],[1.0,2.0,3.0],[1.0,3.0,2.0],[0.0,0.0,0.0]]],\"spatialReference\":{\"wkid\":4326}}"