Fastest way to reduce number of latitude and longitude points

后端 未结 3 1695
攒了一身酷
攒了一身酷 2021-02-20 04:24

I\'m trying to reduce and combine a number of points to the center point of those locations. Right now I\'m brute-forcing it by finding the closest pair, combining those and re

3条回答
  •  猫巷女王i
    2021-02-20 04:51

    As for an efficient way, have you considered laying down a grid over the map and then assigning each point to its corresponding cell in the grid? This should have good performance.

    A better (yet slower) approach would be to have dynamic cells instead of fixed cells like the suggestion above. You start with no cells at all. Then drop the first point in the map and define a cell with some predetermined dimensions around it. Then drop the next point on the map. If it falls inside the previous cell you add it to it, and possibly recenter the cell around the two points. If the point falls outside the cell then you create a second cell for it. Now you add the third point to the map and check it against the two cells. This process continues until you have added all the points to the map. I hope you get the idea. I think you could approximately limit the number of reduced points by changing the size of the cells.

    EDIT (based on comment from rrenaud): You can start using a big cell size and apply one of the algorithms above. If the number of cells you end up with is too low, then you can repeat the algorithm on each of the cells and subdivide them even more. While this won't allow you to exactly reduce to a fixed number of points, you can get pretty close.

提交回复
热议问题