3

So I have a GeoJSON that I want to pull/filer/select all kitespots with a wind direction that includes "S" in Leaflet. (..and yes, am a beginner at coding..)

var kitespots = {
  "type": "FeatureCollection",
  "features":[    
    {
     "type": "Feature",
     "properties": {"name": "STUBBSAND", "windDirection": "S/SW"},
     "geometry": {
       "type": "Point",
       "coordinates": [18.959655761718746, 63.220636696106915]}
    },
    {
     "type": "Feature",
     "properties": {"name": "GULLVIK", "windDirection": "SW/S/SE"},
     "geometry": {
       "type": "Point",
       "coordinates": [ 18.82786273956299, 63.214932082292805]}
    },
    {
     "type": "Feature",
     "properties": {"name": "VANNVIKEN", "windDirection": "SE/S"},
     "geometry": {
       "type": "Point",
       "coordinates": [18.815631866455078, 63.20794965080512]}
    },
    {
     "type": "Feature",
     "properties": {"name": "STORSANDEN", "windDirection": "N/NW"},
     "geometry": {
       "type": "Point",
       "coordinates": [18.528785705566406, 62.99013053076868]}
    }
  ]
}

So far I have tried;

// - Select all kitespots facing south - //
var southSpotCluster = new L.markerClusterGroup();
var windDirectionS = L.geoJson(kitespots,{windDirection: "S"});
southSpotCluster.addLayer(windDirectionS);

// - ...and.. - //

var southSpotCluster = L.geoJson(kitespots)
var windDirection = ["S"]
for(var windDirection in southSpotCluster){
    windDirection.push(southSpotCluster["S"]);
}
southSpotCluster.addLayer(windDirection);


var overLays = {
    'All Kitespots': allSpotsCluster,
    'Kitespots S': southSpotCluster
};

L.control.layers(baseLayers, overLays).addTo(map);
1
  • @user:29431 thank you for the edit! First post, not sure yet how things work. – Liah Cheston Jan 10 at 17:28
3

This is probably more of a question for stackoverflow directly, however if you are able to use ES6, you could use something like this:

const filteredFeatures = kitespots.features.filter(item => { 
    return item.properties.windDirection.split("/").includes("S");
});

The filter function will return merely those items which include S. Splitting of the string beforehand will make sure it won't match something like SW in case S alone doesn't exist.

1
  • I think theres should be a ES6 branch for Esri Leaflet (am working in esri-leaflet). I'll check it tomorrow, thank you! – Liah Cheston Jan 11 at 1:10
2

There is a practical library you can use to filter Geojson data, https://github.com/digidem/feature-filter-geojson.

With this library you can create mapbox-style-definition like filters.

import createFilter from "feature-filter-geojson";

function geojson_filter(geojson, filter_exp) {
  let features = geojson.features;
  geojson.features = [];
  let geojson_clon = Object.assign({}, geojson);
  geojson.features = features;

  let filter = createFilter(filter_exp);
  geojson_clon .features = filter(geojson.features, filter);    
  return geojson_clon ;
}

var ffilter = ["all",["==", "windDirection", "S"],];

var filtered_geojson = geojson_filter(kitespots , ffilter);
1
  • Thank you, I'll try implementing this with leaflet tomorrow! – Liah Cheston Jan 11 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.