0

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?

8
  • 2
    math.cos expects radians, not degrees. convert your latitude to radians before applying the cosinus. Commented Apr 4, 2022 at 11:09
  • Did you try to debug the code, for example by checking the result from math.cos(latitude)? Are you able to do the calculation by hand? If so, that makes it much easier to notice what goes wrong. Commented Apr 4, 2022 at 11:15
  • @SembeiNorimaki Thanks for your comment. See my edit please, it still does not work Commented Apr 4, 2022 at 11:27
  • What are you expecting the answer to be(the return value)? @vojtam Commented Apr 4, 2022 at 11:31
  • @mrtechtroid 100. Because shift value is 100. I check it on an example: distance between (49.550586, 18.859254) and (49.550586, 18.859254 + horizontalne(49.550586 , 100)) Commented Apr 4, 2022 at 11:33

3 Answers 3

2

Correct solutions is:

import math

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

See my edit please, it still does not work
0

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.

4 Comments

Its The Exact Same Thing As What I Suggested with /180 replaced by *2/360 but OP Tells it doesnt work
using Pithagoras to compute geodesic distances will not work unless the distances are very small
@SembeiNorimaki: agreed about Pythagoras/Euclidian distance. For larger distances use the great circle [link]en.wikipedia.org/wiki/Great-circle_distance but that only works on perfect spheres and the Earth is not. For me it is not clear why you would only want to calculate horizontal distance or vertical distance.
Mine was wrong, parentheses issue. I added something to my solution.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.