vojtam
vojtam

Reputation: 1245

Unable to shift coordinates in python

I have problems with shifting coordinates by 100 meters horizontaly/verticaly in Python. I found that If your displacements aren't too great (less than a few kilometers) and you're not right at the poles, use the quick and dirty estimate that 111,111 meters (111.111 km) in the y direction is 1 degree (of latitude) and 111,111 * cos(latitude) meters in the x direction is 1 degree (of longitude). here: https://gis.stackexchange.com/questions/2951/algorithm-for-offsetting-a-latitude-longitude-by-some-amount-of-meters

I wrote two functions in Python. This function works, that is it computes the shift in latitude:

def vertikalne(shift):
    return shift/111_111

Check that distance between (49.550586, 18.859254) and (49.550586 + vertikalne(100), 18.859254) is really 100 meters.

But this function does not work and I don`t know what is the problem:

import math

def horizontalne(latitude, shift):
    return (shift/111_111) * math.cos(latitude*math.pi/180) # UPDATE: I just converted to radians

Distance between (49.550586, 18.859254) and (49.550586 , 18.859254 + horizontalne(49.550586 , 100)) is 49 which is nonsense.

Can you help me please?

Upvotes: 0

Views: 258

Answers (3)

Flag
Flag

Reputation: 45

math.cos() takes radians as input not degrees. You need to convert.

def horizontalne(latitude, shift):
    return (shift/111_111) * math.cos(2*math.pi()*latitude/360)

something like this. And if you want to calculate distances you need to apply Pythagoras' theorem. Maybe even take the average of the latitudes for the cos.

-- Mine is wrong. @vojtam is correct. (parentheses...) It is rather unclear what the function is supposed to do. Maybe better to give your params more meaningful names. e.g. shift_distance, shift_long, shift_lat etc. I think it is supposed to do the following: The functions both take a distance as input and as output it gives the fraction of 1 degree of the arc of the intended circle around the earth. (for vertikalne it is the meridian, for horizontalne it is the parallel at the given latitude. (https://www.britannica.com/science/latitude ) 111_111 meters is indeed more or less the distance over the earth of 1 degree. (at least along a meridian, the equator is slightly larger....) So your evaluation should be different:

A = (49.550586, 18.859254)   # lat,long
B = (49.550586, 18.859254 + horizontalne(49.550586 , 100)) # B is 100m east of A along the parallel at lat 49.550586
(B[0] - A[0], (B[1] - A[1])  ) 

gives:

Out[42]: (0.0, 0.0005838993787818936)

Which means: moving 100 meters horizontally along the earth's parallel at latitude 49.55... means changing the longitude angle for 0.0005838993787818936 of a degree. Makes sense to me.

Upvotes: 0

vojtam
vojtam

Reputation: 1245

Correct solutions is:

import math

def horizontalne(latitude, shift):
    return shift/(111_111 * math.cos(math.radians(latitude)))

Upvotes: 2

mrtechtroid
mrtechtroid

Reputation: 708

Change math.cos(latitude) to math.cos(math.pi/180*latitude) as the math.cos() expects angles in radians and not in degrees.

Upvotes: 0

Related Questions