我想知道从大型GeoJSON集合中删除重复值(坐标)的最简单的javascript方法(大约100k行)。删除重复值后,我想将更新的集合记录到控制台或在网页上显示结果。我的尝试样本如下,但我在控制台中获得的是一个空数组。
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]
}
}
]
}
答案 0 :(得分:6)
这假定复制品是指具有相同坐标的条目。
// 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);
答案 1 :(得分:0)
您将集合视为数组,但它是一个对象。
function eliminateDuplicates(collection) {
var i;
var len = collection.features.length;
var out = [];
var obj = {};
/* Your deduplication code over collection.features here */
}