合并json并使用重复项构建csv

时间:2015-04-18 11:38:40

标签: javascript jquery json grep geojson

以下是我所拥有的一些geojson的例子:

var json = {
  features: [
    {
      properties: {
        osm_key: "amenity",
        extent: [
          151.214672,
          -33.8562966,
          151.2158814,
          -33.8574149
        ],
        street: "Lower Concourse",
        name: "Sydney Opera House",
        state: "New South Wales",
        osm_id: 4960757,
        osm_type: "W",
        postcode: "2061",
        osm_value: "theatre",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2152582491399,
          -33.85685575
        ]
      }
    },
    {
      properties: {
        osm_key: "tourism",
        extent: [
          151.214672,
          -33.8562966,
          151.2158814,
          -33.8574149
        ],
        street: "Lower Concourse",
        name: "Sydney Opera House",
        state: "New South Wales",
        osm_id: 4960757,
        osm_type: "W",
        postcode: "2061",
        osm_value: "attraction",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2152582491399,
          -33.85685575
        ]
      }
    },
    {
      properties: {
        osm_key: "highway",
        extent: [
          -95.6987584,
          29.9960185,
          -95.6984449,
          29.9907477
        ],
        name: "Opera House Row Drive",
        state: "Texas",
        osm_id: 261793234,
        osm_type: "W",
        postcode: "77433",
        osm_value: "residential",
        city: "Cypress",
        country: "United States of America"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          -95.698749,
          29.993634
        ]
      }
    },
    {
      properties: {
        osm_key: "tourism",
        street: "葆台路",
        name: "Sydney Opera House",
        state: "Beijing",
        osm_id: 3184358225,
        osm_type: "N",
        postcode: "100070",
        osm_value: "attraction",
        city: "Beijing",
        country: "China"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          116.2844542,
          39.8078321
        ]
      }
    },
    {
      properties: {
        osm_key: "amenity",
        street: "Macquarie Street",
        name: "Opera House Car Park",
        state: "New South Wales",
        osm_id: 2877110066,
        osm_type: "N",
        postcode: "2000",
        osm_value: "parking",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2135144,
          -33.8593646
        ]
      }
    }
  ],
  type: "FeatureCollection"
}

正如您所看到的,Sydney Opera House有两个条目 - 一个标记有osm_value "theatre",另一个标记为"attraction"

我无法控制正在返回的数据,所以我需要一个javascript函数,我可以将json传递给它,它以相同的格式返回一个geoJson对象,但删除了重复的条目,并且删除了osm_values复制以csv格式合并,如:

var json = {
  features: [
    {
      properties: {
        osm_key: "amenity",
        extent: [
          151.214672,
          -33.8562966,
          151.2158814,
          -33.8574149
        ],
        street: "Lower Concourse",
        name: "Sydney Opera House",
        state: "New South Wales",
        osm_id: 4960757,
        osm_type: "W",
        postcode: "2061",
        osm_value: "theatre, attraction",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2152582491399,
          -33.85685575
        ]
      }
    },
    {
      properties: {
        osm_key: "highway",
        extent: [
          -95.6987584,
          29.9960185,
          -95.6984449,
          29.9907477
        ],
        name: "Opera House Row Drive",
        state: "Texas",
        osm_id: 261793234,
        osm_type: "W",
        postcode: "77433",
        osm_value: "residential",
        city: "Cypress",
        country: "United States of America"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          -95.698749,
          29.993634
        ]
      }
    },
    {
      properties: {
        osm_key: "tourism",
        street: "葆台路",
        name: "Sydney Opera House",
        state: "Beijing",
        osm_id: 3184358225,
        osm_type: "N",
        postcode: "100070",
        osm_value: "attraction",
        city: "Beijing",
        country: "China"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          116.2844542,
          39.8078321
        ]
      }
    },
    {
      properties: {
        osm_key: "amenity",
        street: "Macquarie Street",
        name: "Opera House Car Park",
        state: "New South Wales",
        osm_id: 2877110066,
        osm_type: "N",
        postcode: "2000",
        osm_value: "parking",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2135144,
          -33.8593646
        ]
      }
    }
  ],
  type: "FeatureCollection"
}

以下需要记住:

  • 可能有也可能没有任何重复内容可以过滤掉
  • 返回的geoJson必须保持相同的顺序,但删除了重复项
  • 重复项可能不是features数组
  • 中的相邻项
  • json的结构应该或多或少保持相同,但geoJson中的键不应该是硬编码的,因为可以在任何地方添加额外的子键(例如coordinatesstreet)点
  • 通过同时使用properties.osm_idproperties.osm_type 来识别
  • 重复项
  • 页面上正在使用jquery

我已经看到下面的页面提到类似的东西,但是adeneo的答案看起来仅限于硬编码密钥:https://stackoverflow.com/a/19118236/1116573而我需要它是动态的。

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:1)

希望这个功能可以解决问题。

    function mergeJson(json) {
    //invalid object
    if (!json || !json.features) {
        return;
    }
    var features = json.features;
    var added = {};
    for (var i = 0, l = features.length; i < l; i++) {
        var o = features[i].properties;
        if (!o) {
            continue;
        }
        if (added.hasOwnProperty(o.name)) {
            if (added[o.name].osm_value.indexOf(o.osm_value) === -1) {
                added[o.name].osm_value = added[o.name].osm_value + "," + o.osm_value;
            }
            //remove the object and subtract and size
            json.features.splice(i--, 1), l--;
        } else {
            added[o.name] = o;
        }
    }
}

mergeJson(json);

<强>输出

 {
    "features": [{
        "properties": {
            "osm_key": "amenity",
            "extent": [151.214672, -33.8562966, 151.2158814, -33.8574149],
            "street": "Lower Concourse",
            "name": "Sydney Opera House",
            "state": "New South Wales",
            "osm_id": 4960757,
            "osm_type": "W",
            "postcode": "2061",
            "osm_value": "theatre,attraction",
            "country": "Australia"
        },
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [151.2152582491399, -33.85685575]
        }
    }, {
        "properties": {
            "osm_key": "highway",
            "extent": [-95.6987584, 29.9960185, -95.6984449, 29.9907477],
            "name": "Opera House Row Drive",
            "state": "Texas",
            "osm_id": 261793234,
            "osm_type": "W",
            "postcode": "77433",
            "osm_value": "residential",
            "city": "Cypress",
            "country": "United States of America"
        },
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [-95.698749, 29.993634]
        }
    }, {
        "properties": {
            "osm_key": "amenity",
            "street": "Macquarie Street",
            "name": "Opera House Car Park",
            "state": "New South Wales",
            "osm_id": 2877110066,
            "osm_type": "N",
            "postcode": "2000",
            "osm_value": "parking",
            "country": "Australia"
        },
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [151.2135144, -33.8593646]
        }
    }],
    "type": "FeatureCollection"
};