问题
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]
}
}
]
}
回答1:
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);
回答2:
You treat collection as an array, however, it is an Object.
function eliminateDuplicates(collection) {
var i;
var len = collection.features.length;
var out = [];
var obj = {};
/* Your deduplication code over collection.features here */
}
来源:https://stackoverflow.com/questions/30388155/remove-duplicate-values-from-geojson-collection