Select to view content in your preferred language

Data expression - list of merged polygons that intersect with another polygon layer (two FeatureSets)

399
1
08-03-2023 08:49 AM
KerryKang
Occasional Contributor III

Hello, 

 

I hope to get some direction on how I can get the list of merged polygons (A+B) that intersect another polygon layer (C).

 

I have two polygon layer A. Municipalities and B. Regional Districts. I merged A and B into a new FeatureSet in Data Expression using common attributes such as admin type and name of the area.

 

Then, I want to count how many of these admin areas are affected by a weather event (polygon C), ideally returning all the names & geometry, not just count of intersects so that I can use the returned FeatureSet for further like filters.

 

-- This is what I did so far. The third  variable (fs3) is the one need to be used to define intersect, and has not been used yet.

 

====================================


// Portal
var p = Portal('https://xyz.maps.arcgis.com');

// Create FeatureSet for points - municipality
var fs1 = FeatureSetByPortalItem(p,'xxx', 0, ['*'], true);

// Create Featureset for lines - regional district
var fs2 = FeatureSetByPortalItem(p,'yyy', 13 ,['*'], true);
 
var fs3  = Filter(FeatureSetByPortalItem(Portal('https://arcgis.com/'), 'zzz',1,['*'], true),"ImpactLevel=5");
 

// Create empty feature array and feat object for output
var features = [];
var feat;

// Iterate fs1
for (var feature in fs1) {
   
    // Create feature with aggregated values
    feat = {
        'attributes': {    
            'admin_type': ['Municipality'],
            'name':feature['Admin_area_name']
        }
    };
   
    // Push feature into array
    Push(features, feat);
};

for (var feature in fs2) {
 
   
    // Create feature with aggregated values
    feat = {
        'attributes': {
            'admin_type': ['Regional District'],
            'name':feature['admin_area_name']
        }
    };
   
    // Push feature into array
    Push(features, feat);
};

// Create dict for merged output FeatureSet
var out_dict = {
    'fields': [
       
        {'name': 'admin_type', 'type': 'esriFieldTypeString'},
        {'name': 'name', 'type': 'esriFieldTypeString'}
       
    ],
  'geometryType': '',
  'features': features
};



// Convert dictionary to feature set - mergend A and B
//return FeatureSet(Text(out_dict));
 
//Intersect the new FeatureSet with fs3 (polygon C)
0 Kudos
1 Reply
JohannesLindner
MVP Frequent Contributor

To post formatted code:

JohannesLindner_0-1689266678674.pngJohannesLindner_1-1689266693900.png

 

  • In your loops over fs1 and fs2, you use feature as iteration variable. This overwrites the native Arcade function Feature. That's not a problem here, but in general, it's not recommended to do that, because you can't use those native functions anymore.
  • You try to input arrays into the admin_type field. 'admin_type': ['Municipality'] should be 'admin_type': 'Municipality'
  • You say you want to output the geometry, but you don't set it in your output fs nor in your out features.
    • Upon further inspection, you seem to have points (municipalities), lines (districts) and polygons (weather events). You can't combine these in the same featureset.

 

You can get  the features intersecting a target feature with Intersects(). You would need a third loop over your merged features that intersects those features with the weather events.

But actually, it should be faster to do it the other way around: 

  • loop over the weather events
    • intersects with fs1 and copy each intersected feature
    • do the same for fs2

Something like this:

var municipalities = 
var districts = 
var weather_events = 

out_fs = {
    fields: [
        {name: "admin_type", type: "esriFieldTypeString"},
        {name: "name", type: "esriFieldTypeString"},
    ],
    geometryType: "",
    features: []
}

for(var weather_event in weather_events) {
    var i_municipalities = Intersects(weather_event, municipalities)
    for(var m in i_municipalities) {
        var f = {attributes: {admin_type: "Municipality", name: m.Municipality}}
        Push(out_fc.features, f)
    }
    var i_districts = Intersects(weather_event, districts)
    for(var d in i_districts) {
        var f = {attributes: {admin_type: "Regional District", name: d.admin_area_name}}
        Push(out_fc.features, f)
    }
}
return Featureset(out_fs)

 


Have a great day!
Johannes
0 Kudos