javascript - How to get unique feature properties from geojson source in Mapbox GL JS? - Stack Overflow

I've defined a mapbox geojson source:map.addSource("places", {type: "geojson",

I've defined a mapbox geojson source:

map.addSource("places", {
    type: "geojson",
    data: ".geojson",
});

My geojson source file has this structure:

{
"type": "FeatureCollection",
"features": [{
    "type": "Feature",
    "properties": {
        "icon": "theatre"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-77.038659, 38.931567]
    }},

    {
    "type": "Feature",
    "properties": {
        "icon": "music"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-77.020945, 38.878241]
    }},
    ...]
}

I would like to get the unique names of the 'icon' properties (here: theatre and music). How can I loop over source to get these unique values? The objective here is to add a layer from these unique names for filtering purposes.

I've defined a mapbox geojson source:

map.addSource("places", {
    type: "geojson",
    data: "http://example./myfile.geojson",
});

My geojson source file has this structure:

{
"type": "FeatureCollection",
"features": [{
    "type": "Feature",
    "properties": {
        "icon": "theatre"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-77.038659, 38.931567]
    }},

    {
    "type": "Feature",
    "properties": {
        "icon": "music"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-77.020945, 38.878241]
    }},
    ...]
}

I would like to get the unique names of the 'icon' properties (here: theatre and music). How can I loop over source to get these unique values? The objective here is to add a layer from these unique names for filtering purposes.

Share Improve this question edited Aug 24, 2019 at 4:05 Nikhil 6,66110 gold badges35 silver badges72 bronze badges asked Aug 23, 2019 at 20:25 PatrickPatrick 2,7296 gold badges32 silver badges58 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I found here the answer to my question. Basically, add a layer to the source, use mapbox function queryRenderedFeatures to get the features and then get unique features with the help of dedicated function getUniqueFeatures. After I can loop over uniqueFeatures to print the elements:

var features = map.queryRenderedFeatures({layers: ['my_layer']});
var uniqueFeatures = getUniqueFeatures(features, "icon"); 

uniqueFeatures.forEach(function(feature) {
        var prop = feature.properties;
        console.log(prop.icon);
})

The getUniqueFeatures function:

function getUniqueFeatures(array, paratorProperty) {
    var existingFeatureKeys = {};
    // Because features e from tiled vector data, feature geometries may be split
    // or duplicated across tile boundaries and, as a result, features may appear
    // multiple times in query results.
    var uniqueFeatures = array.filter(function(el) {
        if (existingFeatureKeys[el.properties[paratorProperty]]) {
            return false;
        } else {
            existingFeatureKeys[el.properties[paratorProperty]] = true;
            return true;
        }
    });

    return uniqueFeatures;
}

We can use JavaScript Set object to store unique values.

const uniqueIcons = new Set();

const data = {}; // Your JSON data.

data.features.forEach((item) => {
  uniqueIcons.add(item.properties.icon);
});

Example:

const uniqueIcons = new Set();

const data = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": {
        "icon": "theatre"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-77.038659, 38.931567]
      }
    },

    {
      "type": "Feature",
      "properties": {
        "icon": "music"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-77.020945, 38.878241]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "icon": "theatre"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-77.038659, 38.931567]
      }
    }
  ]
};

data.features.forEach((item) => {
  uniqueIcons.add(item.properties.icon);
});

for(let icon of uniqueIcons) {
  console.log(icon);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104007a4611449.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信