Python: speeding up geographic comparison

前端 未结 6 1216
梦毁少年i
梦毁少年i 2020-12-05 20:35

I\'ve written some code that includes a nested loop where the inner loop is executed about 1.5 million times. I have a function in this loop that I\'m trying to optimize. I\

6条回答
  •  一向
    一向 (楼主)
    2020-12-05 20:47

    The formula you wrote (d=abs(lat2-lat1)+(lon2-lon1)) does NOT preserve triangle inequality: if you find lat, lon for wich d is min, you don't find the closest point, but the point closest to two diagonal straight lines crossing in the point you are checking!

    I think you should order the large ammount of points by lat and lon (this means: (1,1),(1,2), (1,3)...(2,1),(2,2) etc. Then use the gunner method to find the some of the closest points in terms of latitude and longitude (this should be really fast, it is going to take cpu time proportional to ln2 (n) where n is the number of points). You can do this easily, on example: choose all the points in a square of 10x10 around the point you are going to check, this means: find all the points that are from -10 to +10 in lat (gunner method) and again those that are from -10 to +10 in lon (gunner method). Now you have a really small ammount of data do process, and it should be very fast!

提交回复
热议问题