Arcade Data Expression - Create dictionary from Geometry

1993
1
Jump to solution
03-22-2022 03:01 PM
Labels (1)
SteveRichards1
New Contributor II

Hello All,

I am new to Arcade Data Expressions and I would like to return a Feature Set that contains all of the "Fire Numbers" and the corresponding "District" (polygon) that it falls within. I started off with the code in the below link and tried to modify it a little to make it work for my situation. Mainly, I added an additional sub "for-loop" to iterate through the points and for each point determine the district it falls within and put that into an array and finally a dictionary.

https://github.com/Esri/arcade-expressions/blob/master/dashboard_data/SpatialAggregation.md

The Data Expression will be used within a Category Selector so I can filter the fire numbers out that only fall within a specific district (a district selector would filter out this data expression feature set to show only relevant fire numbers )

I am not able to get this to work and I am unsure what to try next. Can anyone potentially guide me in the right direction or maybe show another approach? Any help is very much appreciated!

 

// Portal
var portal = Portal('https://governmentofbc.maps.arcgis.com/');

// Create FeatureSet for polygons
var poly_fs = FeatureSetByPortalItem(
    portal,
    '5ccd439dd835451aa304468ebdce0427',
    58,
    [
        'ORG_UNIT'
    ],
    true
);

// Create Featureset for points
var pt_fs = FeatureSetByPortalItem(
    portal,
    '397a1defe7f04c2b8ef6511f6c087dbf',
    0,
    [
        'FIRE_NUMBER'
    ],
    true
);

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

// Iterate over fire points
for (var pt in pt_fs) {
    // Iterate over districts
    for (var poly in poly_fs) {
    
        // Filter points by polygon
        var pts = Contains(poly, pt);
        
        // Create feature with aggregated values
        feat = { 
            'attributes': { 
                'district': poly['ORG_UNIT'], 
                'fire': pt['FIRE_NUMBER']
            }
        };
        
        // Push feature into array
        Push(features, feat);
    };
};

// Create dict for output FeatureSet
var out_dict = { 
    'fields': [
        {'name': 'district', 'alias': 'Districts', 'type': 'esriFieldTypeString'},
        {'name': 'fire', 'alias': 'Fire Number', 'type': 'esriFieldTypeString'}
    ],
  'geometryType': '', 
  'features': features 
}; 

// Convert dictionary to feature set. 
return FeatureSet(Text(out_dict)); 

 

0 Kudos
1 Solution

Accepted Solutions
SteveRichards1
New Contributor II

OK. I was able to get it to work finally. The issue was that I was not passing in a FeatureSet for the inside geometry (points) as part of the Contains function, which caused an error. I looped through the returned points after creating the list of points that intersected each polygon. Working code below.

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



// Iterate over districts
for (var poly in poly_fs) {

    // Filter points by polygon
    var pts = Contains(poly, pt_fs);
    
    for (var pt in pts) {
    
        // Create feature with aggregated values
        feat = { 
            'attributes': { 
                'district': poly['ORG_UNIT'], 
                'fire': pt['FIRE_NUMBER']
            }
        };
        
        // Push feature into array
        Push(features, feat);
    };
};

// Create dict for output FeatureSet
var out_dict = { 
    'fields': [
        {'name': 'district', 'alias': 'Districts', 'type': 'esriFieldTypeString'},
        {'name': 'fire', 'alias': 'Fire Number', 'type': 'esriFieldTypeString'}
    ],
  'geometryType': '', 
  'features': features 
}; 

// Convert dictionary to feature set. 
return FeatureSet(Text(out_dict)); 

 

View solution in original post

0 Kudos
1 Reply
SteveRichards1
New Contributor II

OK. I was able to get it to work finally. The issue was that I was not passing in a FeatureSet for the inside geometry (points) as part of the Contains function, which caused an error. I looped through the returned points after creating the list of points that intersected each polygon. Working code below.

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



// Iterate over districts
for (var poly in poly_fs) {

    // Filter points by polygon
    var pts = Contains(poly, pt_fs);
    
    for (var pt in pts) {
    
        // Create feature with aggregated values
        feat = { 
            'attributes': { 
                'district': poly['ORG_UNIT'], 
                'fire': pt['FIRE_NUMBER']
            }
        };
        
        // Push feature into array
        Push(features, feat);
    };
};

// Create dict for output FeatureSet
var out_dict = { 
    'fields': [
        {'name': 'district', 'alias': 'Districts', 'type': 'esriFieldTypeString'},
        {'name': 'fire', 'alias': 'Fire Number', 'type': 'esriFieldTypeString'}
    ],
  'geometryType': '', 
  'features': features 
}; 

// Convert dictionary to feature set. 
return FeatureSet(Text(out_dict)); 

 

0 Kudos