react-leaflet icon indicating copy to clipboard operation
react-leaflet copied to clipboard

GeoJSON filtering doesn't filter

Open smcalilly opened this issue 5 years ago • 2 comments

Bug report

Before opening an issue, make sure to read the contributing guide and understand this is a bug tracker, not a support platform.

Please make sure to check the following boxes before submitting an issue.
Issues opened without using this template will be closed unless they have a good reason not to follow this template.

  • [x] All peer dependencies are installed: React, ReactDOM and Leaflet.
  • [x] Using a supported version of React and ReactDOM (v17.0.0 minimum).
  • [x] Using the supported version of Leaflet (v1.7.1 minimum) and its corresponding CSS file is loaded.
  • [x] Using the latest v3 version of React-Leaflet.
  • [x] The issue has not already been reported.
  • [x] Make sure you have followed the quick start guide for Leaflet.
  • [x] Make sure you have fully read the documentation and that you understand the limitations.

Expected behavior

I expect the filter prop in the GeoJSON component to filter geojson, based on the returned boolean from the callback passed into the prop.

Actual behavior

The callback fires, returning true for the desired properties. It doesn't filter the geojson.

Steps to reproduce

Please provide the simplest example possible to reproduce the issue, based on this CodePen.

In that same codepen, copy/paste this:

const { useState } = React
const { MapContainer, TileLayer, Marker, Popup, GeoJSON } = ReactLeaflet

const data = {
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "properties": {
          "id": 1,
          "name": "Coors Field",
          "amenity": "Baseball Stadium",
          "popupContent": "This is where the Rockies play!"
        },
        "geometry": {
          "type": "Point",
          "coordinates": [-104.99404, 39.75621]
        }
      },
      {
        "type": "Feature",
        "properties": {
          "id": 2,
          "name": "Denver International Airport",
          "amenity": "Airport",
          "popupContent": "This is where you board an airplane."
        },
        "geometry": {
          "type": "Point",
          "coordinates": [-104.6737, 39.8561]
        }
      }
  ]
}

function SimpleExample(props) {
  const [selection, setSelection] = useState(null)
  
  function filterGeo(feature) {
    console.log('feature', feature.properties.id)
    console.log('selection', selection)
  
    if (selection) {
      if (feature.properties.id === selection) {
        return true
      } else {
        return false
      }
    } else {
      // return true for all features if selection is false
      return true
    }
    
  }
  
  return (
    <>
       <button onClick={() => setSelection(1)}>Coors Field</button>
       <MapContainer center={[39.75621, -104.99404]} zoom={10}>
          <TileLayer
            url="https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}{r}.png"
            attribution='&copy; <a href="https://www.openstreetmap.org/copyright" target="_parent">OpenStreetMap</a> contributors &copy; <a href="https://carto.com/attributions" target="_parent">CARTO</a>'
                />
            <GeoJSON
              key='denver'
              data={data}
              filter={filterGeo} />
        </MapContainer>
    </>
  )
}

ReactDOM.render(<SimpleExample />, document.getElementById('container'))

Click the Coors Field button. (In my actual code, I'm using an html select element via the react-select library, but I just added a simple button for the purpose of a basic demo.) Clicking that button should set the filter. I would expect the filter mechanism to show only feature.properties.id === selection, but it doesn't filter out anything. In my actual code, with a select component, the callback is fired when I select, but the filtering doesn't happen.

If you set the initial state for selection/setSelection to equal 2 (aka Denver Airport), the map will load with the correct filtering (by showing the Denver Airport property). If you click the Coors Field button, I would expect the filter to show only the Coors Field property, but this doesn't happen.

smcalilly avatar Oct 29 '21 19:10 smcalilly

The filter prop of GeoJSON is not mutable, if that's what you're expecting.

PaulLeCam avatar Jan 08 '22 09:01 PaulLeCam

The docs say:

The properties passed to the components are used to create the relevant Leaflet instance when the component is rendered the first time and should be treated as immutable by default.

So I guess I interpreted that as meaning that the actual geojson object was immutable. I didn't think it applied to boolean flags that could show/not show the data.

If anybody comes to this issue in the future, we ended up filtering our geojson in the react state, and forcing a re-render with the help of this stackoverflow: https://stackoverflow.com/questions/44155385/rendering-geojson-with-react-leaflet

smcalilly avatar Jan 18 '22 21:01 smcalilly