#geospatial #conversion #geometry #geo-types #package #sf #primitive

sfconversions

Support conversion from R's sf package types to geo_types geometry primitives using extendr

3 releases (breaking)

0.3.0 Dec 2, 2024
0.2.0 Aug 22, 2023
0.1.0 Mar 12, 2023

#76 in Geospatial

Download history 16/week @ 2024-09-22 28/week @ 2024-09-29 2/week @ 2024-10-06 2/week @ 2024-11-17 3/week @ 2024-11-24 185/week @ 2024-12-01 13/week @ 2024-12-08

203 downloads per month

MIT license

27KB
539 lines

sfconversions

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.

Example

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.

Dependencies

~13MB
~252K SLoC