从GeoJSON Collection中删除重复的值 [英] Remove duplicate values from GeoJSON Collection

查看:303
本文介绍了从GeoJSON Collection中删除重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to know the simplest javascript method for removing duplicate values (coordinates) from a large GeoJSON collection (approx 100k lines). After removing the duplicate values I would like to log the updated collection to the console or display the result on a webpage. A sample of my attempt is below, however all I am getting in the console is an empty array.

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]               
               }
            }
  ]         

}


推荐答案

这个假设是通过重复来表示具有相同坐标的条目。

This assumes that by duplicate you mean an entry with the same coordinates.

// 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 Collection中删除重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆