1

I am working on an application with Spatial data. Here I need to find the distance from the centroid to a point. Can know how to convert miles to degrees in Java?

Rakesh Gourineni
  • 1,361
  • 5
  • 16
  • 30

4 Answers4

22

Final Edit: Look into Great Circle Distance and Geodesics to discover the complexities of what you were really asking in this question. This is admittedly well out of my familiarity with mathematics.

[Leaving original suggestions in for historical reasons. Though pieces may help you, they will not directly answer your question.]

See https://stackoverflow.com/a/1253545/1964221 for the answer to this question.

Latitude: 1 deg = 110.54 km

Longitude: 1 deg = 111.320*cos(latitude) km

To go the other direction, I wonder if we can do this(my algebra is rusty):

Latitude: 1 km = 1 deg / 110.54 km
Longitude: 1 km = 1 deg / (111.320*cos(latitude) km)

Application

Is your distance always strictly East-West or North-South? If so, you should be able to say:

Integer latTraveledMiles = 100;
Double latTraveledKM = 100 * 0.621371;
Double latTraveledDeg = (1 / 110.54) * latTraveledKM;

or

Double currentLat = 74.0064;
Integer longTraveledMiles = 100;
Double longTraveledKM = 100 * 0.621371;
Double longTraveledDeg = (1 / (111.320 * Math.cos(currentLat))) * longTraveledKM;

More than likely, you will need to know distance in both North-South and East-West directions(and takes this into the realm of "not straight-forward). You could look at the Pythagorean Theorem. You know the "long side" of the triangle(c^2) and can calculate for longitude(a^2) and latitude(b^2).

You should be able to apply portions of my previous code snippets to figure out
c = sqrt(a^2 + b^2).

Good luck.

Community
  • 1
  • 1
Dwight DeGroff
  • 600
  • 3
  • 9
5

If this is really a straight forward question we should ignore latitude and longitude, and just look at the angle between the radii to two points such that the distance between them, along the great circle containing both points, is m miles.

To keep it really straight forward, we should assume the earth is spherical, so that the answer is independent of location. I'm using mean radius 3,958.761 miles.

One radian is the angle for a line 3,958.761 miles long, so the angle for a line m miles long is m/3,958.761 radians. Use Math.toDegrees if you want the angle in degrees.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • 1
    with the information given, treating this as an abstract spherical angle rather than a lat/long problem seems the only approach. – Alnitak Jan 15 '13 at 19:26
2

Expressing your distance in degrees is an incorrect approach because the distance degrees represent is not equal in all positions on the earth and they are expressed differently for latitude and longitude.

Examples: 1° longitude on the equator is about 70 miles, whereas around the north pole it's a few feet.

Latitude does not change over the surface of the earth, but what about distances which are not along a meridian or a parallel? Which measure would you represent those in?

Zoltán
  • 21,321
  • 14
  • 93
  • 134
2

Answer by dwight-degroff is great (thanks, it helped me), but contains one mistype, which of course is of very low importance. If you want to be more correct (e.g. you're preparing tests based on external data coming from source with many decimal places), then it should be

Latitude: 1 km = 1 deg / 110.574 km

instead of:

Latitude: 1 km = 1 deg / 110.54 km

And thus for latitude it is:

    Integer latTraveledMiles = 100;
    Double latTraveledKM = 100 * 0.621371;
    Double latTraveledDeg = (1 / 110.574) * latTraveledKM;

And actually, for longitude it's 111.319 instead of 111.320, so:

    Double currentLat = 74.0064;
    Integer longTraveledMiles = 100;
    Double longTraveledKM = 100 * 0.621371;
    Double longTraveledDeg = (1 / (111.319 * Math.cos(currentLat))) * longTraveledKM;

However, both corrections might be negligible.

ppi
  • 86
  • 7