geojson的属性功能按属性值在javascript中

发布于 2025-02-11 06:07:49 字数 3340 浏览 3 评论 0原文

我有一个Geojson功能列表,每个功能都具有其属性中的资产ID。我想操纵Geojson,因此我只剩下每个资产ID的单个功能,每个功能的属性都添加到了功能属性中。

例如,以下Geojson具有4个功能。其中2个具有100和2的资产ID,其中2个资产ID为200: -

{
   "type":"FeatureCollection",
   "title":"map_features",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE29302",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "DESCRIPTION":"Bin is full"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE56843",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "NOTES":"Bin needs emptying"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE77112",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin cleaned"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE04393",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin full"
         }
      }
   ]
}

我希望仅保留资产ID 100和200的两个功能,并将属性从重复的功能中分组在一起: -

{
    "type": "FeatureCollection",
    "title": "map_features",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    525408,
                    162788
                ]
            },
            "properties": [
                {
                    "CASENO": "CASE29302",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "100",
                    "DESCRIPTION": "Bin is full"
                },
                {
                    "CASENO": "CASE56843",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "100",
                    "NOTES": "Bin needs emptying"
                }
            ]
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    525408,
                    162788
                ]
            },
            "properties": [
                {
                    "CASENO": "CASE56843",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "100",
                    "NOTES": "Bin needs emptying"
                },
                {
                    "CASENO": "CASE04393",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "200",
                    "NOTES": "Bin full"
                }
            ]
        }
    ]
}

我不确定是否有JavaScript / jQuery方法可能会有所帮助?我需要使用循环吗?

任何指针都将不胜感激。

I have a list of geojson features that each have an asset ID in their properties. I want to manipulate the geojson so I am left with only a single feature per asset ID, with the properties from each feature found added to the feature properties.

As an example, the following geojson has 4 features; 2 of them have an asset ID of 100 and 2 of them have an asset ID of 200:-

{
   "type":"FeatureCollection",
   "title":"map_features",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE29302",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "DESCRIPTION":"Bin is full"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE56843",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "NOTES":"Bin needs emptying"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE77112",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin cleaned"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE04393",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin full"
         }
      }
   ]
}

I am looking to be left with just two features for asset ID 100 and 200, and have the properties grouped together from the duplicate features:-

{
    "type": "FeatureCollection",
    "title": "map_features",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    525408,
                    162788
                ]
            },
            "properties": [
                {
                    "CASENO": "CASE29302",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "100",
                    "DESCRIPTION": "Bin is full"
                },
                {
                    "CASENO": "CASE56843",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "100",
                    "NOTES": "Bin needs emptying"
                }
            ]
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    525408,
                    162788
                ]
            },
            "properties": [
                {
                    "CASENO": "CASE56843",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "100",
                    "NOTES": "Bin needs emptying"
                },
                {
                    "CASENO": "CASE04393",
                    "CASE_TYPE": "litterBin",
                    "ASSETID": "200",
                    "NOTES": "Bin full"
                }
            ]
        }
    ]
}

I am not sure if there is a javascript / jquery method that might help with this? Would I need to use a for loop?

Any pointers would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2025-02-18 06:07:49

如果您有信心重复的功能具有相同的几何形状,则可以使用以下方法:

  1. 通过assetId将功能分组,您可以使用 map 使用降低在每个键的地方执行此操作是唯一的AssetId,该值是一个填充功能的数组。
  2. 对于MAP中的每个项目,请使用几何>几何>的数组中的第一个功能,然后使用properties用数组替换功能数组仅属性
  3. 创建一个新的featurecollection,并在步骤2中解决了新功能。

请参阅下面的工作代码中的注释:

// group features by ASSETID
const assets = Array.from(
  fc.features.reduce((a, c) => {
    const id = c.properties.ASSETID;
    if (!a.has(id)) a.set(id, []); // <-- initialise each entry with an empty array
    a.get(id).push(c);             // <-- add the feature to the array for that ASSETID
    return a;
  }, new Map())                    // <-- the accumulator is a Map
);

// create new features
const newFeatures = assets.reduce((a, c) => {
  a.push({
    "type": "Feature",
    "geometry": c[1][0].geometry,  // <-- take geometry from first array entry
    "properties": c[1].map(f => f.properties) // <-- just retain the properties of the feature
  });
  return a;
}, []);

// create new feature collection
const newFc = {
   "type":"FeatureCollection",
   "title":"map_features",
   "features": newFeatures
}

// output
console.log(newFc);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script>
const fc = {
   "type":"FeatureCollection",
   "title":"map_features",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE29302",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "DESCRIPTION":"Bin is full"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE56843",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "NOTES":"Bin needs emptying"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE77112",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin cleaned"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE04393",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin full"
         }
      }
   ]
}
</script>

If you're confident that the duplicate features have the same geometry you can use this approach:

  1. Group the features by ASSETID you can use a Map with reduce to do this where each key is a unique ASSETID and the value is an array that fills up with features.
  2. For each item in the Map, use the first feature in the array for the geometry and then for properties replace the array of features with an array of just the properties.
  3. Create a new featureCollection with the new features resolved in step 2.

See the comments in the working code below:

// group features by ASSETID
const assets = Array.from(
  fc.features.reduce((a, c) => {
    const id = c.properties.ASSETID;
    if (!a.has(id)) a.set(id, []); // <-- initialise each entry with an empty array
    a.get(id).push(c);             // <-- add the feature to the array for that ASSETID
    return a;
  }, new Map())                    // <-- the accumulator is a Map
);

// create new features
const newFeatures = assets.reduce((a, c) => {
  a.push({
    "type": "Feature",
    "geometry": c[1][0].geometry,  // <-- take geometry from first array entry
    "properties": c[1].map(f => f.properties) // <-- just retain the properties of the feature
  });
  return a;
}, []);

// create new feature collection
const newFc = {
   "type":"FeatureCollection",
   "title":"map_features",
   "features": newFeatures
}

// output
console.log(newFc);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script>
const fc = {
   "type":"FeatureCollection",
   "title":"map_features",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE29302",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "DESCRIPTION":"Bin is full"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525408,
               162788
            ]
         },
         "properties":{
            "CASENO":"CASE56843",
            "CASE_TYPE":"litterBin",
            "ASSETID":"100",
            "NOTES":"Bin needs emptying"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE77112",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin cleaned"
         }
      },
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               525177,
               164509
            ]
         },
         "properties":{
            "CASENO":"CASE04393",
            "CASE_TYPE":"litterBin",
            "ASSETID":"200",
            "NOTES":"Bin full"
         }
      }
   ]
}
</script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文