1

At the moment I am working on an OpenLayers project or rather I am learning on a project.

I stumbled upon a feature:

When you click on a polygon on the OpenLayers map and that plygon is selected and marked, I would like you to bring me from PostGis or calculate openlayers the size of the sides of the polygon one by one and pass them on that line in "m "..

enter image description here

geometry coordinates:

flatCoordinates: Array (12)
0: 3024661.117360038
1: 5657480.092961456
2: 3024466.963703795
3: 5656972.883250752
4: 3024488.1992827402
5: 5657122.118405258
6: 3024485.744880199
7: 5657251.372586279
8: 3024551.9375920547
9: 5657592.494035185
10: 3024661.117360038
11: 5657480.092961456
length: 12

I tried something after a long search on this forum and I found this:

feature.getGeometry (). getArea ()

but it only returns a number to me, not an array with all the sides of points A-B, B-C, C-D and so on.

Here I would ask for your help ...

2 Answers 2

2

feature.getGeometry ().getCoordinates() gives you all the points you need. It will return an array and first element of the array is the array of points you need. Here your polygon is simple as it does not have holes and is not a multipolygon. It may be less simpler with multipolygons as you will need to get coordinates from each polygon from the multipolygon.

From this coordinates, you can either create an ol.geom.LineString and use it method getLength for projected coordinates (considering your projection keeps distance well) or use spheric length calculation with ol.sphere.getDistance

0

Many thanks to ThomasG77 for his instructions and I will solve it here:

var array_coordinates = feature.getGeometry().getCoordinates();
                                                                                            
console.log(array_coordinates);
var dist_array = [];
for(var o = 0; o < array_coordinates[0].length - 1; o++){
            var point  = array_coordinates[0][o];
            //console.log(point);
                                                     
            PointA = array_coordinates[0][o];
            PointB = array_coordinates[0][o + 1];
            console.log(PointA+"  "+PointB);
                                               
            var dist = calc_dist(PointA, PointB);
            var formated_dist = format_dist(dist);
                                                     
            var line = new ol.geom.LineString([PointA, PointB]);
            dist = line.getLength();
            dist_array.push(dist);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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