Get feature count of an empty FeatureLayer and applyEdits

1212
4
Jump to solution
01-19-2022 05:00 AM
arahman_mdmajid
Occasional Contributor

How can I add the queryFeatureCount function to a feature layer whose source is not set and will later be set? The next time this function is called again, it will have some features to delete. 

The feature layer is initialized as

polygonsFeatureLayer = new FeatureLayer({
title: "test",
objectIdField: "OBJECTID",
source: [],
renderer: {
type: "simple", // autocasts as new SimpleFillSymbol()
symbol: {
type: "simple-fill", // autocasts as new SimpleFillSymbol()
color: [255, 0, 0, 0.5],
outline: { // autocasts as new SimpleLineSymbol()
color: [255, 255, 0],
width: 3
}
}
} as any
});


In another function, I am trying to add new features to the same layer, but I need to delete the old features if there are any in the layer, so I am running the following code to do so

this.polygonsFeatureLayer.queryFeatures().then((results) => {
console.log(results);
const deleteEdits = {
deleteFeatures: results.features
};
// apply edits to the layer
this.polygonsFeatureLayer.applyEdits(deleteEdits).then((results) => {
console.log(results);
// if edits were removed
if (results.deleteFeatureResults.length > 0) {
console.log(
results.deleteFeatureResults.length,
"features have been removed"
);
}
});
});


Then when the new features are received and collected. I am running the addFeatures to add new items to the existing feature layer.

const addEdits = {
addFeatures: results.features
};
this.polygonsFeatureLayer.applyEdits(addEdits).then((results) => {
console.log(results);
// if edits were removed
if (results.addFeatureResults.length > 0) {
console.log(
results.addFeatureResults.length,
"features have been added"
);
}
});

map.add(this.polygonsFeatureLayer);


But I am getting the following errors,

[esri.views.2d.layers.FeatureLayerView2D] #resolve() Failed to resolve layer view (layer title: 'test', id: '17e720aea79-layer-28') o {name: 'featurelayerview:table-not-supported', details: {…}, message: "table feature layer can't be displayed"}

Logger.js:5 [esri.views.LayerViewManager] Failed to create layerview for layer title:'test', id:'17e720aea79-layer-28' of type 'feature'. {layer: a, error: o}

ERROR Error: Uncaught (in promise): [featurelayerview:table-not-supported]: table feature layer can't be displayed

What am I missing here?  

Abdur Rahman
GIS Developer
0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

The error indicates that FeatureLayer you creating is a non-spatial FeatureLayer. Meaning that graphics you are adding to the FeatureLayer does not have geometry, just attributes. Is this what you are adding? If you are then you cannot add non-spatial FeatureLayer to the map. Creating a FeatureLayer doc explains how non-spatial FeatureLayers can be used. See under Read more. 

If you are creating spatial FeatureLayer then several things to check. 

1. You can only add graphics with same geometry types. 

2. Are you graphic geometries valid? 

3. You should set the FeatureLayer.geometryType in the layer's constructor.  The codepen app sets the params required to initialize FeatureLayer. If you don't then the API will try to infer required parameters from the graphics you pass in and will fail if it cannot infer the params.

View solution in original post

4 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

I modified an existing sample to remove all features before new features are added. Essentially, you have to wait until existing features are deleted before new ones are added. 

 

Test app: https://codepen.io/U_B_U/pen/PoJvbab?editors=1000

Take a look at the addFeatures function.

Hope this helps,

arahman_mdmajid
Occasional Contributor

I was able to get the feature count on an empty featureLayer as per your code. And I was also able to add the feature to the feature layer using addFeatures.

But when I add the layer to the map, it still gives me the following error.

[esri.views.2d.layers.FeatureLayerView2D] #resolve() Failed to resolve layer view (layer title: 'test', id: '17e720aea79-layer-28') o {name: 'featurelayerview:table-not-supported', details: {…}, message: "table feature layer can't be displayed"}

Logger.js:5 [esri.views.LayerViewManager] Failed to create layerview for layer title:'test', id:'17e720aea79-layer-28' of type 'feature'. {layer: a, error: o}

ERROR Error: Uncaught (in promise): [featurelayerview:table-not-supported]: table feature layer can't be displayed

I am adding doing a console.log of the features added to the addFeatures method, and it shows like this.

features snap.PNG

 

The feature after shows as

feature added.PNG

 

 But when the same featureLayer is added to the map afterward, it gives the errors

Thanks for the help.

Abdur Rahman
GIS Developer
0 Kudos
UndralBatsukh
Esri Regular Contributor

The error indicates that FeatureLayer you creating is a non-spatial FeatureLayer. Meaning that graphics you are adding to the FeatureLayer does not have geometry, just attributes. Is this what you are adding? If you are then you cannot add non-spatial FeatureLayer to the map. Creating a FeatureLayer doc explains how non-spatial FeatureLayers can be used. See under Read more. 

If you are creating spatial FeatureLayer then several things to check. 

1. You can only add graphics with same geometry types. 

2. Are you graphic geometries valid? 

3. You should set the FeatureLayer.geometryType in the layer's constructor.  The codepen app sets the params required to initialize FeatureLayer. If you don't then the API will try to infer required parameters from the graphics you pass in and will fail if it cannot infer the params.

arahman_mdmajid
Occasional Contributor

Yes, thank you for mentioning the third point.

3. You should set the FeatureLayer.geometryType in the layer's constructor.  The codepen app sets the params required to initialize FeatureLayer. If you don't then the API will try to infer required parameters from the graphics you pass in and will fail if it cannot infer the params.

This point helped me and solved the issue, so now my initialization of the featureLayer looks like this:

  polygonsFeatureLayer = new FeatureLayer({
    title: "test",
    objectIdField: "OBJECTID",
    geometryType: "polygon",
    source: [],
    renderer: {
      type: "simple",  // autocasts as new SimpleFillSymbol()
      symbol: {
        type: "simple-fill",  // autocasts as new SimpleFillSymbol()
        color: [255, 0, 0, 0.5],
        outline: {  // autocasts as new SimpleLineSymbol()
          color: [255, 255, 0],
          width: 3
        }
      }
    } as any
  });

 

As for the previous two points

1. You can only add graphics with same geometry types. 

I made sure that it was only storing polygon type features.

2. Are you graphic geometries valid? 

I was convinced that the features were valid since I added and showed them using a GraphicsLayer.

Thanks

Abdur Rahman
GIS Developer
0 Kudos