
Convert geometries to standard interchange formats
Source:R/ddbs_geom_conversion.R
ddbs_as_format.RdConvert spatial geometries to common interchange formats using DuckDB spatial serialization functions.
ddbs_as_text()– Convert geometries to Well-Known Text (WKT)ddbs_as_wkb()– Convert geometries to Well-Known Binary (WKB)ddbs_as_hexwkb()– Convert geometries to hexadecimal Well-Known Binary (HEXWKB)ddbs_as_geojson()– Convert to GeoJSON (aFeatureCollectionor oneFeatureper row)
Usage
ddbs_as_text(x, conn = NULL)
ddbs_as_wkb(x, conn = NULL)
ddbs_as_hexwkb(x, conn = NULL)
ddbs_as_geojson(x, conn = NULL, feature_collection = TRUE)Arguments
- x
Input spatial data. Can be:
A
duckspatial_dfobject (lazy spatial data frame via dbplyr)An
sfobjectA
tbl_lazyfrom dbplyrA character string naming a table/view in
conn
Data is returned from this object.
- conn
A connection object to a DuckDB database. If
NULL, the function runs on a temporary DuckDB database.- feature_collection
Logical, only used by
ddbs_as_geojson(). IfTRUE(default), all rows are returned as a single GeoJSONFeatureCollectionstring. IfFALSE, a character vector with oneFeaturestring per row is returned.
Value
Depending on the function:
ddbs_as_text()returns a character vector of WKT geometriesddbs_as_wkb()returns a list of raw vectors (binary WKB)ddbs_as_hexwkb()returns a character vector of HEXWKB stringsddbs_as_geojson()returns a single GeoJSONFeatureCollectionstring (class"geojson") whenfeature_collection = TRUE, or a character vector ofFeaturestrings (one per row) whenFALSE; non-geometry columns are included as feature properties
Details
These functions are thin wrappers around DuckDB spatial serialization
functions (ST_AsText, ST_AsWKB, ST_AsHEXWKB, and ST_AsGeoJSON).
They are useful for exporting geometries into widely supported formats for interoperability with external spatial tools, databases, and web services.
Unlike the other serializers, which only encode the geometry, ddbs_as_geojson()
produces complete GeoJSON Features: the geometry is placed in the geometry
member and all remaining (non-geometry) columns are included as feature
properties. By default the features are wrapped in a single FeatureCollection,
the canonical GeoJSON artifact for writing .geojson files and feeding web
services and mapping libraries.
Examples
if (FALSE) { # \dontrun{
library(duckspatial)
argentina_ddbs <- ddbs_open_dataset(
system.file("spatial/argentina.geojson", package = "duckspatial")
)
ddbs_as_text(argentina_ddbs)
ddbs_as_wkb(argentina_ddbs)
ddbs_as_hexwkb(argentina_ddbs)
## a single FeatureCollection (default)
ddbs_as_geojson(argentina_ddbs)
## one Feature string per row
ddbs_as_geojson(argentina_ddbs, feature_collection = FALSE)
} # }