根据Python中的条件插入和删除geojson对象

时间:2019-04-26 03:52:35

标签: python json geojson

我有一个Geojson,如下:

json.dumps

如果suppress.focus.stealing=false中不存在data = { "type": "FeatureCollection", "name": "entities", "features": [ { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "3C8", "area": "141.81", "type": "p", "Text": "area:141.81;type:p" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2721.1572741014097, 1454.3223948456648 ], [ 2720.121847266826, 1454.3223948456648 ], [ 2720.121847266826, 1452.6092152478227 ], [ 2710.5679254269344, 1452.6092152478227 ], [ 2721.1572741014097, 1430.1478385206133 ], [ 2721.1572741014097, 1454.3223948456648 ] ] ] } }, { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "3CE", "area": "44.79", "type": "h", "Text": "area:44.79;type:h" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2710.723323781393, 1450.3320226620049 ], [ 2720.0654518264787, 1450.3320226620049 ], [ 2720.0654518264787, 1445.537183875705 ], [ 2710.723323781393, 1445.537183875705 ], [ 2710.723323781393, 1450.3320226620049 ] ] ] } }, { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "610", "name": "706", "area": "92.28", "type": "o", "Text": "name:706;area:92.28;type:o" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2714.603212251531, 1462.7249212430308 ], [ 2711.7289360797, 1462.7249212430308 ], [ 2711.7289360797, 1464.852506681824 ], [ 2705.7302059101926, 1460.6840827804538 ], [ 2710.567925426934, 1454.3223948456637 ], [ 2710.567925426934, 1453.838838298367 ], [ 2714.603212251531, 1453.838838298367 ], [ 2714.603212251531, 1462.7249212430308 ] ] ] } } ] } ,我想插入"name": "",并删除name对象,因为它是重复的,如何在Python中做到这一点? 提前非常感谢!

预期结果:

properties

更新: 到目前为止,我的解决方案似乎可行。

"Text"

我将其定义为一个函数,但是在某些情况下,出现如下错误。有人知道如何解决吗?谢谢。

data = {
      "type": "FeatureCollection",
      "name": "entities",
      "features": [
        {
          "type": "Feature",
          "properties": {
            "Layer": "0",
            "SubClasses": "AcDbEntity:AcDbBlockReference",
            "EntityHandle": "3C8",
            "name": "",
            "area": "141.81",
            "type": "p"
          },
          "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  2721.1572741014097,
                  1454.3223948456648
                ],
                [
                  2720.121847266826,
                  1454.3223948456648
                ],
                [
                  2720.121847266826,
                  1452.6092152478227
                ],
                [
                  2710.5679254269344,
                  1452.6092152478227
                ],
                [
                  2721.1572741014097,
                  1430.1478385206133
                ],
                [
                  2721.1572741014097,
                  1454.3223948456648
                ]
              ]
            ]
          }
        },
        {
          "type": "Feature",
          "properties": {
            "Layer": "0",
            "SubClasses": "AcDbEntity:AcDbBlockReference",
            "EntityHandle": "3CE",
            "name": "",
            "area": "44.79",
            "type": "h"
          },
          "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  2710.723323781393,
                  1450.3320226620049
                ],
                [
                  2720.0654518264787,
                  1450.3320226620049
                ],
                [
                  2720.0654518264787,
                  1445.537183875705
                ],
                [
                  2710.723323781393,
                  1445.537183875705
                ],
                [
                  2710.723323781393,
                  1450.3320226620049
                ]
              ]
            ]
          }
        },
        {
          "type": "Feature",
          "properties": {
            "Layer": "0",
            "SubClasses": "AcDbEntity:AcDbBlockReference",
            "EntityHandle": "610",
            "name": "706",
            "area": "92.28",
            "type": "o"
          },
          "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  2714.603212251531,
                  1462.7249212430308
                ],
                [
                  2711.7289360797,
                  1462.7249212430308
                ],
                [
                  2711.7289360797,
                  1464.852506681824
                ],
                [
                  2705.7302059101926,
                  1460.6840827804538
                ],
                [
                  2710.567925426934,
                  1454.3223948456637
                ],
                [
                  2710.567925426934,
                  1453.838838298367
                ],
                [
                  2714.603212251531,
                  1453.838838298367
                ],
                [
                  2714.603212251531,
                  1462.7249212430308
                ]
              ]
            ]
          }
        }
      ]
    }

2 个答案:

答案 0 :(得分:0)

到目前为止,我的解决方案似乎可行。

def is_palindrome(word):
    if len(word) <= 1:
        return True
    if len(word) > 1:
        if word[0] != word[len(word) - 1]:
            return False
        elif word[0] == word[len(word) - 1]:
            return word[1:(len(word) - 1)]

possible_palindrome = input("Enter a word/phrase to check: ")
if is_palindrome(possible_palindrome):
    print(possible_palindrome, "is a palindrome")
else:
    print(possible_palindrome, "is not a palindrome")

我将其定义为一个函数,但是在某些情况下,出现如下错误。有人知道如何解决吗?谢谢。

import json
features = data["features"]

for i in features:
    d = i["properties"]
    if "name" not in d:
        d["name"] = ""
    if i["properties"]["Text"] is not None:
        del i["properties"]["Text"]

答案 1 :(得分:0)

在每个属性中,“文本”仅出现一次。请解释在哪里重复?