Crates.io | sfconversions |
lib.rs | sfconversions |
version | 0.3.0 |
source | src |
created_at | 2023-03-12 14:14:20.534632 |
updated_at | 2024-12-02 22:34:02.46851 |
description | Support conversion from R's sf package types to geo_types geometry primitives using extendr |
homepage | |
repository | https://github.com/josiahparry/sfconversions |
max_upload_size | |
id | 808058 |
size | 27,377 |
A minimal Rust library to convert geometry objects from the R package {sf} into geo-types geometry primitives using extendr.
Provides simple conversions between sfg, sfc objects from sf, and geometry primitives from geo_types that can be used with other georust libraries powered by extendr.
Due to the orphan rule conversion directly from extendr Lists
to geo_types is not possible. For that reason a simple struct Geom
is implemented with a single field geom
which contains a geo_types Geometry
enum.
Basic conversion from sfg objects is done with sfg_to_geom()
.
use sfconversions::{sfg_to_geom, geom::Geom};
use extendr_api::prelude::*;
#[extendr]
fn extract_sfg(x: Robj) -> String {
sfg_to_geom(x).unwrap().print()
}
The Geom
struct is an extendr compatible struct with a single method which prints the string.
sfconversions acts similarly to sfheaders in that it returns the correct R objects with the appropriate classes without dependence upon sf. If sf is not available the conversions still will work but the print methods and other functions from sf will not be available.
It is important to note that sfconversions will only create sfg objects and will not make sfc
class object. This is because sfc
objects require a bounding box attribute which can only be calculated using geo
which is a larger dependency. To create an sfc
object return a List
of sfg
objects and in R use
sf::st_sfc()
to complete the conversion. Use geos_to_sfc()
to aid in this process.