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

New transformer: s2_convex_hull #151

Merged
merged 6 commits into from
Dec 31, 2021
Merged

Conversation

spiry34
Copy link
Contributor

@spiry34 spiry34 commented Dec 10, 2021

OK, I tried... It works (except on geometry collections - I gave up) but I'm afraid my coding may not be of the quality of other functions.
Please, dear maintainer, review carefully ;-)

@codecov-commenter
Copy link

codecov-commenter commented Dec 15, 2021

Codecov Report

Merging #151 (72875b2) into main (186c9cc) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #151      +/-   ##
==========================================
+ Coverage   94.45%   94.50%   +0.04%     
==========================================
  Files          40       40              
  Lines        2814     2838      +24     
==========================================
+ Hits         2658     2682      +24     
  Misses        156      156              
Impacted Files Coverage Δ
R/s2-transformers.R 100.00% <100.00%> (ø)
src/s2-transformers.cpp 96.56% <100.00%> (+0.26%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 186c9cc...72875b2. Read the comment docs.

Copy link
Collaborator

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome...thank you! I'm going to request a few changes, mostly to make it more similar to the other functions in s2-transformers.cpp. Also a few suggestions that might help you simplify things!

I don't see any tests here...could you add one to test-s2-transformers.R?

# @rdname s2_boundary
#' @export
s2_convex_hull <- function(x, options = s2_options() ) {
new_s2_xptr( cpp_s2_convex_hull(as_s2_geography(x), options), "s2_geography")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new_s2_xptr( cpp_s2_convex_hull(as_s2_geography(x), options), "s2_geography")
new_s2_xptr(cpp_s2_convex_hull(as_s2_geography(x), options), "s2_geography")

@@ -165,6 +166,7 @@ std::unique_ptr<Geography> rebuildGeography(S2ShapeIndex* index,
);
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this newline (to minimize the diff)?

Comment on lines 681 to 691
GeographyOperationOptions options(s2options);
GeographyOperationOptions::LayerOptions layerOptions = options.layerOptions();
S2Builder::Options buillderOptions = options.builderOptions();

// create the builder
S2Builder builder(buillderOptions);

// create the data structures that will contain the output
std::vector<S2Point> points;
std::vector<std::unique_ptr<S2Polyline>> polylines;
std::unique_ptr<S2Polygon> polygon = absl::make_unique<S2Polygon>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below - you won't need the Builder here so you can remove these lines.

item = geog[i];
if (item != R_NilValue) {
Rcpp::XPtr<Geography> feature(item);
feature->BuildShapeIndex(&index);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the index here so I think you can remove this line (and the one above where you declare it).

if (item != R_NilValue) {
Rcpp::XPtr<Geography> feature(item);
feature->BuildShapeIndex(&index);
//std::cout << typeid(feature).name() << "\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this debug comment?

}
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the builder, I think you can just use return XPtr<Geography>(new PolygonGeography(std::move(convexHullQuery.GetConvexHull()))).



// [[Rcpp::export]]
List cpp_s2_convex_hull(List geog, List s2options) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way you've implemented this is as an aggregate function...could you change the name to reflect this? (Maybe cpp_s2_convex_hull_agg()?)

}
} else if (feature->GeographyType() == Geography::Type::GEOGRAPHY_COLLECTION) {
if (!feature->IsEmpty()) {
// TODO: recursion?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't implemented a way to loop over the elements of a collection yet...Could you use Rcpp::stop() here instead to error when this happens?

@spiry34
Copy link
Contributor Author

spiry34 commented Dec 17, 2021

Hi @paleolimbot ,
Many thanks for your help and comments! I did my best to solve the problems and the ugly stuff.
Tests added (and passed).
All the best,
Sylvain

@paleolimbot
Copy link
Collaborator

This is really fantastic...I really appreciate the effort you put in to make the edits! Thank you again! I pushed a small fix to one of the tests to make it more likely to pass on more systems.

@paleolimbot paleolimbot merged commit 0a05838 into r-spatial:main Dec 31, 2021
@spiry34
Copy link
Contributor Author

spiry34 commented Jan 3, 2022

Thanks to you! Your suggestions were really helpful.
I'm wondering if it's save to always return a polygon, as the convex hull of a point is a point and the convex hull of a pair of points is a single line.
Anyway, it's easy to reimplement the builder if needed.
BTW happy new year and best wishes!
S.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants