Hi everyone, so I'm allowing users to draw buffer rings and I was wondering if its possible to calculate how much percentage of the feature layer's area/geometry is within the buffer. Is there an api to do such thing?
Solved! Go to Solution.
You are only working with the whole geometry of each feature in your loop. Nowhere are you calling the intersect method. I'm just guessing that geoslabel[i].geometry is the other geometry you want to calculate the percentage with...
for (i=0; i< geoslabel.length; i++) {
let querycentroids2 = TradeArea_Data.createQuery();
querycentroids2.geometry = geoslabel[i].geometry;
querycentroids2.where = "year = 2022";
querycentroids2.spatialRelationship = "intersects";
querycentroids2.outFields=["DACODE"];
querycentroids2.returnGeometry = true;
await TradeArea_Data.queryFeatures(querycentroids2)
.then (function (response) {
const values = response.features
for (var a = 0; a < values.length; a++) {
const intersect_geom = geometryEngine.intersect(values[a].geometry, geoslabel[i].geometry);
const intersect_area = geometryEngine.geodesicArea(intersect_geom, 'square-kilometers');
const feature_area = geometryEngine.geodesicArea(values[a].geometry, 'square-kilometers');
const intersect_percent = (intersect_area / feature_area) * 100;
console.log(values[a].attributes['DACODE'], `feature_area = ${feature_area}`, `intersect_area = ${intersect_area}`, `intersect_percent = ${intersect_percent}`);
}
});
}
If you're working with the Maps SDK for JS, try using the intersect method of the geometryEngine. You would intersect the two geometries, calculate the area of the intersect result, then compare that with the total area of the original feature.
Hi @BlakeTerhune Will I be using the intersect results to calculate the area?
Yes.
percentage = (part/whole)*100
The "part" will be the intersect geometry area and the "whole" is the original feature geometry area.
Hi @BlakeTerhune so I used the following code to get the geometries of the intersected results. However, I cant find the geometry for it
Sorry, I don't understand the question. Please clarify your end goal.
Hi @BlakeTerhune I have updated my question!
You are only working with the whole geometry of each feature in your loop. Nowhere are you calling the intersect method. I'm just guessing that geoslabel[i].geometry is the other geometry you want to calculate the percentage with...
for (i=0; i< geoslabel.length; i++) {
let querycentroids2 = TradeArea_Data.createQuery();
querycentroids2.geometry = geoslabel[i].geometry;
querycentroids2.where = "year = 2022";
querycentroids2.spatialRelationship = "intersects";
querycentroids2.outFields=["DACODE"];
querycentroids2.returnGeometry = true;
await TradeArea_Data.queryFeatures(querycentroids2)
.then (function (response) {
const values = response.features
for (var a = 0; a < values.length; a++) {
const intersect_geom = geometryEngine.intersect(values[a].geometry, geoslabel[i].geometry);
const intersect_area = geometryEngine.geodesicArea(intersect_geom, 'square-kilometers');
const feature_area = geometryEngine.geodesicArea(values[a].geometry, 'square-kilometers');
const intersect_percent = (intersect_area / feature_area) * 100;
console.log(values[a].attributes['DACODE'], `feature_area = ${feature_area}`, `intersect_area = ${intersect_area}`, `intersect_percent = ${intersect_percent}`);
}
});
}
Indeed, there are geometry functions for Arcade too.