Hi i want to calculate area of polygons drawn on map geometry, any arcgis sample will be helpful
Rajni,
A ring is just an array of coordinate and not an actual geometry class thus the geodesicArea method is failing.
Here is what you need to do instead.
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">calculateArea</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">var</SPAN> area <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">;</SPAN> graphicsLayer<SPAN class="punctuation token">.</SPAN>graphics<SPAN class="punctuation token">.</SPAN><SPAN class="token function">map</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">function</SPAN> <SPAN class="punctuation token">(</SPAN>grap<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> area <SPAN class="operator token">=</SPAN> geometryEngine<SPAN class="punctuation token">.</SPAN><SPAN class="token function">geodesicArea</SPAN><SPAN class="punctuation token">(</SPAN>grap<SPAN class="punctuation token">.</SPAN>geometry<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"hectares"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">log</SPAN><SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//End calculateArea</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Do i need to convert them rings_for_area to webMercatorUtils.geographicToWebMercator, Ok i have converted it and now it returns 0 area value, even when i have a polygon on map
function calculateArea(){ var rings_for_area=[]; var k=0; graphicsLayer.graphics.map(function(grap){ var updatedGeometry = webMercatorUtils.webMercatorToGeographic(grap.geometry); var rings_area = updatedGeometry.rings; rings_for_area= []; rings_area.forEach(function(ring, i) { rings_for_area=ring; k++; }); }); console.log(rings_for_area); var area = geometryEngine.geodesicArea( webMercatorUtils.geographicToWebMercator(rings_for_area), "hectares"); console.log(area); }//End calculateArea
Check out this test app to see how it works. Hopefully, it will point you to a right direction.
-Undral
Hi ken, this time i summed it up but due to this bolded code to calculate area, my whole map stops working. No error in console even. No area consoled even.
function calculateArea(){ var rings_for_area=[]; var k=0; graphicsLayer.graphics.map(function(grap){ var updatedGeometry = webMercatorUtils.webMercatorToGeographic(grap.geometry); var rings_area = updatedGeometry.rings; rings_for_area= []; rings_area.forEach(function(ring, i) { rings_for_area=ring; k++; }); }); console.log(rings_for_area); var area = geometryEngine.geodesicArea(rings_for_area, "hectares"); console.log(area); }//End calculateArea
You have a scope problem. Since the variable rings_for_area is declared inside the map function, it's not available outside the function. Why aren't you calculating the area of the rings inside the map function and summing that up?
Hi Noah , I m trying to get geometry of all polygons from a map with below code, but doen't get rings area in console.
function calculateArea(){ graphicsLayer.graphics.map(function(grap){ var updatedGeometry = webMercatorUtils.webMercatorToGeographic(grap.geometry); var rings_for_area = updatedGeometry.rings; console.log(rings_for_area);//return all rings }); var area = geometryEngine.geodesicArea(rings_for_area, "hectares"); console.log(area);//doesn't return area }
There is also a lot of good information inside the GeometryEngine class:
geometryEngine | ArcGIS API for JavaScript 4.14
Take a look at the measure widget: Measurement in 2D | ArcGIS API for JavaScript 4.14
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.