如何合并一组 geojson 项目的坐标值?
How does one merge coordinate values of an array of geojson items?
您好,我正在处理一个大型 geojson 数据集,我正在尝试查看是否可以根据共享相同“User_ID”的条目合并每个条目的坐标值。
我的数据集如下所示:
{
"geometry":{
"type":"Point",
"coordinates":[
-3.231658,
51.687026
]
},
"type":"Feature",
"properties":{
"User_ID":1002848324
}
},
{
"geometry":{
"type":"Point",
"coordinates":[
-3.231659,
51.687016
]
},
"type":"Feature",
"properties":{
"User_ID":1002848324
}
}
我尝试使用 mwarren 的回答 url 中显示的方法合并条目:“
然而,这会带来一个小问题,即当我尝试 运行 测试时,“attr”被视为“意外标识符”。
目前我对代码的测试如下:
features.map(function(feature){
var matchedArray = features2.filter(function(feature2){
return feature2.User_ID === feature.User_ID;
});
if(matchedArray && matchedArray[0]){
for(var attr in matchedArray[0].properties){
feature.properties[attr] = matchedArray[0].properties[attr];
}
}
});
所需的结果应如下所示:
{
"geometry":{
"type":"Point",
"coordinates":[
-3.231658,
51.687026
], [
-3.231659,
51.687016
]
},
"type":"Feature",
"properties":{
"User_ID":1002848324
}
任何帮助将不胜感激。
您将 var 拼写为“war”,需要 'var'。
如果这不是错误,请说明什么是 'features2' 数组。
从上面的评论...
"The OP needs to group/collect the data not only by same User_ID
values but by both same type
AND same User_ID
values"
...甚至可能是相同的 geometry.type
。仅基于 User_ID
的 grouping/collecting 不够明确,因为具有相同 User_ID
值的项目可能具有不同的 type
值,甚至可能在 geometry.type
值上有所不同。
一个基于 reduce
的任务,它使用一个 collector
对象,该对象以一个对象为 lookup
和一个 result
数组作为聚合的最终数据,确实解决了 OP 的任务在一个迭代步骤内...
function collectSameGeoCategoryItems(collector, item) {
const { lookup, result } = collector;
const { properties: { User_ID }, type } = item;
const groupKey = `${ type }_${ User_ID }`;
let groupItem = lookup[groupKey];
if (groupItem) {
// push coordinates copy into existing array of coordinate arrays.
groupItem.geometry.coordinates.push([...item.geometry.coordinates]);
} else {
// create full copy of geo item in order
// to not mutate the original reference.
groupItem = lookup[groupKey] = {
geometry: {
type: item.geometry.type,
coordinates: [ [...item.geometry.coordinates] ],
},
type,
properties: { User_ID },
};
result.push(groupItem);
}
return collector;
}
const sampleData = [{
geometry: {
type: "Point",
coordinates: [
-3.231658,
51.687026,
],
},
type: "Feature",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231659,
51.687016,
],
},
type: "Feature",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231658,
51.687026,
],
},
type: "Foo",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231659,
51.687016,
],
},
type: "Bar",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231658,
51.687026,
],
},
type: "Foo",
properties: {
User_ID: 1000000000,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231659,
51.687016,
],
},
type: "Bar",
properties: {
User_ID: 1002848324,
},
}];
console.log(
'aggregated `result` ...',
sampleData.reduce(collectSameGeoCategoryItems, { lookup: {}, result: [] }).result
);
console.log('unmutated sample data ... ', { sampleData });
console.log(
'aggregated `lookup`, not needed, just for demonstration purpose ...',
sampleData.reduce(collectSameGeoCategoryItems, { lookup: {}, result: [] }).lookup
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
您好,我正在处理一个大型 geojson 数据集,我正在尝试查看是否可以根据共享相同“User_ID”的条目合并每个条目的坐标值。
我的数据集如下所示:
{
"geometry":{
"type":"Point",
"coordinates":[
-3.231658,
51.687026
]
},
"type":"Feature",
"properties":{
"User_ID":1002848324
}
},
{
"geometry":{
"type":"Point",
"coordinates":[
-3.231659,
51.687016
]
},
"type":"Feature",
"properties":{
"User_ID":1002848324
}
}
我尝试使用 mwarren 的回答 url 中显示的方法合并条目:“ 然而,这会带来一个小问题,即当我尝试 运行 测试时,“attr”被视为“意外标识符”。 目前我对代码的测试如下: 所需的结果应如下所示: 任何帮助将不胜感激。features.map(function(feature){
var matchedArray = features2.filter(function(feature2){
return feature2.User_ID === feature.User_ID;
});
if(matchedArray && matchedArray[0]){
for(var attr in matchedArray[0].properties){
feature.properties[attr] = matchedArray[0].properties[attr];
}
}
});
{
"geometry":{
"type":"Point",
"coordinates":[
-3.231658,
51.687026
], [
-3.231659,
51.687016
]
},
"type":"Feature",
"properties":{
"User_ID":1002848324
}
您将 var 拼写为“war”,需要 'var'。 如果这不是错误,请说明什么是 'features2' 数组。
从上面的评论...
"The OP needs to group/collect the data not only by same
User_ID
values but by both sametype
AND sameUser_ID
values"
...甚至可能是相同的 geometry.type
。仅基于 User_ID
的 grouping/collecting 不够明确,因为具有相同 User_ID
值的项目可能具有不同的 type
值,甚至可能在 geometry.type
值上有所不同。
一个基于 reduce
的任务,它使用一个 collector
对象,该对象以一个对象为 lookup
和一个 result
数组作为聚合的最终数据,确实解决了 OP 的任务在一个迭代步骤内...
function collectSameGeoCategoryItems(collector, item) {
const { lookup, result } = collector;
const { properties: { User_ID }, type } = item;
const groupKey = `${ type }_${ User_ID }`;
let groupItem = lookup[groupKey];
if (groupItem) {
// push coordinates copy into existing array of coordinate arrays.
groupItem.geometry.coordinates.push([...item.geometry.coordinates]);
} else {
// create full copy of geo item in order
// to not mutate the original reference.
groupItem = lookup[groupKey] = {
geometry: {
type: item.geometry.type,
coordinates: [ [...item.geometry.coordinates] ],
},
type,
properties: { User_ID },
};
result.push(groupItem);
}
return collector;
}
const sampleData = [{
geometry: {
type: "Point",
coordinates: [
-3.231658,
51.687026,
],
},
type: "Feature",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231659,
51.687016,
],
},
type: "Feature",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231658,
51.687026,
],
},
type: "Foo",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231659,
51.687016,
],
},
type: "Bar",
properties: {
User_ID: 1002848324,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231658,
51.687026,
],
},
type: "Foo",
properties: {
User_ID: 1000000000,
},
}, {
geometry: {
type: "Point",
coordinates: [
-3.231659,
51.687016,
],
},
type: "Bar",
properties: {
User_ID: 1002848324,
},
}];
console.log(
'aggregated `result` ...',
sampleData.reduce(collectSameGeoCategoryItems, { lookup: {}, result: [] }).result
);
console.log('unmutated sample data ... ', { sampleData });
console.log(
'aggregated `lookup`, not needed, just for demonstration purpose ...',
sampleData.reduce(collectSameGeoCategoryItems, { lookup: {}, result: [] }).lookup
);
.as-console-wrapper { min-height: 100%!important; top: 0; }