# simplify distance calculation (Spherical distance to degree difference)

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? :))

• This seems to be an XY problem: the real problem is to look for drivers close to a given position, and you have decided upon a specific solution, which is looping through all the drivers and calculate their distances to the given point to find one close enough. Then you want help with calculating a distance in a clever and efficient way. But wouldn't it be better to look for a solution where you only look at drivers close by? Sep 21, 2019 at 15:00
• But if you're going for this solution, it might be better to calculate the straight line distance between two points instead of the distance along the sphere. Sep 21, 2019 at 15:08
• @md2perpe you are right there are more shortcut solutions for this problem. For your first comment that I itterate through near cars, I need a start point to define which cars are close? I have started right here and I know that it is a feasible solution. After that I thinked to this question that what is the minimum RANGE instead of a big number as start point to exactly find the cars in the range not a wider range of cars. Sep 21, 2019 at 15:36
• The target area, is it the spherical rectangle with corners in $(\mathsf{lat}_1, \mathsf{long}_1)$, $(\mathsf{lat}_1, \mathsf{long}_2)$, $(\mathsf{lat}_2, \mathsf{long}_2)$, $(\mathsf{lat}_2, \mathsf{long}_1)$? Sep 21, 2019 at 17:06
• How big is the target area? Perhaps it's small enough to approximate as flat? Sep 21, 2019 at 17:07

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

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.

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.