Select to view content in your preferred language

Combine multiple layers (point, line and polygon) into new feature set using data expression for Dashboard

957
3
11-16-2023 09:51 AM
CatherineCoe
New Contributor

Hello, 

I am trying to create a new data expression for an indicator element, which will combine the three layers from my map (which share the same attributes, but have different geometry types). I currently have an indicator for my point, line and polygon layer, but I would like a single indicator which will be able to count the number of features in each layer and sum together. 

I have used various combinations of codes from other queries, but still have the same error message "Test execution error: Compilation error - Line : 4, 33: Identifier not recognized. Verify test data." Here is the code that I have so far: 

// Portal
Portal("https://www.arcgis.com/");
// Create FeatureSet for points
var fs1 = FeatureSetByPortalItem(p,'2b84b6b511584a34807749f5d9969dd4&sublayer=0', 0, ['NatureReserve','Priority','IssueCategory','Notes','RecordDate','CompletionDate','CompletedBy','CreationDate','GlobalID'], true);

// Create Featureset for lines
var fs2 = FeatureSetByPortalItem(p,'2b84b6b511584a34807749f5d9969dd4&sublayer=1', 0,
['NatureReserve','Priority','IssueCategory','Notes','RecordDate','CompletionDate','CompletedBy','CreationDate','GlobalID'], true);

//Create Featureset for polygons
var fs3 = FeatureSetByPortalItem(p,'2b84b6b511584a34807749f5d9969dd4&sublayer=2', 0,
['NatureReserve','Priority','IssueCategory','Notes','RecordDate','CompletionDate','CompletedBy','CreationDate','GlobalID'], true);

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

// Iterate fs1
for (var feature in fs1) {
    
    // Filter points by polygon
    //var pts = Contains(feature, fs2);
    
    // Create feature with aggregated values
    feat = { 
        'attributes': { 
            'objecttype': ['PointObject'],
            'NatureReserve': feature['NatureReserve'],
            'Priority': feature['Priority'],
            'IssueCategory': feature['IssueCategory'],
            'Notes': feature['Notes'],
            'RecordDate': feature['RecordDate'],
            'CompletionDate': feature['CompletionDate'],
            'CompletedBy': feature['CompletedBy'],
            'CreationDate': feature['CreationDate'],
            'GlobalID': feature['GlobalID']
        }
    };
    
    // Push feature into array
    Push(features, feat);
};

for (var feature in fs2) {
    
    // Filter points by polygon
    //var pts = Contains(feature, fs2);
    
    // Create feature with aggregated values
    feat = { 
        'attributes': { 
            'objecttype': ['PointObject'],
            'NatureReserve': feature['NatureReserve'],
            'Priority': feature['Priority'],
            'IssueCategory': feature['IssueCategory'],
            'Notes': feature['Notes'],
            'RecordDate': feature['RecordDate'],
            'CompletionDate': feature['CompletionDate'],
            'CompletedBy': feature['CompletedBy'],
            'CreationDate': feature['CreationDate'],
            'GlobalID': feature['GlobalID']
        }
    };
    
    // Push feature into array
    Push(features, feat);
};

// Create dict for output FeatureSet
var out_dict = { 
    'fields': [
        {'name': 'objecttype', 'type': 'esriFieldTypeString'},
        {'name': 'NatureReserve', 'type': 'esriFieldTypeString'},
        {'name': 'Priority', 'type': 'esriFieldTypeString'},
        {'name': 'IssueCategory', 'type': 'esriFieldTypeString'},
        {'name': 'Notes', 'type': 'esriFieldTypeString'},
        {'name': 'RecordDate', 'type': 'esriFieldTypeDate'},
        {'name': 'CompletionDate', 'type': 'esriFieldTypeDate'},
        {'name': 'CompletedBy', 'type': 'esriFieldTypeString'},
        {'name': 'CreationDate', 'type': 'esriFieldTypeDate'},
        {'name': 'GlobalID', 'type': 'esriFieldTypeGlobalID'},
    ],
  'geometryType': '', 
  'features': features 
}; 

 I am very new to data expressions so any and all suggestions would be greatly appreciated. 

Thank you!

0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

You haven't instantiated the variable "p" used in your FeatureSetByPortalItem calls. Change line 2 to

var p = Portal("https://www.arcgis.com/");
0 Kudos
CatherineCoe
New Contributor

Thank you. I've now instantiated variable "p" but my output still won't run. It now says "Test execution error: Unknown Error. Verify test data"

0 Kudos
KenBuja
MVP Esteemed Contributor

Do you have access to the source? When I run this part of the code in the Playground, I get the same error since I don't have permissions to see that item.

var p = Portal("https://www.arcgis.com/");
// Create FeatureSet for points
var fs1 = FeatureSetByPortalItem(p,'2b84b6b511584a34807749f5d9969dd4&sublayer=0', 0, ['NatureReserve','Priority','IssueCategory','Notes','RecordDate','CompletionDate','CompletedBy','CreationDate','GlobalID'], true);
return fs1

Have you included the line "return FeatureSet(out_Dict);" to return the FeatureSet?

Use the console in various places in your code to make sure it's not breaking down somewhere. For example, examine the features variable at the completion of each loop.

0 Kudos