simplify distance calculation (Spherical distance to degree difference)

142 Views Asked by At

I am programming for a transportaion system which every 5 second I have to check if there are drivers nearby the start point. I know that it is not optimum solution If I loop through all thousnads drivers and calculate the distance using (LAT,LONG) so I want to simplify the calculations just by linear difference between the (LAT,LONG) of start point and the drivers to be in specific range. My problem is how to calculate this RANGE to satisfy the minimum desired distance in a specific area.

Assumptions:

list of variables: (lat1,long1) , (lat2,long2) , (lat0,long0), MINIMUM , RANGE, Distance

  1. The targt area of activity is beteewn (lat1,long1) and (lat2,long2)
  2. The start Point is (lat0,long0) which is in bounds of area.
  3. Drivers position is any point (latD,longD) which is in the bounds of area.
  4. The real DISTANCE is calculated using a complex spherical formula between start point and drivers point (Please see the footnotes).
  5. I want to simplify the distance check just by comparing abs(lat0 - latD)< Range also abs(long0-longD)<Range . The Range should be the minimum number which if satisfies these two formula, so the real DISTANCE also should be Less than the MINIMUM.

in brief find the relation between RANGE and MINIMM which:

if (abs(lat0 - latD)< Range) && (abs(long0 - longD)<Range)

So : Distance < Minimum

How should I calculate the RANGE within the defined area to satisfy all points in this area? I am aware that using a single RANGE causes the low accuracy in different points of the area but I am looking for minimum one which satisfies all points (or perhaps an optimum one).

Footnote: obviousley the RANGE is independent of drivers position and should be calculated using these 7 parameters (LAT0,LONG0),(LAT1,LONG1),(LAT2,LONG2) and MINIMUM.

Footnote2: The real Forumla for spherical Distance between (Lat0,Lon0) and (latD,lonD) by assuming the earth radius (Ref.):

function distance(lat0, lon0, latD, lonD)
    p = 0.017453292519943295
    a = 0.5 - cos((latD - lat0) * p)/2 + cos(lat0 * p) * cos(latD * p) * (1 - cos((lonD - lon0) * p))/2
    distance = 12742 * arcsin(sqr(a))
end function

I guess I need a mapping from spherical to Cartesian system to convert that curved area to a flat square? Do you have any other idea? Do you deep dive into that complex formula to extract latD - lat0 from the shark's mouth? :))

3

There are 3 best solutions below

0
On

Refer to https://en.wikipedia.org/wiki/Geographic_coordinate_system

And specifically take a look at "Length of a degree" section in there, which is based on these two main articles if you wanna dive in:

https://en.wikipedia.org/wiki/Length_of_a_degree_of_latitude

and

https://en.wikipedia.org/wiki/Length_of_a_degree_of_longitude

0
On

Is your system is running either (a) near the poles, or (b) over areas much larger than a couple of degrees of latitude?

If not, one can obtain a good first linear approximation as follows. Pick a representative latitude, $\lambda$. For instance, for Los Angeles, I would pick $\lambda = 34$ degrees.

Then, convert all lat-lon pairs as follows: Multiply the latitude by $111$ km, and multiply the longitude by $111 \cos \lambda$ km. Then you can use your usual Cartesian distance formulas after that.

0
On

Ragarding to both answers this and this I tried to find a simple formula which fits to those complex distance factor for Latitude and Longitude in the references. These are not global formula for a big area but are simplified to small areas with linear assumptions.

The following one shows how many Kilometers is surrounded in 1 degree of Longitude around a given latitude:

$$LongDistanceFactor= -0.0114(lat)^2 - 0.2396(lat) + 112.57$$

and the second one shows how many kilometers is surrounded in 1 degree of latitude around a given latitude:

$$LatDistanceFactor = 0.0139(lat) + 110.51$$

Footnotes:

  1. use the absolute amount of latitude in both formula because the earth is symmetrical
  2. As the formula is generated for 1 unit of degree (lat of long), so to find how many degrees equals to a given DISTANCE you just need the ratio of DISTANCE/LatDistanceFactor or DISTANCE/LongDistanceFactor and simply add the result (by factor 1) to to the given Lat or Long.