Geographic Information Systems Asked by Liah Cheston on May 29, 2021
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);
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.
Answered by Timothy Dalton on May 29, 2021
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);
Answered by Deniz on May 29, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP