Thanks for the response man,
That’s what I have.
I’m assuming you mean that you have lat.ong coordinates.
How can I add many coordinates between the first and the last
coordinate using the script below, by specifying the distance between
each coordinate? So for example by saying the second coordinate should
be far from the first one by a distance of 2 meters and so on till we
reach the last coordinate. Itâs a straight line route, and if I have to
type in the coordinates manually it will be a lot of work.
Things are harder in lat/long because the Earth’s surface is curved. You
can be fairly accurate by pretending that the Earth is a sphere (it
isn’t) and that all your coordinates are at a fixed distance from the
centre, eg sea level. You will need to do some trigonometry.
I would be inclined to start by converting the lat/long coordinates into
3-space (x,y,z) coordinates in metres with (0,0,0) being the centre of
the Earth and z measured along the axis. More on that below.
Having to 3-space metres coordinates lets you measure their distance
apart, again using Pythagoras (works just as well in 3-space).
Then just use the 1-metre/distance fraction you computed before to
advance in increments of 2 metres along the line. Then for each point
along the line, convert back into lat/long to get KML coordinates.
Of course, a straight line between two points on a sphere passes under
the surface of the Earth - you would compute the lat/long by projecting
a line from the centre of the Earth through your (x,y,z)m point to the
surface. For small distances the difference will be ignoreable. For
larger distances the path over the surface will be in steps of
significantly more than 2 metres because 2 metres along your straight
line will correspond to points along the curve, with is obviously
longer. You can compute the length of the curve versus that of the line
and use that to scale down 2m along the line to get 2m along the curve
(which is the number you want).
Regarding converting lat/long to (x,y,z)m:
-
latitude has a fixed ratio - the arc from the pole to the equator is
90° so knowing the distance of that will give you the conversion ratio
-
longitude varies with the latitude - the ratio is the same at the
equator and 0 at the pole. Since changing only the longitude traces a
circle around the sphere centred on the axis, the length of the circle
is the circumference, giving you the ratio at that latitude; the
radius of the circle is of course the radius of the earth multiplied
by the cosine of the latitude
The (arbitrary) x value is (say) the sine of the longitude multiplied by
the radius of the circle at that latitude; the y value uses the cosine.
The z value (along the axis) is the sine of the latitude multiplied by
the radius of the earth.
To do all that in metres just use the radius in metres, since sine and
cosine are just ratios.
Presupplied sine and cosine functions are available in the math
module: math — Mathematical functions — Python 3.12.1 documentation
They work in radians, so you will want to convert lat/long into radians
when using them: Radian - Wikipedia
I would start by writing functions to convert lat_to_z and long_to_xy.
Get them right. Then the reverse: xyz_to_latlong. Then the rest should
follow pretty directly.
Finally:
-
draw some diagrams by hand on paper to visualise the triangles
involved; label the angles as lat and long, earth radius etc so that
you can see where these values correspond to triangles
-
do soe hand calculations of the ratio between latitude and metres so
that you can look at the results of your functions and have an idea
wether they are correct - a close value does necessarily not mean
you’re correct, but a far off value means you’re definitely wrong!
-
write a few functions to test correctness; for example you could write
a function that takes a lat/long pair, computes the (x,y,z)
coordinates, then converts those back into lat/long using your
reverse conversion functions, and checks that you get the same
lat/long that you started with
You could write a few tests like that. Note that floating point numbers
are actaully fixed precision fractions internally, so going from
lat/long to (x,y,z)m and back will almost certainly not get exactly
the same lat/long - you want to check that they are very close, usually
by subtracting them and checking that the difference is very small.
Cheers,
Cameron Simpson cs@cskk.id.au