Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The distance calculated from point projections and linear distance is different and difference is big #551

Closed
b2bking opened this issue Jun 18, 2020 · 13 comments

Comments

@b2bking
Copy link

b2bking commented Jun 18, 2020

Hello
I am projecting two points onto the shape and trying to calculate the distance between two projections. I also calculated the distance between two coords . Two distances differ and the difference is big. Can you please help me to find a mistake or prove that it is expected result in calculations.
the shape : LINESTRING (174.76716 -36.84489, 174.76715 -36.84489, 174.76719 -36.84481, 174.76598196955078 -36.84449656507264)

point one: "lon": 174.76716, "lat": -36.84489,
point two: "lon": 174.76646666666667, "lat": -36.844705,

point one projection: 0.0
point two projection: 8.286513887779961E-4

the difference between two projections is converted to meters and is equal to 92.2450506439055

Please confirm that my calculations are correct or wrong
With kind regards

@b2bking
Copy link
Author

b2bking commented Jun 18, 2020

The linear distance is about 65 meters and the shape is almost straight.

@jnh5y
Copy link
Contributor

jnh5y commented Jun 18, 2020

JTS is a library for doing geometry/topology, not geography. How are you projecting the above geometries?

Distance in JTS is in the units of whatever the values of the coordinates are in. If you ask for distance using longitude / latitude, you get back distance in degrees (which isn't entirely sensible, but can be slightly useful).

@b2bking
Copy link
Author

b2bking commented Jun 18, 2020

Coordinate vehicleCoordinate = new Coordinate(vehicleLon, vehicleLat);
LengthIndexedLine shape;
LengthIndexedLine linePart = new LengthIndexedLine(shape);
double angularDistance = linePart.project(vehicleCoordinate)
return convertProjectionIndexToMetres(angularDistance)
private static double convertProjectionIndexToMetres(double index) {
return index * (PI / 180) * RADIUS_OF_EARTH;
}

This how I make my calculations.

@jnh5y
Copy link
Contributor

jnh5y commented Jun 18, 2020

Ok, the first point is clearly on the line (since it is one of the vertices). Do you expect that the second point is on the line?

@b2bking
Copy link
Author

b2bking commented Jun 18, 2020

What I do is I project the second point onto the shape, measure the difference between two projections by subtracting, and then convert the result to meters

@b2bking
Copy link
Author

b2bking commented Jun 18, 2020

the projection distance gives me 92 meters and the straight line distance is just 65 meters, I wonder why the difference is that big.

@dr-jts
Copy link
Contributor

dr-jts commented Jun 19, 2020

Looks correct to me in the TestBuilder:

image

Perhaps your projection approach has an issue?

@dr-jts
Copy link
Contributor

dr-jts commented Jun 19, 2020

Your convertProjectionIndexToMetres routine does not take latitude into account. So it won't match a true geodetic distance.

@b2bking
Copy link
Author

b2bking commented Jun 19, 2020

Is there a way to correctly calculate the distance in meters between start point to desired point along the route?

@b2bking
Copy link
Author

b2bking commented Jun 19, 2020

what is the EPSG used in the library?

@dr-jts
Copy link
Contributor

dr-jts commented Jun 19, 2020

what is the EPSG used in the library?

It is whatever is used by the coordinates you provide. JTS itself simply assumes you are using a planar coordinate system.

@dr-jts
Copy link
Contributor

dr-jts commented Jun 19, 2020

Is there a way to correctly calculate the distance in meters between start point to desired point along the route?

Assuming a very short distance, you can convert distance in degrees at a given latitude to metres using:

m = degrees * 111319.488 *cos(Latitude)

@dr-jts
Copy link
Contributor

dr-jts commented Jun 20, 2020

Please close if this has helped.

@b2bking b2bking closed this as completed Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants