Finding coordinates of a line

Hello guys,

If I have the first and the last coordinate of a line, how can I find the many other coordinates of this line, or choose the coordinates that are located at a distance of 1m from the first point till we reach the end?

Using a python function.

Thank you in advance, everyone.

Have you done the math? And what are the coordinates in? Metres?
Lat/long?

If you’re working in metres on a nice flat Euclidean plane then
everything’s easy. Suppose the start is at (x1,y1) in metres and the end
is at (x2,y2) in metres. The horizontal is x2-x1. The vertical is y2-y1.
The distance comes from Pythaoras’ Theorem.

Determine the fraction that 1m represents by dividing it by the
distance. Add the hroizontal and vertical scaled by that fraction.

If you’ve got latitude/longitude it gets harder; you’ll need some
trigonometry.

What’ve you got?

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks for the response man,
That’s what I have.
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.

  • Script
    import simplekml
    import pandas as pd

#Germany1 52.17365048266471, 10.50326921341822
#Germany2 52.17428145858134, 10.50385051351152

lines_kml = simplekml.Kml()
lines = lines_kml.newlinestring(name=‘Path’,
description=‘Germany1 to Germany2’,
coords=[(10.50326921341822,52.17365048266471),
(10.50385051351152,52.17428145858134)])

#change the line width and color
lines.style.linestyle.width = 5
lines.style.linestyle.color = simplekml.Color.red

Lines_kml_path = ‘/Users/alkon/PycharmProjects/Lines_kml.kml’
lines_kml.save(Lines_kml_path)

I would be very thankful for any assistance.
Thank you in advance

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

If I were working only with “small” areas, I would be tempted to just convert everything to UTM. Then pretend everything is flat and a nice cartesian grid. Then convert back to lat/long if necessary to report the results

Only if the error I got by doing that was too large would I bother dealing with spherical coords. You could use something like utm · PyPI to convert between lat/long and UTM.

Nice! I hadn’t come across UTM before: Universal Transverse Mercator coordinate system - Wikipedia

Home page for the PyPI link above (since it lacks a description in
PyPI): GitHub - Turbo87/utm: Bidirectional UTM-WGS84 converter for python

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you for your support.

In this way, I can generate many other coordinates on the same line? because as I said I only have the first and the last coordinate of the line and I need to generate the others automatically at the same distance from each other.

Thank you a lot

I really appreciate your support, thank you a lot man.
I will check it and come back to you if I have any further questions.

Hello again,
I have been sucessfully to find all the coordinates at the same distance, now how can I save all these new coordinates in a kml file which I can open in GoogleEarth?

Thank you in advance

[quote=“Almtein, post:9, topic:9769”]
Hello again,
I have been successfully able to find all the coordinates at the same distance, now how can I save all these new coordinates in a kml file which I can open in GoogleEarth?

Thank you in advance man