Return geometry in data expression to support spatial selection in dashboard

261
0
01-30-2024 08:59 AM
Labels (1)
erica_poisson
Occasional Contributor III

Hi - 

I'm sure someone on here has asked this and received an answer, but I can't find it. 

I have a data expression being used within a table element of a dashboard. I'd ideally like the user be able to make a selection (polygon) in the dashboard's map and then have the table be filter spatially based on the selection in the map using layer actions. 

This code works great with a category selector, but not with my configured layer actions (it returns no data). I suspect my geometry is not being included in the Push and final FeatureSet (starts at lines 108 to end). Any suggestions on how to correct this?

// custom memorize function to increase performance of featureset
function Memorize(fs) {
    var temp_dict = {
        fields: Schema(fs)['fields'],
        geometryType: Schema(fs).geometryType,
        features: []
    }

    for (var f in fs) {
        var attrs = {}

        for (var attr in f) {
            attrs[attr] = Iif(TypeOf(f[attr]) == 'Date', Number(f[attr]), f[attr])
        }

        Push(
            temp_dict['features'],
            {attributes: attrs, geometry: Geometry(f)}
        )
    }

    return FeatureSet(Text(temp_dict))
}
// variable to define the portal URL
var portal = Portal("https://mass-eoeea.maps.arcgis.com");

 
//Quabbin and Ware Encroachments
var qEncroach = FeatureSetByPortalItem(portal,"1234abc",0,['Date_Found', 'Type', 'Encroachment_Priority', 'Description'],true);

//Wachusett and Sudbury Encroachments
var wEncroach = FeatureSetByPortalItem(portal,"5678def",0,['Date_Found', 'Type', 'Encroachment_Priority', 'Description'],true);

// Create a FeatureSet with all encroachments from Quabbin, Ware and Wachusett

var int_dict = {
    fields: [
        {'name': 'type', 'type': 'esriFieldTypeString'},
        {'name': 'edate', 'type': 'esriFieldTypeDate'},
        {'name': 'priority', 'type': 'esriFieldTypeString'},
        {'name': 'descript', 'type': 'esriFieldTypeString'}],
    geometryType: 'esriGeometryPoint',
    "spatialReference": {
        "wkid": 102100,
        "latestWkid": 3857
    }, 'features':[],
  };


// Fill intermediate FeatureSet with encroachment info
var i = 0;
for (var f in qEncroach) {
    int_dict.features[i] = {
       attributes: {
         'type': f["Type"],
         'edate': DateDiff(f["Date_Found"], Date(1970, 0, 1, 0, 0, 0), "milliseconds"),
         'priority': f["Encroachment_Priority"],
         'descript': f["Description"],
       },
       geometry: Geometry(f),
     }
     i++;
   };

for (var g in wEncroach) {
    int_dict.features[i] = {
       attributes: {
         'type': g["Type"],
         'edate': DateDiff(g["Date_Found"], Date(1970, 0, 1, 0, 0, 0), "milliseconds"),
         'priority': g["Encroachment_Priority"],
         'descript': g["Description"],
       },
       geometry: Geometry(g),
     }
     i++;
   };

Console(Text(int_dict));
var encroach_set = Memorize(FeatureSet(Text(int_dict)))

// Basins
var basins = Memorize(FeatureSetByPortalItem(portal, "987406d7d0614370b6493dfe58c9e62c", 0, ['District', 'Subbasin_Name'], true))

// create final featureset
var finalDict = {
  fields: [
    {name: "Encroach_Type", type: "esriFieldTypeString"},
    {name: "Encroach_Date", type: "esriFieldTypeDate"},
    {name: "Encroach_Priority", type: "esriFieldTypeString"},
    {name: "Encroach_Description", type: "esriFieldTypeString"},
    {name: "SubbasinName", type: "esriFieldTypeString"},
    {name: "DistrictName", type: "esriFieldTypeString"},
  ],
  geometryType:"esriGeometryPoint",
  "spatialReference": {
    "wkid":102100,
    "latestWkid":3857
  },
  features: [],
}

// intersect CMRs with basins to determine which basin each occured in
for(var e in encroach_set) {
  var i_basin = First(Intersects(e, basins))
  var i_basin_name = Iif(i_basin == null, "No basin", i_basin.Subbasin_Name)
  var i_basin_district = Iif(i_basin == null, "No district", i_basin.District)
  //add to array
  Push(finalDict['features'], 
  {attributes:{
    Encroach_Type: e['type'],
    Encroach_Date: e['edate'],
    Encroach_Priority: e['priority'],
    Encroach_Description: e['descript'],
    SubbasinName: i_basin_name,
    DistrictName: i_basin_district,
  }})
}
Console(Text(finalDict));
return FeatureSet(Text(finalDict))

 

Erica
0 Kudos
0 Replies