7

Say I have a latitude of 38.802610 and a longitude of -116.419389 and I want to be able to add a distance to that, 20 miles for example.

So far I have came up with a rough idea on how I would do this:

  • Firstly, work out how many miles are in 1° of latitude, say for example it was 30
  • Then, divide one by it:
    • 1 / 30 = 0.033333
  • Add it to my original latitude to get my maximum ° of latitude:
    • 38.802610 + 0.033333 = 38.8355943
  • Subtract it to my original latitude to get my minimum ° of latitude:
    • 38.802610 - 0.033333 = 38.769277

But this is flawed because there seems to be no direct conversion for longitude as from what I've read the calculation varies. I have looked around and found some resources but I can't find one that applies directly to my question. I came across Haversine Formula but I can't find a way of applying it to my situation as, from what I've read, it's used for calculating the distance between two points.

Ultimately, I need to be able to find out the:

  • maximum latitude (my current latitude + given distance e.g 20 miles)

  • minimum latitude (my current latitude - given distance e.g 20 miles)

  • maximum longitude (my current longitude + given distance e.g 20 miles)

  • minimum longitude (my current longitude - given distance e.g 20 miles)

4
  • 5
    You've discovered the first geodetic problem. Many GIS packages have tools to solve it (and will solve it on a spheroid). The US Geodetic Survey has a website devoted to this and the second problem, with source code.
    – Vince
    Oct 6, 2017 at 10:59
  • 1
    depending on what tool you want to use one of the questions in gis.stackexchange.com/search?q=azimuth+distance will be a duplicate
    – Ian Turton
    Oct 6, 2017 at 13:33
  • any interest in a python/ogr suggestion? Oct 7, 2017 at 2:00
  • Yes I would be interested in a Python suggestion.
    – John Jones
    Oct 7, 2017 at 10:45

1 Answer 1

4

if interested in a python suggestion - using OGR

from osgeo import ogr, osr

# setup function to reproject coordinates
def convertCoords(xy, src='', targ=''):

    srcproj = osr.SpatialReference()
    srcproj.ImportFromEPSG(src)
    targproj = osr.SpatialReference()
    if isinstance(targ, str):
        targproj.ImportFromProj4(targ)
    else:
        targproj.ImportFromEPSG(targ)
    transform = osr.CoordinateTransformation(srcproj, targproj)

    pt = ogr.Geometry(ogr.wkbPoint)
    pt.AddPoint(xy[0], xy[1])
    pt.Transform(transform)

    return([pt.GetX(), pt.GetY()])

you can then feed the lat/long coordinates to the function, with appropriate epsg codes (for this example, we'll use WGS84 as the input and albers for the output):

coords =  (-116.419389, 38.802610)
albersXY = convertCoords(coords, 4326, 5070)

add 20 miles to one direction of the albers coordinates (which are in meters)

albersXY[0] = albersXY[0] + 20 * 1609.344
# albersXY[1] = albersXY[1] + 20 * 1609.344

then convert from albers back to WGS

newlonlat = convertCoords(albersXY, 5070, 4326)
print(newlonlat)
>>> [-116.13229978439009, 39.14407586190597]
3
  • I think that is not correct to name the result 'newlatlon' because the function is returning x(longitude), y(latitude), which means that is returning Longitude and Latitude. Please correct me if I'm wrong. You can check that coordinates -116.419389, 38.802610 doesn't exists, if -116 stands for latitude. The -116 is the longitude.
    – makkasi
    Apr 19, 2019 at 8:53
  • Not sure if the previous answer works correctly, because adding kilometers is giving me wrong result on google maps. # zone.center=lon = lat 46.85418051919657, lon 2.3291015625, zone.km = 300 radius .... Result dots for the bounding box, all albers xy, are: | 50°35'13.1"N 3°46'39.7"E | 45°48'25.4"N 7°36'59.7"E | 43°09'07.5"N 1°02'07.5"E | 47°41'39.9"N 3°06'44.6"W
    – makkasi
    Apr 30, 2019 at 6:40
  • @makkasi - I edited the variable name to match the order of x,y. And you're correct on the distance! I should have added the distance to only one direction of the coordinates. Otherwise, you have to calculate the vector. Jul 7, 2019 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.