【问题标题】:Remove duplicate values from GeoJSON Collection从 GeoJSON 集合中删除重复值
【发布时间】:2015-05-22 12:23:42
【问题描述】:

我想知道从大型 GeoJSON 集合(大约 100k 行)中删除重复值(坐标)的最简单的 javascript 方法。删除重复值后,我想将更新的集合记录到控制台或在网页上显示结果。下面是我的尝试示例,但是我在控制台中得到的只是一个空数组。

window.onload = init;

function init() {  

 function eliminateDuplicates(arr) {
  var i;
  var len = arr.length;
  var out = [];
  var obj = {};

  for (i = 0; i < len; i++) {
    obj[arr[i]]=0;
  }
  for (i in obj) {
    out.push(i);
  }
  return out;
}     

var newCollection = eliminateDuplicates(arr);
console.log(newCollection);

}

var arr =
{
      "type": "FeatureCollection",
         "features": [
            {
               "type": "Feature","properties": {"@id": "123",
               "name": "test1",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.994720, 40.686902]
               }
            },
            {
               "type": "Feature","properties": {"@id": "1234",
               "name": "test2",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.994720, 40.686902]
               }
            },
            {
               "type": "Feature","properties": {"@id": "1945",
               "name": "test3",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.989205, 40.686675]
               }
            },
            {
               "type": "Feature","properties": {"@id": "1946",
               "name": "test3",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.994655, 40.687391]               
               }
            },
            {
               "type": "Feature","properties": {"@id": "1947",
               "name": "test4",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.985557, 40.687683]               
               }
            }
  ]         

}

【问题讨论】:

  • 您是否要消除具有相同坐标的项目?

标签: javascript duplicate-removal geojson


【解决方案1】:

这里假设重复是指具有相同坐标的条目。

// for simplicity's sake I got rid of the outer data layer
// and made arr a simple array. With the original data you
// could do arr.features.forEach instead of the arr.forEach below.
var arr = [
            {
               "type": "Feature","properties": {"@id": "123",
               "name": "test1",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.994720, 40.686902]
               }
            },
            {
               "type": "Feature","properties": {"@id": "1234",
               "name": "test2",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.994720, 40.686902]
               }
            },
            {
               "type": "Feature","properties": {"@id": "1945",
               "name": "test3",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.989205, 40.686675]
               }
            },
            {
               "type": "Feature","properties": {"@id": "1946",
               "name": "test3",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.994655, 40.687391]               
               }
            },
            {
               "type": "Feature","properties": {"@id": "1947",
               "name": "test4",
               "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."},

               "geometry": {
                  "type": "Point","coordinates": [-73.985557, 40.687683]               
               }
            }
  ];

// generates a key from the item's coordinates.
function keyFor(item) {
  return item.geometry.coordinates[0] + ':' + item.geometry.coordinates[1];
}

// index the entries by their coordinates such that
// indexed['lat:lng'] == the entry.
var indexed = {};
arr.forEach(function(item) {
  // a duplicate key will replace the existing entry
  indexed[keyFor(item)] = item;
});

// turn the results back into an array of just values.
var result = Object.keys(indexed).map(function(k) {
  return indexed[k];
});

// emit the results.
console.log(result);

【讨论】:

  • 你好。感谢您提供此代码。我正在使用 geoJson 文件。我有 'var promise = $.getJSON("examen.json"); promise.then(function(data) {' 来获取我所有的 geoJson 文件数据。我尝试使用 'promise.features.forEach' 它不起作用..你能确定如何循环遍历 geoJson 文件属性吗??我有我在其中放置所有数据的 html 块,所以我的目标是将去重结果放入我的 html...
  • @blogob 你不会重复承诺。你会遍历它解析的数据。
【解决方案2】:

您将集合视为一个数组,但它是一个对象。

function eliminateDuplicates(collection) {
  var i;
  var len = collection.features.length;
  var out = [];
  var obj = {};

  /* Your deduplication code over collection.features here */
}  

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 2019-02-17
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多