Filter a GEOJSON layer by one property

234
2
a month ago
JaninaGro5
New Contributor

Hello! 

I am working on a project, where I will  feature  lots interesting geological locations on a globe, using Arcgis API for Javascript. These locations will be featured in GEOJSON layer and people should be able to click on the symbols and a pop-up window with pictures and texts will appear. This works fine so far, but I would like to apply a filter. I assigned different categories to the selected locations, like "Volcanoes", "Mountains", "Reefs" and much more. I would like to add a filter, so that users can select for  example "Volcanoes" and just the volcanoes will be visible on the globe. 

I looked at tutorials (e.g. https://developers.arcgis.com/javascript/latest/sample-code/featurefilter-attributes/), but I am not able to apply it on my globe. I am beginner and probably I am doing something wrong. Is the sample code in link applicable for GEOJSON  layers as well?  Or maybe someone has another idea or knows about other tutorials or sample codes, that could help in this case. 

Below is a small excerpt of the GEOJSON file. There are more properties, but I would like to filter them only by the "category". The geometry type stays the same throughout the file.

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "Mount Fuji",
        "category":"Volcanoes",
        "type": "...............",
        "location": "................",
        "description": "......"
}, "geometry": { "type": "Point", "coordinates": [ 138.7275, 35.360556 ] } },
..
..
..

Thank you very much!

 

0 Kudos
2 Replies
BlakeTerhune
MVP Regular Contributor

Using the ArcGIS API for JavaScript, it doesn't look like there's a filter method like the sample you linked to, but you could query the GeoJSON layer and display the query results. Alternatively, you could use plain JS to filter the GeoJSON object.

 

let filteredFeatures = myFeatureCollection.features.filter(function (feature) {
  return feature.properties.category == "Volcanoes";
});

 

 

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

The feature filters and effects can be applied on the GeoJSONLayerView as you are describing in your question.

If you are not familiar with layer views and filters, please take a look at this guide doc to understand how they work. 

I modified the sample you referenced in your question to filter features from GeoJSONLayerView. Take a look at this test app

Hope this helps,

-Undral