Search code examples
pythongpscoordinates

GPS coordinates: how to round to a define number of kilometers?


I have a dataframe with latitude and longitudes:

   latitude  longitude

0 -1.621622   8.918919
1 -1.216216   8.648649
2 -1.486486   8.783784
3 -2.027027   8.918919
4 -1.216216   8.648649

I want to round the coordinates to a define number of kilometers n (between 5 and 50), which means I will obtain a subset of coordinates, with n kilometers between each point.

Basically, I can round to 1 decimal, which approximately corresponds to 11.1 km (http://wiki.gis.com/wiki/index.php/Decimal_degrees):

   latitude  longitude
0      -1.6        8.9
1      -1.2        8.6
2      -1.5        8.8
3      -2.0        8.9
4      -1.2        8.6

But how can I achieve this if n is not 11.1 ? For instance if n = 10. I tried the following formula:

degree_to_km_factor = 1.11
ratio = degree_to_km_factor / n = 0.111
rounded_latitude = (latitude * ratio).round(2) / ratio

Which gives me:

        latitude  longitude
0      -1.621622   8.918919
1      -1.171171   8.738739
2      -2.072072   8.918919
3      -2.072072   8.918919
4      -1.621622   8.918919

This allows me to get latitude and longitude points rounded to 1/11.1 = 0.09, which correspond to 10 kilometers. I've tested this formula for several values of n and it seems to work, but I'm not sure it is correct and I didn't achieve to prove it. Do you know if it is correct, and if not, is there a better way to achieve this ?

NOTE: I assume I'm always at the equator, so the approximation 1 degree = 111 km is always true in my case, I don't need to be more precise than this.


Solution

  • If you want to round degree coordinates to a multiple of n kilometers you have to

    1. Compute the coordinates in kilometers.
    2. Round it to a multiple of n.
    3. Compute it back to degree coordinates.

    As you said your coordinates are close to the equator you can determine the operation 1) and 3) as a simple multiplication.

    kilometers = degree * 111 # Approximate number of kilometers for 1 degree close to the equator
    

    Rounding any number to a multiple of n can be expressed as follows

    y = round(x/n) * n
    

    Therefore the function to round degrees to a defined number of kilometers can be expressed such as:

    def round_to_n_km(degree_coordinate, n):
       kilometers = degree_coordinate * 111
       rounded_kilometers = round(kilometers / n) * n
       as_degree = rounded_kilometers / 111
       return as_degree
    

    If you need to solve this problem for coordinates far from the equator you can replace the multiplication by 111 with a more advanced function such as geopy.distance.distance() function.

    Now, you wondered why rounding the degree coordinate to 1 decimal allows you to round to about 11.1 kilometers. If you take the formula above and replace n with 11.1 you get this

    rounded_kilometers = round(kilometers * 11.1) * 11.1
                       = round(degree_coordinate * 111 / 11.1) * 11.1
                       = round(degree_coordinate * 10 ) * 11.1
    as_degree = rounded_kilometers / 111
              = round(degree_coordinate * 10) * 11.1 / 111
              = round(degree_coordinate / 0.1) * 0.1 
              # equivalent to rounding to .1 degrees (what you did with latitude*ratio.round(2) )
    

    And that is why rounding to .1 degree is roughly the same as rounding to 11.1 kilometers if you are close to the equator.