FeatureFilter in TypeScript

934
6
Jump to solution
04-11-2023 04:53 AM
Den-GIS
Occasional Contributor

hi everyone,

I'm in the process of developing a TypeScript web application that uses a spatial filter. I don't understand why the option "filter" on the featureLayerView is not offered at all.

here is my code:

view.map.layers.forEach((layer) => {
view.whenLayerView(layer).then((layerView) => {
if (layer.type === "feature") {
const featureLayerView = layerView;
let selectedFilter = "contains";
const featureFilter = {
geometry: geometry,
spatialRelationship: selectedFilter
};
}
})
.catch(console.error);
});

For any hint, I'm very grateful!

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor
   view.map.layers.forEach((layer) => {
         if (layer.type === "feature") {
             view.whenLayerView(layer as FeatureLayer).then((layerView) => {
                 let featureLayerView = layerView;
                  let selectedFilter = "contains";
                  const featureFilter = {
                        geometry: geometry,
                        spatialRelationship: selectedFilter
                  };
                 featureLayerView.
             })
                   .catch(console.error);
             }
         });

Alternatively you can do (layerView as __esri.FeatureLayerView)

Either of those will work.

View solution in original post

0 Kudos
6 Replies
ReneRubalcava
Frequent Contributor

If you do the layer.type === "feature" before you use whenLayerView. the layer type passed should be recognized as a FeatureLayer, and the layerView will have the type FeatureLayerView.

As it is, the layer being passed to the whenLayerView is just a type "Layer" and you are getting the generic "LayerView" type back, which doesn't have the filter property on it. You can always cast it too, as __esri.FeatureLayerView, but the better option is to narrow the layer type before passing it to whenLayerView to get the proper type back.

0 Kudos
Den-GIS
Occasional Contributor

Hi ReneRubalcava
Thanks for the hint!
If that's what you meant, unfortunately didn't help. Here modified code:

function filterFeatures(results: __esri.FeatureSet) {
  let geometry = results.features[0].geometry;

   view.map.layers.forEach((layer) => {
         if (layer.type === "feature") {
             view.whenLayerView(layer).then((layerView) => {
                 let featureLayerView = layerView;
                  let selectedFilter = "contains";
                  const featureFilter = {
                        geometry: geometry,
                        spatialRelationship: selectedFilter
                  };
                 featureLayerView.
             })
                   .catch(console.error);
             }
         });

     }

0 Kudos
ReneRubalcava
Frequent Contributor

That's weird, I thought that would narrow it down. You can cast "layer as FeatureLayer" when you pass it to whenLayerView and that would return a FeatureLayerView then.

0 Kudos
Den-GIS
Occasional Contributor

I'm not sure how you mean that. I tried some code changes. Unfortunately, none of them work. Is there any such example with spatial filter of features classes in typescript?
What I try to do is filter the lines and  stations only within a city. I have one polygon feature class (cities), four polyline feature classes (lines) and six point features classes (stations) My goal is for users to select the city via drop down and only within that city should the Lines as well as  stations are displayed.

I first do a SQL Where query (e.g. "City = 'Berlin'") to get the geometry. I then pass the geometry to the FeatureFilter parameters:
const featureFilter = {
     geometry: geometry,
     spatialRelationship: 'contains'
};
At this point then problems begin that as soon as the forEach(); method is applied and whenLayerViewer is executed to get the LayerView of all FeatureLayers, then the FeatureFilter option is not offered. Although I can run for each individual layer whenLayerView is just not so optimal.

Unfortunately, in this case it is necessary to use the spatial filter, because the attribute data of lines and stations is not complete, so an SQL query is not possible. Many would then not be taken into account by the SQL filter. On the other hand, the city data is very good and complete, and a SQL query can be run.

Most of all I would like to run a spatial query against Map Image Service as the performance and rendering is much better than Feature Classes. But I haven't seen the spatial filter option anywhere in ArcGIS API JS. Only SQL query, i.e. definition expression.

0 Kudos
ReneRubalcava
Frequent Contributor
   view.map.layers.forEach((layer) => {
         if (layer.type === "feature") {
             view.whenLayerView(layer as FeatureLayer).then((layerView) => {
                 let featureLayerView = layerView;
                  let selectedFilter = "contains";
                  const featureFilter = {
                        geometry: geometry,
                        spatialRelationship: selectedFilter
                  };
                 featureLayerView.
             })
                   .catch(console.error);
             }
         });

Alternatively you can do (layerView as __esri.FeatureLayerView)

Either of those will work.

0 Kudos
Den-GIS
Occasional Contributor

That's it. The magic word was "as" which I was missing. I mistakenly wrote view.whenLayerView(layer = new FeatureLayer).then((layerView) => ....

Thank you ReneRubalcava

0 Kudos