16

I'm using OpenLayers with an ordinary mercator map and I'm trying to sample a bounding box by finding a grid of points in latlong. The bbox is expressed in latlon, e.g.

48.1388,-15.3616,55.2057,-3.9359

I can define a distance in degrees (e.g. x: 2.5, y: 2.4) and work out the points from there. But I'd like to express this distance in metres (e.g. 50000) in order to relate it to the user mindset (people understand metres, not degrees). How can I convert this distance? I know how to reproject a point, but not a distance.

Thanks for any hints! Mulone

winwaed
  • 7,645
  • 6
  • 36
  • 81
Mulone
  • 3,603
  • 9
  • 47
  • 69

3 Answers3

22

Use the haversine formula to get the distance between two points of lat/long. This assumes the earth is a sphere (which is, for most cases, "good enough").

A Javascript implementation of it (shamelessly stolen from here) looks like this:

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;
blindauer
  • 374
  • 3
  • 5
  • 1
    The Haversine works for different latitudes but is the opposite of what the OP is requesting. The OP wants to map linear (metre) distance to degrees latitude and longitude. – winwaed Nov 05 '10 at 00:34
11

Without allowing for the slightly non-spherical shape of the earth,

One minute of latitude North to south = 1 Nautical Mile = 6075 feet So One degree = 60 Minutes = 60 * 6075 feet There are 3.28 Feet in a meter so One degree = 60 * 6075 / 3.28 Meters = 111,128 meters

Alternatively, one minute of Latitude = 1,852 Meters So One degree = 60 * 1852 meters = 111,120 meters

I'm not sure which is more accurate...

For One degree of Longitude, do the same thing, but multiply by the Cosine(Latitude) since the Longitude lines converge, (get closer together), as you move north.

Note: if you're using a calculator or computer, make sure you use the right units for the latitude (degrees or radians) that your device requires or is set to use.

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • This answer seems to contradict https://stackoverflow.com/a/1253545/1889720. I assume I am misunderstanding something. What is the difference between the two answers? – Evorlor Sep 26 '22 at 14:12
  • The two answers are different by only (111.128 - 110.574)/ 111.128 = 0.498% – Charles Bretana Sep 27 '22 at 03:10
  • What causes the difference? I'm unclear on which numbers I should be using. – Evorlor Sep 27 '22 at 12:38
  • 1
    You are missing the point. In the real world, No numerical measurement answer is or ever can be 100% accurate. You are not *counting* things! You are *measuring* something real. Any measurement you make is and will be inaccurate to a degree. The question is to what degree, (what level of accuracy), is the measurement ? If you need accuracy greater than +- 0.498%, then *none* of the simplified formulae will suffice for every point or pair of points on the globe. If you demand perfect accuracy, then go out and measure it with a tape measure. And *that* will change from moment to moment! – Charles Bretana Sep 27 '22 at 18:50
  • Gotcha. Makes sense. I imagined it to be as accurate as going out with measuring tape. Good to know it isn't! Thanks! – Evorlor Sep 27 '22 at 20:17
  • 1
    No *Measurement* can ever be 100% accurate. Even the Tape measure! How accurate is the tape measure? It shrinks and expands as temperature changes, as you pull on it, If it is bent slightly, etc. etc. Sure they are small changes, but that's the point! They are way bigger than a laser measurement that counts wavelengths. And laser wavelengths change depending on the strength of the gravitational field. The real question is: How big an error or uncertainty is *significant* for the purpose for which you need the measurement? – Charles Bretana Sep 27 '22 at 21:22
  • 1
    There used to be a somewhat facetious saying in carpentry: "Measure with a micrometer, mark it with a pencil, cut it with an axe,." – Charles Bretana Sep 27 '22 at 21:24
1

The transformation between degrees and metres varies across the Earth's surface.

Assuming a spherical Earth, degrees latitude = distance * 360 / (2*PI * 6400000)

Note that longitude will vary according to the latitude:

Degrees longitude = distance *360 * / (2*PI* cos(latitude) )

The above is for the Earth's surface, and does not use the Mercator projection. If you wish to work with projected linear distance, then you will need to use the Mercator projection.

winwaed
  • 7,645
  • 6
  • 36
  • 81
  • And for the more general case when working with different map projections in javascript, use the open source Proj4js library. - which is also used by OpenLayers. – winwaed Jan 01 '11 at 21:07
  • Unit of measurements of the longitude makes no sense. You have degrees on one side and meters times degrees on the other. Do you have to divided times the radius earth, as ` longitude = distance *360 * / (2*PI* cos(latitude) * 6400000)`? – SeF Dec 22 '21 at 22:36