Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to extendR on main #43

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ crate-type = [ 'staticlib' ]
name = 'rsgeo'

[dependencies]
extendr-api = { version = "0.6.0" }
extendr-api = { version = "*" }
geo = { version = "0.27.0" }
geo-types = "0.7.8"
rayon = "1.7.0"
rstar = "0.12.0"
sfconversions = { git = "https://github.com/JosiahParry/sfconversions" }
sfconversions = "*"
# sfconversions = { git = "https://github.com/JosiahParry/sfconversions" }


[patch.crates-io]
extendr-api = { git = "https://github.com/extendr/extendr", branch = "extendr_is_singlethreaded" }
sfconversions = { git = "https://github.com/CGMossa/sfconversions.git", branch = "extendr_main" }
geo-types = { git = "https://github.com/georust/geo" }
# geo = { path = "../../../geo/geo" }
geo = { git = "https://github.com/georust/geo" }
Expand Down
12 changes: 6 additions & 6 deletions src/rust/src/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn signed_area(x: List) -> Doubles {
if xi.is_null() {
Rfloat::na()
} else {
let area = <&Geom>::from_robj(&xi).unwrap().geom.signed_area();
let area = <&Geom>::try_from(&xi).unwrap().geom.signed_area();
Rfloat::from(area)
}
})
Expand All @@ -60,7 +60,7 @@ fn unsigned_area(x: List) -> Doubles {
if xi.is_null() {
Rfloat::na()
} else {
let area = <&Geom>::from_robj(&xi).unwrap().geom.unsigned_area();
let area = <&Geom>::try_from(&xi).unwrap().geom.unsigned_area();
Rfloat::from(area)
}
})
Expand All @@ -76,7 +76,7 @@ fn signed_area_cd(x: List) -> Doubles {
if xi.is_null() {
Rfloat::na()
} else {
let area = <&Geom>::from_robj(&xi)
let area = <&Geom>::try_from(&xi)
.unwrap()
.geom
.chamberlain_duquette_signed_area();
Expand All @@ -95,7 +95,7 @@ fn unsigned_area_cd(x: List) -> Doubles {
if xi.is_null() {
Rfloat::na()
} else {
let area = <&Geom>::from_robj(&xi)
let area = <&Geom>::try_from(&xi)
.unwrap()
.geom
.chamberlain_duquette_unsigned_area();
Expand All @@ -114,7 +114,7 @@ fn unsigned_area_geodesic(x: List) -> Doubles {
if xi.is_null() {
Rfloat::na()
} else {
let area = <&Geom>::from_robj(&xi)
let area = <&Geom>::try_from(&xi)
.unwrap()
.geom
.geodesic_area_unsigned();
Expand All @@ -133,7 +133,7 @@ fn signed_area_geodesic(x: List) -> Doubles {
if xi.is_null() {
Rfloat::na()
} else {
let area = <&Geom>::from_robj(&xi).unwrap().geom.geodesic_area_signed();
let area = <&Geom>::try_from(&xi).unwrap().geom.geodesic_area_signed();
Rfloat::from(area)
}
})
Expand Down
47 changes: 22 additions & 25 deletions src/rust/src/boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn bounding_box(x: List) -> Robj {
let bbox = x
.iter()
.fold([f64::MAX, f64::MAX, f64::MIN, f64::MIN], |acc, (_, xi)| {
let g = <&Geom>::from_robj(&xi);
let g = <&Geom>::try_from(&xi);

match g {
Ok(geo) => {
Expand All @@ -74,10 +74,9 @@ fn bounding_box(x: List) -> Robj {

// TODO what if all values are NA? We will be returning massive numbers and that wouldnt be good

Doubles::from_values(bbox)
.into_robj()
.set_names(["xmin", "ymin", "xmax", "ymax"])
.unwrap()
let mut robj = Doubles::from_values(bbox).into_robj();
robj.set_names(["xmin", "ymin", "xmax", "ymax"]).unwrap();
robj
}

#[extendr]
Expand All @@ -89,28 +88,25 @@ fn bounding_boxes(x: List) -> List {
.map(|(_, xi)| {
if x.is_null() {
let bb = [Rfloat::na(); 4];
Doubles::from_values(bb)
.into_robj()
.set_names(["xmin", "ymin", "xmax", "ymax"])
.unwrap()
let mut robj = Doubles::from_values(bb).into_robj();
robj.set_names(["xmin", "ymin", "xmax", "ymax"]).unwrap();
robj
} else {
let bb = Geom::try_from(xi).unwrap().geom.bounding_rect();

match bb {
Some(b) => {
let (xmin, ymin) = b.min().x_y();
let (xmax, ymax) = b.max().x_y();
Doubles::from_values([xmin, ymin, xmax, ymax])
.into_robj()
.set_names(["xmin", "ymin", "xmax", "ymax"])
.unwrap()
let mut robj = Doubles::from_values([xmin, ymin, xmax, ymax]).into_robj();
robj.set_names(["xmin", "ymin", "xmax", "ymax"]).unwrap();
robj
}
None => {
let bb = [Rfloat::na(); 4];
Doubles::from_values(bb)
.into_robj()
.set_names(["xmin", "ymin", "xmax", "ymax"])
.unwrap()
let mut robj = Doubles::from_values(bb).into_robj();
robj.set_names(["xmin", "ymin", "xmax", "ymax"]).unwrap();
robj
}
}
}
Expand All @@ -130,7 +126,7 @@ fn bounding_rect(x: List) -> Robj {
if x.is_null() {
().into_robj()
} else {
let bb = <&Geom>::from_robj(&xi).unwrap().geom.bounding_rect();
let bb = <&Geom>::try_from(&xi).unwrap().geom.bounding_rect();

match bb {
Some(b) => Geom::from(Polygon::from(b)).into_robj(),
Expand All @@ -152,7 +148,7 @@ fn convex_hull(x: List) -> Robj {
if xi.is_null() {
().into_robj()
} else {
let xi = <&Geom>::from_robj(&xi).unwrap().geom.convex_hull();
let xi = <&Geom>::try_from(&xi).unwrap().geom.convex_hull();
Geom::from(xi).into_robj()
}
})
Expand Down Expand Up @@ -190,7 +186,7 @@ fn concave_hull(x: List, concavity: Doubles) -> Robj {
if xi.is_null() || !ci.is_real() {
().into_robj()
} else {
let g = <&Geom>::from_robj(&xi).unwrap();
let g = <&Geom>::try_from(&xi).unwrap();

match &g.geom {
Geometry::LineString(g) => g.concave_hull(ci.inner()).into_geom().into(),
Expand Down Expand Up @@ -219,7 +215,7 @@ fn extreme_coords(x: List) -> List {
if xi.is_null() {
().into_robj()
} else {
let extremes = <&Geom>::from_robj(&xi).unwrap().geom.extremes();
let extremes = <&Geom>::try_from(&xi).unwrap().geom.extremes();
match extremes {
Some(ext) => {
let crds = [
Expand All @@ -229,11 +225,12 @@ fn extreme_coords(x: List) -> List {
Point::from(ext.y_max.coord).into_geom(),
];

List::from_values(crds)
.set_class(geom_class("point"))
let mut robj = List::from_values(crds);
robj.set_class(geom_class("point"))
.unwrap()
.set_names(["xmin", "ymin", "xmax", "ymax"])
.unwrap()
.unwrap();
robj.into_robj()
}
_ => ().into_robj(),
}
Expand Down Expand Up @@ -275,7 +272,7 @@ fn minimum_rotated_rect(x: List) -> Robj {
// if xi.is_null() {
// ().into_robj()
// } else {
// let bb = <&Geom>::from_robj(&xi).unwrap().geom.minimum_rotated_rect();
// let bb = <&Geom>::try_from(&xi).unwrap().geom.minimum_rotated_rect();
// match bb {
// Some(b) => b.into_geom().into_robj(),
// None => NULL.into_robj(),
Expand Down
30 changes: 14 additions & 16 deletions src/rust/src/casting/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,40 @@ use sfconversions::{vctrs::as_rsgeo_vctr, Geom};

#[extendr]
fn explode_linestrings_(x: List) -> Robj {

let res_vec = x.into_iter()
let res_vec = x
.into_iter()
.flat_map(|(_, xi)| {
if xi.is_null() {
vec![().into_robj()]
} else {
let xi = Geom::from(xi);
let li = LineString::try_from(xi.geom).unwrap();

li
.lines()
.map(|li| {
Geom::from(LineString::from(li)).into_robj()
}).collect::<Vec<Robj>>()
}})
li.lines()
.map(|li| Geom::from(LineString::from(li)).into_robj())
.collect::<Vec<Robj>>()
}
})
.collect::<Vec<Robj>>();

as_rsgeo_vctr(List::from_values(res_vec), "linestring")
}

#[extendr]
fn explode_multilinestrings_(x: List) -> Robj {

let res_vec = x.into_iter()
let res_vec = x
.into_iter()
.flat_map(|(_, xi)| {
if xi.is_null() {
vec![().into_robj()]
} else {
let xi = Geom::from(xi);
let li = MultiLineString::try_from(xi.geom).unwrap();
li
.lines_iter()
.map(|li| {
Geom::from(LineString::from(li)).into_robj()
}).collect::<Vec<Robj>>()
}})
li.lines_iter()
.map(|li| Geom::from(LineString::from(li)).into_robj())
.collect::<Vec<Robj>>()
}
})
.collect::<Vec<Robj>>();

as_rsgeo_vctr(List::from_values(res_vec), "linestring")
Expand Down
21 changes: 11 additions & 10 deletions src/rust/src/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn geom_point_(x: Doubles, y: Doubles) -> Robj {
}
}

res.set_attrib("class", geom_class("point")).unwrap()
res.set_attrib("class", geom_class("point")).unwrap();
res.into()
}

#[extendr]
Expand Down Expand Up @@ -77,9 +78,9 @@ fn geom_multipoint_(x: Doubles, y: Doubles, id: Integers) -> Robj {
.collect::<Vec<Geom>>();

// create multipoint vector
List::from_values(res_vec)
.set_class(geom_class("multipoint"))
.unwrap()
let mut robj = List::from_values(res_vec);
robj.set_class(geom_class("multipoint")).unwrap();
robj.into()
}

#[extendr]
Expand Down Expand Up @@ -119,9 +120,9 @@ fn geom_linestring_(x: Doubles, y: Doubles, id: Integers) -> Robj {
.collect::<Vec<Geom>>();

// create multipoint vector
List::from_values(res_vec)
.set_class(geom_class("linestring"))
.unwrap()
let mut robj = List::from_values(res_vec);
robj.set_class(geom_class("linestring")).unwrap();
robj.into()
}

#[extendr]
Expand Down Expand Up @@ -192,9 +193,9 @@ fn geom_polygon_(x: Doubles, y: Doubles, id: Integers, ring: Integers) -> Robj {
.collect::<Vec<Geom>>();

// create multipolygon vector
List::from_values(res_vec)
.set_class(geom_class("polygon"))
.unwrap()
let mut robj = List::from_values(res_vec);
robj.set_class(geom_class("polygon")).unwrap();
robj.into()
}

#[extendr]
Expand Down
8 changes: 4 additions & 4 deletions src/rust/src/coord_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn n_coords(x: List) -> Integers {
if xi.is_null() {
Rint::na()
} else {
let n = <&Geom>::from_robj(&xi).unwrap().geom.coords_count() as i32;
let n = <&Geom>::try_from(&xi).unwrap().geom.coords_count() as i32;
Rint::from(n)
}
})
Expand All @@ -54,7 +54,7 @@ fn coord_last(x: List) -> Robj {
if xi.is_null() {
NULL.into_robj()
} else {
let pnt: Point = <&Geom>::from_robj(&xi)
let pnt: Point = <&Geom>::try_from(&xi)
.unwrap()
.geom
.coords_iter()
Expand Down Expand Up @@ -87,7 +87,7 @@ fn coord_first(x: List) -> Robj {
if xi.is_null() {
NULL.into_robj()
} else {
let pnt: Point = <&Geom>::from_robj(&xi)
let pnt: Point = <&Geom>::try_from(&xi)
.unwrap()
.geom
.coords_iter()
Expand Down Expand Up @@ -130,7 +130,7 @@ fn coord_n_(x: List, n: Integers) -> Robj {
if xi.is_null() || ni.is_na() {
NULL.into_robj()
} else {
let crd = <&Geom>::from_robj(&xi)
let crd = <&Geom>::try_from(&xi)
.unwrap()
.geom
.coords_iter()
Expand Down
4 changes: 2 additions & 2 deletions src/rust/src/coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn point_to_coords(x: List) -> Robj {
let (x, y): (Vec<f64>, Vec<f64>) = x
.into_iter()
.filter_map(|(_, robj)| {
let p = <&Geom>::from_robj(&robj).unwrap();
let p = <&Geom>::try_from(&robj).unwrap();
let crds = match p.geom {
Geometry::Point(p) => Some(p.x_y()),
_ => None,
Expand All @@ -26,7 +26,7 @@ fn multipoint_to_coords(x: List) -> Robj {
.into_iter()
.enumerate()
.filter_map(|(i, (_, robj))| {
let g = <&Geom>::from_robj(&robj).unwrap();
let g = <&Geom>::try_from(&robj).unwrap();
let crds = match &g.geom {
Geometry::MultiPoint(mp) => {
let n = mp.coords_count();
Expand Down
4 changes: 2 additions & 2 deletions src/rust/src/densify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn densify_euclidean(x: List, max_distance: Doubles) -> Robj {
.into_iter()
.zip(max_distance.iter())
.map(|((_, xi), md)| {
let xi = <&Geom>::from_robj(&xi).unwrap();
let xi = <&Geom>::try_from(&xi).unwrap();
match &xi.geom {
Geometry::LineString(l) => l.densify(md.inner()).into_geom(),
Geometry::MultiLineString(l) => l.densify(md.inner()).into_geom(),
Expand Down Expand Up @@ -102,7 +102,7 @@ fn densify_haversine(x: List, max_distance: Doubles) -> Robj {
.into_iter()
.zip(max_distance.iter())
.map(|((_, xi), md)| {
let xi = <&Geom>::from_robj(&xi).unwrap();
let xi = <&Geom>::try_from(&xi).unwrap();
match &xi.geom {
Geometry::LineString(l) => l.densify_haversine(md.inner()).into_geom(),
Geometry::MultiLineString(l) => l.densify_haversine(md.inner()).into_geom(),
Expand Down
Loading
Loading