1

I am using getLength to retrieve the linestring length.

For the same segment: 1- when using google map measure tool, I get 228m 2- when using IGN geoportail measure tool, I get 228m 3- when I use e.feature.getGeometry().getLength() I get 330m

Here are the flat coordinates: e.feature.getGeometry().getFlatCoordinates() : [571382.4214041593, 5723486.068714521, 571593.8175605105, 5723741.65502785]

in 4326: [5.132815622245775, 45.644023326845485, 5.134714626228319, 45.64562844964627]

When I check the coordinates position on either ol3 or google map, I get the same points. The difference must come from the calcul...

Did I miss something and I should not use the getLength method? Please give me some direction if you think this is not an issue.

I've done many tests and I get a 1.44 factor all the time between the distances. Maybe it's a unit problem. But I though the default unit in ol3 was meter. I just divide the getLength result by 1.44 but I don't like it...

2 Answers2

2

Use geodesic measurements, see http://openlayers.org/en/v3.4.0/examples/measure.html for an example.

bartvde
  • 2,469
  • 10
  • 6
  • Thnak you for your help, do you know what is the getLenth() function returning then? – Alexandre Mélard Apr 10 '15 at 07:25
  • Just normal length, where the geometry is not projected on a sphere. In OpenLayers 2 we had getLength and getGeodesicLength. For ol3 we just provide the convenience functions and you can do this at the application level pretty easily as you can see in the measure example. – bartvde Apr 10 '15 at 10:03
  • Thank you for the details, it's a bit confusing for the neophyte like me. I think it's a huge difference though on such a small distance. (100m) Don't you agree? – Alexandre Mélard Apr 10 '15 at 10:21
  • You made me curious, is there a way in a mountain area to get real distance taking into account the elevation ? – Alexandre Mélard Apr 10 '15 at 10:22
1

To expand a bit on Bart's answer, your post is missing an important piece of information: which projection you are using. When you add a vector source to the map, the coordinates are converted into the projection of the view so they can be displayed in the right place; in your case, this looks like EPSG:3857. getLength() uses standard geometry to calculate the length on that 2D plane. Mercator's projection however increasingly distorts longitude distances the further you go from the equator, so getLength() isn't useful in this case. Your coordinates are in France, so try using the standard French Lambert93 projection instead, which more closely matches the geometry of France. Go to for example http://geofree.fr/gf/coordinateConv.asp#listSys and convert your coordinates into Lambert, create a geometry using them ls=new ol.geom.LineString([[866112.5, 6507185.9], [866255.5, 6507368.1]]), and ls.getLength() gives you a similar answer to what Google provides. Google uses Haversine, which you can use in ol3 as bartvde says. This doesn't use the projected coordinates, but the lat/longs. It uses a sphere model, so isn't very accurate, as the earth isn't a perfect sphere; "can't be guaranteed correct to better than 0.5%" as Wikipedia puts it. Vincenty uses an ellipsoid, so is a more complicated formula, but more accurate, claimed to be within 0.5mm.

Bottom line: if your view is 4326 or 3857, don't use getLength().

See Does google maps use elevation to calculate travel distance? for your question on elevation. For most practical purposes (how far did I walk/run/cycle/ski?) elevation won't make much difference.