-
Notifications
You must be signed in to change notification settings - Fork 202
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
LineString slicing #986
Comments
This seems useful, and indeed the LineInterpolatePoint/LineLocatePoint seems like good inspiration, since they're in the same spirit. As you say, they both take input parameters as a "ratio" of the total line length, but I can easily imagine how "length" is an equally valid input format (In fact their implementations are primarily in terms of length). I can imagine having both.
Keep in mind that despite your insistence (😉), the computed intersection point paradoxically might not intersect the line due to floating point representation errors. Assuming we also get an API like you mentioned above ("a [start_percent, end_percent] fraction of the total length"), an API that I think is reasonable and should sidestep that kind of error: /// This is probably a bad name, since Slice already means something important in rust.
/// All of these names are just spitballing, don't take them as a request.
impl Slice for LineString {
/// subslice of the linestring until it is length T
fn slice_to_length(&self, length: T) { todo!("assume this is done") }
/// subslice of the linestring until it is (ratio*100)% of it's original length
fn slice_to_ratio(&self, ratio: T) { todo!("and/or assume this is done too") }
/// If `coord` is on the linestring, subslice the linestring from begining until it reaches that `coord`.
/// If `coord` is not on the linestring, the closest point on the linestring will be used.
/// (secretly these are one-and-the same case, but maybe it's easier to explain it this way)
fn slice_to_point(&self, coord: Coord) {
let ratio = LineLocatePoint.line_locate_point(self, coord);
self.slice_to_ratio(ratio)
}
} WDYT? |
See also: #378 |
Hi @dabreegster, I am also very keen on this feature. I have been chipping away at an implementation in #1050. May I please have your feedback when / if you have time 😊 I opted for a very similar API to what you suggested above. I'd be happy to make changes to names etc. But I need a sanity check on the API / result types I have chosen. |
Deeply sorry for multi month delay here...
This API looks good to me, and I like how (Or maybe I misunderstood the output -- one or two LineStrings? If it's two, maybe something like Alternatively, the input could be something like
@thehappycheese, I like the API you've laid out -- you've thought about things more generally than I was. Instead of modifying the input in-place to have start/end points, you're splitting at specified points and returning multiple outputs. For |
hi @dabreegster :)
I can add that in to my current PR easily enough :) Thinking about it though... there could be a special implementation of slice_to_point which further avoids precision issues in the 'ratio' parameter. It could project the new coord onto the nearest bit of the line and then generate two new lines either side of the projected point....
I like the idea of being able simultaneously reverse the line portion using a slice. But the simplest implementation just scans the line string once from start to end. I am imagining you would need to check for reversed input and remember the order, do the slice, then as the final step reverse the Vec just before returning. If the linestring just had a separate
Ooo I had not thought of having an enum as the input... that is nice and explicit, and for documentation it is nice because I'd only have to write docs for one function and each variant of
What do you think about
I think you are right about |
I was responding to Michael's earlier suggested API. I think that API is hard to use for this reason -- to split on different ends, a user might have to reverse a bunch. No need to do any of that if a split returns linestrings on either end.
Yeah, it feels like there could be some sort of optimization here. Ultimately what I'm doing is intersecting two line strings, and getting half from each of the two inputs. The intersection is already finding some fraction along each of the inputs; it could be wasteful to have to project the intersection point onto both linestrings again just to figure out that fraction.
|
If I have a non-closed
LineString
, I often want to slice / split it and get something smaller back. The input could be...There are some edge cases:
I'd like to add an implementation of this to georust. Any opinions on the API or impementation, or other edge cases I've not thought about?
CC @BudgieInWA. Today we make heavy use of this type of operation in osm2streets using something not based on georust.
The text was updated successfully, but these errors were encountered: