JosiahParry / sfconversions

Convert {sf} geometry to Rust native primitives
12 stars 2 forks source link

Support sfc_to_geoms and function naming #8

Closed latot closed 6 months ago

latot commented 6 months ago

Hi, the actual functions like sfc_to_geom works from a List, not from a sfc column, which is pretty weird... that would be something sf_to_geoms.

At the same point, would be great have a function for sfc, to convert a sfc (column) to a Vec<Option<Geometry>>.

Thx!

JosiahParry commented 6 months ago

The function does work on an sfc column. Note that an sfc is a list. Please provide a repro if you think it does not work.

JosiahParry commented 6 months ago

The function you are looking for also does exist:

https://github.com/JosiahParry/sfconversions/blob/fce8e5b944a742b66881bf5763e114868d3b52d6/src/fromsf.rs#L58-L81

pub fn sfc_to_geometry(x: List) -> Vec<Option<Geometry>> {
    x
        .into_iter()
        .map(|(_, robj)| {
            let geo = sfg_to_geom(robj);
            match geo {
                Ok(g) => Some(g.geom),
                Err(_) => None
            }
        }).collect::<Vec<Option<Geometry>>>()
}
latot commented 6 months ago

Hi! after check this, you are right, and there is some concerns about it.

When we parse a SF object to Rust, it is a List, which means each column is a Robj, so, with this we can parse from R to Rust a column, but we can't convert a SF object from Rust to Rust, there is no intuitive implementation of convert a Robj column to List again...

Would it be better use a Robj instead of a List as input to be able to be compatible from both places? (R2R and Rust2Rust)

JosiahParry commented 6 months ago

I'm sorry, I do not follow the logic. You should use sf::st_geometry() to get the geometry out of sf object. To get an Robj into a List you can use List::try_from(x).unwrap()

Or you can use geoarrow-r and arrow-extendr to extract everything as a recordbatch in arrow-rs. Then the geometry column can be iterated over using geo-types if you'd like.