Push a feature in a featureSet - Arcade script

661
2
Jump to solution
10-27-2023 03:27 AM
HamzaMerini
New Contributor

Hello,

I would like to add features in a featureSet, I initialize a featureSet and depending on other features I would like to push elements of these features on conditions :

 

var myFeatureSet = FeatureSet({
    layerDefinition: {
        objectIdField: "FID",
        geometryType: "esriGeometryPolygon",
        fields: [
            { name: "FID", alias: "FID", type: "esriFieldTypeOid" },
            { name: "attributeX", alias: "attributeX", type: "esriFieldTypeDate" },
            { name: "attributeY", alias: "attributeY", type: "esriFieldTypeString" }
        ]
    },
    featureset: {
        features: [Feature(null, "FID", 1, "attributeX", "Hello World")]
    }
});

var features = FeatureSetByName($map, myLayer,["*"],false)
var filteredFeature = Filter(features,
     'attributeZ is not null')

for(var feat in filteredFeature){
// Push the feat in myFeatureSet
}

 

 

I tried to do it with Push function but I get Invalid Input ERROR

How can I do it ? 

Thanks !

 

 

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Your code doesn't follow the way that a FeatureSet is established, according to the docs.

https://developers.arcgis.com/arcade/function-reference/featureset_functions/#featureset

Try defining the geometryType, fields, and features, but don't nest them in separate layerDefinition an dfeatureset objects.

var fs_json = {
  geometryType: "esriGeometryPolygon",
  fields: [
    { name: "FID", alias: "FID", type: "esriFieldTypeOid" },
    { name: "attributeX", alias: "attributeX", type: "esriFieldTypeDate" },
    { name: "attributeY", alias: "attributeY", type: "esriFieldTypeString" }
  ],
  features: [
    {attributes: {FID: 1, attributeX: "Hello World"}}
  ]
};

Also, once your FeatureSet is created, you won't be able to push features into it. You'll need to add the features to a dictionary, then create the FeatureSet once all your features have been loaded in.

var features = FeatureSetByName($map, myLayer, ['*'], false)
var filteredFeature = Filter(features, 'attributeZ is not null')

for ( var feat in feilteredFeature) {
  Push(
    fs_json['features'],
    {
      attributes: {
        FID: feat['FID'],
        attributeX: feat['attributeX'],
        attributeY: feat['attributeY']
      },
      geometry: Geometry(feat)
    }
  )
}

return FeatureSet(fs_json)
- Josh Carlson
Kendall County GIS

View solution in original post

2 Replies
jcarlson
MVP Esteemed Contributor

Your code doesn't follow the way that a FeatureSet is established, according to the docs.

https://developers.arcgis.com/arcade/function-reference/featureset_functions/#featureset

Try defining the geometryType, fields, and features, but don't nest them in separate layerDefinition an dfeatureset objects.

var fs_json = {
  geometryType: "esriGeometryPolygon",
  fields: [
    { name: "FID", alias: "FID", type: "esriFieldTypeOid" },
    { name: "attributeX", alias: "attributeX", type: "esriFieldTypeDate" },
    { name: "attributeY", alias: "attributeY", type: "esriFieldTypeString" }
  ],
  features: [
    {attributes: {FID: 1, attributeX: "Hello World"}}
  ]
};

Also, once your FeatureSet is created, you won't be able to push features into it. You'll need to add the features to a dictionary, then create the FeatureSet once all your features have been loaded in.

var features = FeatureSetByName($map, myLayer, ['*'], false)
var filteredFeature = Filter(features, 'attributeZ is not null')

for ( var feat in feilteredFeature) {
  Push(
    fs_json['features'],
    {
      attributes: {
        FID: feat['FID'],
        attributeX: feat['attributeX'],
        attributeY: feat['attributeY']
      },
      geometry: Geometry(feat)
    }
  )
}

return FeatureSet(fs_json)
- Josh Carlson
Kendall County GIS
HamzaMerini
New Contributor

Thank you, it helped solve my issue ! 👍😁

0 Kudos