How to Determine the Area of a Polygon in OpenLayers

Create a polygon feature., Add the feature to a vector layer., Transform the feature’s geometry to use coordinates., Create a sphere to perform the calculation., Use the sphere to calculate the area using the geodesicArea() method., Check that the...

6 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: Create a polygon feature.

    The Polygon constructor function needs an array of coordinate arrays; define this array in a variable first so that you can use it later.

    Simply copy the following line of code into your <script></script> element:. var coordinates = , , , ]; var polygon_feature = new ol.Feature({ geometry: new ol.geom.Polygon( ) });
  2. Step 2: Add the feature to a vector layer.

    To add the polygon to the map, you need to add it to a source, which you add to a vector layer, which you can then add to the map: var vector_layer = new ol.layer.Vector({ source: new ol.source.Vector({ features: }) }) map.addLayer(vector_layer)
  3. Step 3: Transform the feature’s geometry to use coordinates.

    var current_projection = new ol.proj.Projection({code: "EPSG:4326"}); var new_projection = tile_layer.getSource().getProjection(); polygon_feature.getGeometry().transform(current_projection, new_projection)
  4. Step 4: Create a sphere to perform the calculation.

    The sphere should be the size of the Earth (should have a radius of
    6.3m meters).

    Technically, the sphere has a radius is equal to the semi-major axis of the WGS84 ellipsoid. var sphere = new ol.Sphere(6378137)
  5. Step 5: Use the sphere to calculate the area using the geodesicArea() method.

    Because the method provides a value in square metres, divide by a million to get square kilometres. var area_m = sphere.geodesicArea(coordinates); var area_km = area_m / 1000 / 1000; console.log('area: '

    area_km, 'km²'); // CONSOLE: area:
    2317133.7166773956 km² , We know that it's correct because it appears to be approximately the same size as Algeria, which has an area of 2,381,741 km² (from Wikipedia).
  6. Step 6: Check that the area answer makes sense.

Detailed Guide

The Polygon constructor function needs an array of coordinate arrays; define this array in a variable first so that you can use it later.

Simply copy the following line of code into your <script></script> element:. var coordinates = , , , ]; var polygon_feature = new ol.Feature({ geometry: new ol.geom.Polygon( ) });

To add the polygon to the map, you need to add it to a source, which you add to a vector layer, which you can then add to the map: var vector_layer = new ol.layer.Vector({ source: new ol.source.Vector({ features: }) }) map.addLayer(vector_layer)

var current_projection = new ol.proj.Projection({code: "EPSG:4326"}); var new_projection = tile_layer.getSource().getProjection(); polygon_feature.getGeometry().transform(current_projection, new_projection)

The sphere should be the size of the Earth (should have a radius of
6.3m meters).

Technically, the sphere has a radius is equal to the semi-major axis of the WGS84 ellipsoid. var sphere = new ol.Sphere(6378137)

Because the method provides a value in square metres, divide by a million to get square kilometres. var area_m = sphere.geodesicArea(coordinates); var area_km = area_m / 1000 / 1000; console.log('area: '

area_km, 'km²'); // CONSOLE: area:
2317133.7166773956 km² , We know that it's correct because it appears to be approximately the same size as Algeria, which has an area of 2,381,741 km² (from Wikipedia).

About the Author

T

Teresa Thomas

A passionate writer with expertise in home improvement topics. Loves sharing practical knowledge.

37 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: