4

I'm calculating distance in OpenLayers 3 (3.9.0).

I've got a function included in my project for measuring line/area features, basically lifted from the OpenLayers Examples. I'm using haversineDistance to calculate accurate lengths/areas, but keep getting an error preventing the numbers from being reported.

Basically, when I start to measure in the browser and console.log the measurements so I can see them, they appear to be reporting accurately; however, a js error in ol.js is preventing the measurements from being returned in the webpage so the viewer can see them. Error is 'Uncaught TypeError: Cannot read property 'length' of undefined'. Reports as occuring in ol.js on line 69.

My code for the function is:

var formatLengthFt = function(line) {
var length_ft = 0;
var coords = line.getCoordinates();

for (var i = 0; i < coords.length; i++) {
    var c1 = ol.proj.transform(coords[i], 'EPSG:3857', 'EPSG:4326');
    var c2 = ol.proj.transform(coords[i + 1], 'EPSG:3857', 'EPSG:4326');

    length_ft += wgs84Sphere.haversineDistance(c1, c2);
    console.log(length_ft);
}
return length_ft;
}

Before employing haversineDistance, I simply used line.getLength(), but then realized the distances/areas were all inaccurate. So, tried the solution below with success, just can't report the numbers in the webpage. I've also tried using turf.js, with some level of success. At any rate, I just need a method for getting accurate distances/areas and would prefer to get the code above working correctly, but open to other solutions as well.

  • Just a quick check - have you included wgs84Sphere var wgs84Sphere = new ol.Sphere(6378137); as described at groups.google.com/forum/… – Peter Scott Oct 4 '15 at 3:46
  • Yeah, I did. Oddly enough, I got it working by simply deleting all previous code and starting fresh. I tried so many methods, commenting out each method after trying, that I'm guessing I simply had some bad syntax in there somewhere from not paying close attention. Anyways, the haversineDistance method works like a charm now. – wayoutwest Oct 5 '15 at 17:23
2

An answer (originally edited into their question) by the asker was:

Got this working with the code below. Basically the same, just minor changes than above. I think the issue was that I had some bad syntax somewhere in my JS file. I had commented out a lot of code as I was trying different methods, and likely just had fully commented out some code that started to interfere. Anyways, code below functions great.

var formatLength = function(line) {
    var length = 0;
    var coordinates = line.getCoordinates();
    var sourceProj = map.getView().getProjection();

    for (var i = 0, ii = coordinates.length - 1; i < ii; ++i) {
        var c1 = ol.proj.transform(coordinates[i], sourceProj, 'EPSG:4326');
        var c2 = ol.proj.transform(coordinates[i + 1], sourceProj, 'EPSG:4326');
        length += wgs84Sphere.haversineDistance(c1, c2);
    }
    var output;

    output = ((Math.round(length * 100) / 100)*3.280839).toFixed(1);
    return output;
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.