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

Feature Request: Add points to linestrings in equidistance #846

Closed
michelk opened this issue Jun 15, 2022 · 2 comments · Fixed by #847
Closed

Feature Request: Add points to linestrings in equidistance #846

michelk opened this issue Jun 15, 2022 · 2 comments · Fixed by #847

Comments

@michelk
Copy link

michelk commented Jun 15, 2022

The feature request seeks for a function to add points to a linestring with an approximate equidistance.

The screenshot below illustrates the functionality (max_dist = 2):
Number of points to add per segment (additional to the existing points) = ceiling(length(segment)/max_dist) -1

Maybe it would also be helpful to define alternatively min_dist

tt

@urschrei
Copy link
Member

Sketch of something that seems to work, based on a max_dist input:

use crate::LineInterpolatePoint;

let mut new_line = vec![];
linestring.lines().for_each(|line| {
    new_line.push(line.start_point());
    let num_segments = ((line.euclidean_length() / max_dist).ceil() as usize) - 1;
    if num_segments > 0 {
        // distance "unit" for this line based on how many new segments we need
        let frac = 1.0 / (num_segments + 1) as f64;
        (0..num_segments).enumerate().for_each(|(seg, _)| {
            // multiply distance unit by 1-indexed segment number to get correct offset
            let np = line
                .line_interpolate_point(frac * (seg + 1) as f64)
                .unwrap();
            new_line.push(np);
        })
    }
});
// we're done, push the last coordinate on to finish
new_line.push(linestring.points().last().unwrap());
let nls = LineString::from(new_line);

It'd be nice to be able to pre-allocate new_line's capacity, but I can't think of a good heuristic off the top of my head.

@michelk
Copy link
Author

michelk commented Jun 22, 2022

Cool. Thanks a lot.

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 a pull request may close this issue.

2 participants