Select to view content in your preferred language

Calculating Percentage of feature layer's geometry

956
9
Jump to solution
11-21-2023 01:35 PM
Charan
by
Emerging Contributor

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?

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

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}`);
        }
    });
}

View solution in original post

0 Kudos
9 Replies
BlakeTerhune
MVP Regular Contributor

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.

0 Kudos
Charan
by
Emerging Contributor

Hi @BlakeTerhune Will I be using the intersect results to calculate the area?

0 Kudos
BlakeTerhune
MVP Regular Contributor

Yes.

percentage = (part/whole)*100

The "part" will be the intersect geometry area and the "whole" is the original feature geometry area.

0 Kudos
Charan
by
Emerging Contributor

Hi @BlakeTerhune so I used the following code to get the geometries of the intersected results. However, I cant find the geometry for it

                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
                      if (values.length > 0) {
                        for (var a = 0; a < values.length; a++) {
                          console.log(values[a].attributes['DACODE'],geometryEngine.intersect(values[a].geometry, geoslabel[i].geometry))
                        }
                      }
                    }
                    )
                }
Charan_0-1700670670276.png

 


 


 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Sorry, I don't understand the question. Please clarify your end goal.

0 Kudos
Charan
by
Emerging Contributor

Hi @BlakeTerhune I have updated my question!

0 Kudos
BlakeTerhune
MVP Regular Contributor

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}`);
        }
    });
}
0 Kudos
irmcintosh
Emerging Contributor
You could probably use arcade Expressions to capture that information,
unless you want to store it as an attribute.
BlakeTerhune
MVP Regular Contributor

Indeed, there are geometry functions for Arcade too.

Intersection
Area

0 Kudos