I'm giving my first steps with OpenLayers3, and I'm getting somewhat strange results when trying to obtain the area of a selected feature.
I've a map who loads a vector layer from a KML source. On those KML there are several features represented as multipolygons which represents the borders of some north Spain's regions.
When I select the features, they change correctly it's styles according to my code, but when I try to retrieve it's area through multipolygons.getArea I obtain values a lot bigger than expected (almost double).
My map code:
<div id="map" class="map" style="width:100%; height:600px;"></div>
<script type="text/javascript">
var projection = new ol.proj.get("EPSG:3857");
var mapa = new ol.layer.Tile({
source: new ol.source.MapQuest({ layer: 'osm' })
});
var kmlLayer = new ol.layer.Vector({
source: new ol.source.KML({
projection: projection,
url: "/Content/Documents/CCAA.txt",
extractStyles: false
})
});
var select = new ol.interaction.Select({
condition: ol.events.condition.click,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(0, 255, 0, 0.5)"
}),
stroke: new ol.style.Stroke({
color: "#000000",
width: 2
})
})
});
var map = new ol.Map({
interactions: new ol.interaction.defaults().extend([select]),
target: 'map',
layers: [ mapa, kmlLayer ],
view: new ol.View({
center: [-426970.8463461736, 5299807.853963373],
projection: projection,
zoom: 7
})
});
select.on('select', function (e) {
var feature = e.selected[0];
console.log(feature.getGeometry().getArea());
});
map.addControl(new ol.control.ScaleLine());
</script>
And this is an example of the KML data:
<Placemark>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-4.57486104965204,43.4001388549807,0
.....
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Multigeometry>
</Placemark>
The getArea of this multipolygon is returning 20021810258.82251 (I assume square meters as I'm using Mercator Projection), while the expected result would be about 10000 square km.
I'm aware of the difficulties and problems with projections to a plane, and I was expecting a rough area result, but I'm getting twice the value I expected... it seems too much for a problem with projections or calculation... so I think there's something I'm missing here.
Any insight on the matter will be much appreciated.
This is very similar to: https://gis.stackexchange.com/questions/142062/openlayers-3-linestring-getlength-not-returning-expected-value
You can read the backgrounds and explanations in there. Look at the measure example to see how it should be done currently.