1

I have an interesting algorithmic challenge in a project I am working on. I have a sorted list of coordinate points pointing at buildings on either side of a street that, sufficiently zoomed in, looks like this:

enter image description here

I would like to take this zigzag and smooth it out to linearize the underlying street.

I can think of a couple of solutions:

  1. Calculate centroids using rolling averages of six or so points, and use those.
  2. Spline regression.

Is there a better or best way to approach this problem? (I am using Python 3.5)

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
Aleksey Bilogur
  • 3,686
  • 3
  • 30
  • 57
  • 1
    Are you just trying to smooth them out to "look better" or is there a plan to do analysis on the results. Often the best choice of smoothing for analysis is dependent on the model you are analyzing. – Cort Ammon May 29 '16 at 18:34
  • Exactly what @CortAmmon said. What is your purpose? As you already noted, there's multiple options. I'm a DSP guy. I'd just interpolatingly low-pass these; but that's pretty likely to be useless for you, unless your movement model has something to do with sinusoids. – Marcus Müller May 29 '16 at 18:35
  • I'm using a dataset of all addresses in New York City and trying to process it using this algorithm in order to arrive at a reasonable estimate of the length of every street in New York City for a data analysis project. I'm not sure which model would be accurately enough performant for that. As a fringe benefit I'd like to also perhaps map out all of the generated polylines into a street grid visualization, but I wager that since that's just useful for visualization that probably requires a lower level of accuracy. – Aleksey Bilogur May 29 '16 at 18:46
  • Have a look at the [Douglas-Peucker algorithm](https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm). It strives to reduce the number of waypoints along a route without changing the route too much. [Here](https://github.com/fhirschmann/rdp) is an implementation in Python. – Axel Kemper May 29 '16 at 19:04
  • Maybe a Kalman filter https://en.wikipedia.org/wiki/Kalman_filter can help you. It is mainly use in track reconstruction from perturbated measures. – Jean-Baptiste Yunès May 29 '16 at 19:28

2 Answers2

8

Based on your description and your comments, you are looking for a line simplification algorithms.

enter image description here

Ramer-Doublas algorithm (suggested in the comment) is most probably the most well-known algorithm in this family, but there are many more.

For example Visvalingam’s algorithm works by removing the point with the smallest change, which is calculated by the smallest square of the triangle. This makes it super easy to code and intuitively understandable. If it is hard to read research paper, you can read this easy article.

Other algorithms in this family are:

Read about them, understand what are they trying to minify and select the most suitable for you.

JWCS
  • 1,120
  • 1
  • 7
  • 17
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
0

Dali's post correctly surmises that a line simplification algorithm is useful for this task. Before posting this question I actually examined a few such algorithms but wasn't quite comfortable with them because even though they resulted in the simplified geometry that I liked, they didn't directly address the issue I had of points being on either side of the feature and never in the middle.

Thus I used a two-step process:

  1. I computed the centroids of the polyline by using a rolling average of the coordinates of the five surrounding points. This didn't help much with smoothing the function but it did mostly succeed in remapping them to the middle of the street.
  2. I applied Visvalingam’s algorithm to the new polyline, with n=20 points specified (using this wonderful implementation).

The result wasn't quite perfect but it was good enough:

enter image description here

Thanks for the help everyone!

Aleksey Bilogur
  • 3,686
  • 3
  • 30
  • 57