使用python挑出geojson中坐标小于3的地物并删除
Use python to pick out and delete features with less than 3 coordinates in geojson
我有一个包含 100 多个特征的 GeoJSON 文件。
我注意到其中一些只是线串而不是多边形。
我想写一个 python 代码来挑选并删除 geojson 文件中坐标少于 3 个的要素,但我不确定该怎么做。
我知道我需要一个 for
和 if
循环,所以我已经走到这一步了,但是不知道如何指定少于 3 个坐标的部分。
import json
file = open('file.geojson', 'r')
data = json.load(file)
for i in data['features']:
文件示例
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [{ "type": "Feature","id":"62","properties": { "Layer": "Ref36", "SubClasses": "AcDbEntity:AcDbPolyline", "EntityHandle": "22002" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.887936622094115, 54.618954147445741, -7.3 ], [ -5.88753172216424, 54.61894917351524, -7.3 ], [ -5.887440778252523, 54.620769155685082, -7.3 ], [ -5.887526655330053, 54.62244565276194, -7.3 ], [ -5.887453014956853, 54.623926916256401, -7.3 ], [ -5.887450787638267, 54.623971716263107, -7.3 ], [ -5.890522161350234, 54.622828812872726, -7.3 ], [ -5.890547531832277, 54.622772167816805, -7.3 ], [ -5.890272672621835, 54.622673266966245, -7.3 ], [ -5.889545615138673, 54.62195303872921, -7.3 ], [ -5.888956059094582, 54.620953546743664, -7.3 ], [ -5.888904608649223, 54.619870645137233, -7.3 ], [ -5.888966440862339, 54.618559225436805, -7.3 ], [ -5.889037595793397, 54.61838849149008, -7.3 ], [ -5.889291687862707, 54.617690949111768, -7.3 ], [ -5.888927897852784, 54.617621015725618, -7.3 ], [ -5.889790161477309, 54.616110299708147, -7.3 ], [ -5.890229803751783, 54.616194813948539, -7.3 ], [ -5.889166141692859, 54.614995773327962, -7.3 ], [ -5.889268056138125, 54.612912234383849, -7.3 ], [ -5.888868667420563, 54.612693308366403, -7.3 ], [ -5.888335798340909, 54.612548305797212, -7.3 ], [ -5.888275590411526, 54.614056230303071, -7.3 ], [ -5.88773110071378, 54.614048542306797, -7.3 ], [ -5.887608025100007, 54.614438880590328, -7.3 ], [ -5.88811887231485, 54.61444779687001, -7.3 ], [ -5.888047190821367, 54.616220363648253, -7.3 ], [ -5.888016758159484, 54.616972852041378, -7.3 ], [ -5.887936622094115, 54.618954147445741, -7.3 ] ] } },
{ "type": "Feature","id":"63","properties": { "Layer": "Ref52-", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "2277C" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.881052020169729, 54.62981311635351, -11.3 ], [ -5.880730650147907, 54.629666620207963, -2.5 ] ] } },
{ "type": "Feature","id":"64","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22780" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.880345087067367, 54.630343524687945, -11.3 ], [ -5.880019591844672, 54.630200137059965, -2.5 ] ] } },
{ "type": "Feature","id":"65","properties": { "Layer": "Ref52", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22784" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.882115562941912, 54.629008362012357, -11.3 ], [ -5.881682322682144, 54.628946521181796, -2.5 ] ] } },
{ "type": "Feature","id":"66","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22788" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.878970095400486, 54.631016245634285, -2.5 ], [ -5.879300166872021, 54.631156079664251, -11.3 ] ] } },
{ "type": "Feature","id":"67","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "2278C" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.879280661325875, 54.631221212929972, -9.1 ], [ -5.878981327386499, 54.631181892876789, -2.5 ] ] } },
{ "type": "Feature","id":"68","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22790" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.878799035939746, 54.631790591657015, -2.5 ], [ -5.878946789814882, 54.632336072795887, -9.1 ] ] } },
{ "type": "Feature","id":"69","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B71" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.890809531248387, 54.622760260593964, -12.95 ], [ -5.890547531832277, 54.622772167816805, -7.3 ] ] } },
{ "type": "Feature","id":"70","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B76" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.890902513361069, 54.622552653352464, -12.95 ], [ -5.890272672621835, 54.622673266966245, -7.3 ] ] } },
{ "type": "Feature","id":"71","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B7A" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.891851404312894, 54.622693053783095, -12.95 ], [ -5.892008147615435, 54.622712645859941, -7.5 ] ] } },
{ "type": "Feature","id":"72","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B7E" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.891757679145733, 54.622901544278882, -12.95 ], [ -5.891880590876987, 54.622996396162321, -7.5 ] ] } },
{ "type": "Feature","id":"73","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B82" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.891757679145733, 54.622901544278882, -12.95 ], [ -5.891862839872977, 54.623018746748407, -9.3 ] ] } },
{ "type": "Feature","id":"74","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B86" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.890809531248387, 54.622760260593964, -12.95 ], [ -5.890663880060426, 54.62284008972955, -9.3 ] ] } },
{ "type": "Feature","id":"75","properties": { "Layer": "Ref10", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22F44" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.910798253998712, 54.611243048089676, -5.5 ], [ -5.910722703306485, 54.611234855196507, -6.0 ] ] } },
{ "type": "Feature","id":"76","properties": { "Layer": "Ref10", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F51" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.909063847930645, 54.611274900827347, -6.0 ], [ -5.909050209923891, 54.611284442275362, -6.400000000202057 ] ] } },
{ "type": "Feature","id":"77","properties": { "Layer": "Ref12", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F58" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.901395955548104, 54.616256186068412, -6.4 ], [ -5.901224182653579, 54.6163786067251, -9.1 ] ] } },
{ "type": "Feature","id":"78","properties": { "Layer": "Ref19", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F63" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.910932182938841, 54.612974676924125, -4.0 ], [ -5.910959735727921, 54.612901648194558, -6.4 ] ] } },
{ "type": "Feature","id":"79","properties": { "Layer": "Ref16", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F67" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.912204715032693, 54.612763136939328, -6.4 ], [ -5.912292471798091, 54.612802991249943, -4.0 ] ] } },
{ "type": "Feature","id":"80","properties": { "Layer": "Ref16", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F6B" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.913153166931155, 54.612994531162059, -6.4 ], [ -5.913083077761696, 54.613044796044548, -4.0 ] ] } },
{ "type": "Feature","id":"81","properties": { "Layer": "Ref16", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F6F" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.91328153391156, 54.612992411198732, -8.5 ], [ -5.913149061172596, 54.61308628063238, -4.0 ] ] } },
{ "type": "Feature","id":"82","properties": { "Layer": "Ref1", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F73" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.913634538565916, 54.613157762575192, -8.5 ], [ -5.91350332790106, 54.613252687577841, -4.0 ] ] } },
{ "type": "Feature","id":"83","properties": { "Layer": "Ref19", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F80" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.91009718855703, 54.613131738487034, -4.000000000071991 ], [ -5.909919047530581, 54.613097285060839, -6.399999999902643 ] ] } },
{ "type": "Feature","id":"84","properties": { "Layer": "Ref60", "SubClasses": "AcDbEntity:AcDbPolyline", "EntityHandle": "E58EA" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.775160638198986, 54.694417134728809, -9.6 ], [ -5.77114162233595, 54.695772598663574, -9.6 ], [ -5.76283339864894, 54.698574565809153, -9.6 ], [ -5.759686539936373, 54.695443498589412, -9.6 ], [ -5.767993867108963, 54.692642102771394, -9.6 ], [ -5.773785476803047, 54.692595379393715, -9.6 ], [ -5.775160638198986, 54.694417134728809, -9.6 ] ] } },
{ "type": "Feature","id":"85","properties": { "Layer": "Ref38", "SubClasses": "AcDbEntity:AcDbPolyline", "EntityHandle": "1461A8" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.893508789864172, 54.612251967302818, -6.4 ], [ -5.893117446685634, 54.612512719236747, -6.4 ] ] } }
]
}
这是一个您可以使用的简单脚本。我添加了 with
上下文管理器来处理文件 I/O。另一件需要注意的事情是修改你正在迭代的对象,所以我弹出了非过滤的“功能”列表并创建了一个新的要用循环填充的。
import json
# Use with to open file and close it automatically once its not needed
with open('file.geojson', 'r') as file:
data = json.load(file)
# Remove old features
features = data.pop("features")
# Make new empty list
data["features"] = []
for i in features:
# Keep if coordinates list has 3 or more items
if len(i["geometry"]["coordinates"]) > 2:
data["features"].append(i)
print(json.dumps(data, indent=4)
我有一个包含 100 多个特征的 GeoJSON 文件。 我注意到其中一些只是线串而不是多边形。
我想写一个 python 代码来挑选并删除 geojson 文件中坐标少于 3 个的要素,但我不确定该怎么做。
我知道我需要一个 for
和 if
循环,所以我已经走到这一步了,但是不知道如何指定少于 3 个坐标的部分。
import json
file = open('file.geojson', 'r')
data = json.load(file)
for i in data['features']:
文件示例
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [{ "type": "Feature","id":"62","properties": { "Layer": "Ref36", "SubClasses": "AcDbEntity:AcDbPolyline", "EntityHandle": "22002" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.887936622094115, 54.618954147445741, -7.3 ], [ -5.88753172216424, 54.61894917351524, -7.3 ], [ -5.887440778252523, 54.620769155685082, -7.3 ], [ -5.887526655330053, 54.62244565276194, -7.3 ], [ -5.887453014956853, 54.623926916256401, -7.3 ], [ -5.887450787638267, 54.623971716263107, -7.3 ], [ -5.890522161350234, 54.622828812872726, -7.3 ], [ -5.890547531832277, 54.622772167816805, -7.3 ], [ -5.890272672621835, 54.622673266966245, -7.3 ], [ -5.889545615138673, 54.62195303872921, -7.3 ], [ -5.888956059094582, 54.620953546743664, -7.3 ], [ -5.888904608649223, 54.619870645137233, -7.3 ], [ -5.888966440862339, 54.618559225436805, -7.3 ], [ -5.889037595793397, 54.61838849149008, -7.3 ], [ -5.889291687862707, 54.617690949111768, -7.3 ], [ -5.888927897852784, 54.617621015725618, -7.3 ], [ -5.889790161477309, 54.616110299708147, -7.3 ], [ -5.890229803751783, 54.616194813948539, -7.3 ], [ -5.889166141692859, 54.614995773327962, -7.3 ], [ -5.889268056138125, 54.612912234383849, -7.3 ], [ -5.888868667420563, 54.612693308366403, -7.3 ], [ -5.888335798340909, 54.612548305797212, -7.3 ], [ -5.888275590411526, 54.614056230303071, -7.3 ], [ -5.88773110071378, 54.614048542306797, -7.3 ], [ -5.887608025100007, 54.614438880590328, -7.3 ], [ -5.88811887231485, 54.61444779687001, -7.3 ], [ -5.888047190821367, 54.616220363648253, -7.3 ], [ -5.888016758159484, 54.616972852041378, -7.3 ], [ -5.887936622094115, 54.618954147445741, -7.3 ] ] } },
{ "type": "Feature","id":"63","properties": { "Layer": "Ref52-", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "2277C" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.881052020169729, 54.62981311635351, -11.3 ], [ -5.880730650147907, 54.629666620207963, -2.5 ] ] } },
{ "type": "Feature","id":"64","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22780" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.880345087067367, 54.630343524687945, -11.3 ], [ -5.880019591844672, 54.630200137059965, -2.5 ] ] } },
{ "type": "Feature","id":"65","properties": { "Layer": "Ref52", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22784" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.882115562941912, 54.629008362012357, -11.3 ], [ -5.881682322682144, 54.628946521181796, -2.5 ] ] } },
{ "type": "Feature","id":"66","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22788" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.878970095400486, 54.631016245634285, -2.5 ], [ -5.879300166872021, 54.631156079664251, -11.3 ] ] } },
{ "type": "Feature","id":"67","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "2278C" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.879280661325875, 54.631221212929972, -9.1 ], [ -5.878981327386499, 54.631181892876789, -2.5 ] ] } },
{ "type": "Feature","id":"68","properties": { "Layer": "Ref51", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22790" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.878799035939746, 54.631790591657015, -2.5 ], [ -5.878946789814882, 54.632336072795887, -9.1 ] ] } },
{ "type": "Feature","id":"69","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B71" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.890809531248387, 54.622760260593964, -12.95 ], [ -5.890547531832277, 54.622772167816805, -7.3 ] ] } },
{ "type": "Feature","id":"70","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B76" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.890902513361069, 54.622552653352464, -12.95 ], [ -5.890272672621835, 54.622673266966245, -7.3 ] ] } },
{ "type": "Feature","id":"71","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B7A" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.891851404312894, 54.622693053783095, -12.95 ], [ -5.892008147615435, 54.622712645859941, -7.5 ] ] } },
{ "type": "Feature","id":"72","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B7E" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.891757679145733, 54.622901544278882, -12.95 ], [ -5.891880590876987, 54.622996396162321, -7.5 ] ] } },
{ "type": "Feature","id":"73","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B82" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.891757679145733, 54.622901544278882, -12.95 ], [ -5.891862839872977, 54.623018746748407, -9.3 ] ] } },
{ "type": "Feature","id":"74","properties": { "Layer": "Ref45", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22B86" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.890809531248387, 54.622760260593964, -12.95 ], [ -5.890663880060426, 54.62284008972955, -9.3 ] ] } },
{ "type": "Feature","id":"75","properties": { "Layer": "Ref10", "SubClasses": "AcDbEntity:AcDb3dPolyline", "EntityHandle": "22F44" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.910798253998712, 54.611243048089676, -5.5 ], [ -5.910722703306485, 54.611234855196507, -6.0 ] ] } },
{ "type": "Feature","id":"76","properties": { "Layer": "Ref10", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F51" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.909063847930645, 54.611274900827347, -6.0 ], [ -5.909050209923891, 54.611284442275362, -6.400000000202057 ] ] } },
{ "type": "Feature","id":"77","properties": { "Layer": "Ref12", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F58" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.901395955548104, 54.616256186068412, -6.4 ], [ -5.901224182653579, 54.6163786067251, -9.1 ] ] } },
{ "type": "Feature","id":"78","properties": { "Layer": "Ref19", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F63" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.910932182938841, 54.612974676924125, -4.0 ], [ -5.910959735727921, 54.612901648194558, -6.4 ] ] } },
{ "type": "Feature","id":"79","properties": { "Layer": "Ref16", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F67" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.912204715032693, 54.612763136939328, -6.4 ], [ -5.912292471798091, 54.612802991249943, -4.0 ] ] } },
{ "type": "Feature","id":"80","properties": { "Layer": "Ref16", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F6B" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.913153166931155, 54.612994531162059, -6.4 ], [ -5.913083077761696, 54.613044796044548, -4.0 ] ] } },
{ "type": "Feature","id":"81","properties": { "Layer": "Ref16", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F6F" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.91328153391156, 54.612992411198732, -8.5 ], [ -5.913149061172596, 54.61308628063238, -4.0 ] ] } },
{ "type": "Feature","id":"82","properties": { "Layer": "Ref1", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F73" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.913634538565916, 54.613157762575192, -8.5 ], [ -5.91350332790106, 54.613252687577841, -4.0 ] ] } },
{ "type": "Feature","id":"83","properties": { "Layer": "Ref19", "SubClasses": "AcDbEntity:AcDb3dPolyline", "Linetype": "Continuous", "EntityHandle": "22F80" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.91009718855703, 54.613131738487034, -4.000000000071991 ], [ -5.909919047530581, 54.613097285060839, -6.399999999902643 ] ] } },
{ "type": "Feature","id":"84","properties": { "Layer": "Ref60", "SubClasses": "AcDbEntity:AcDbPolyline", "EntityHandle": "E58EA" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.775160638198986, 54.694417134728809, -9.6 ], [ -5.77114162233595, 54.695772598663574, -9.6 ], [ -5.76283339864894, 54.698574565809153, -9.6 ], [ -5.759686539936373, 54.695443498589412, -9.6 ], [ -5.767993867108963, 54.692642102771394, -9.6 ], [ -5.773785476803047, 54.692595379393715, -9.6 ], [ -5.775160638198986, 54.694417134728809, -9.6 ] ] } },
{ "type": "Feature","id":"85","properties": { "Layer": "Ref38", "SubClasses": "AcDbEntity:AcDbPolyline", "EntityHandle": "1461A8" }, "geometry": { "type": "LineString", "coordinates": [ [ -5.893508789864172, 54.612251967302818, -6.4 ], [ -5.893117446685634, 54.612512719236747, -6.4 ] ] } }
]
}
这是一个您可以使用的简单脚本。我添加了 with
上下文管理器来处理文件 I/O。另一件需要注意的事情是修改你正在迭代的对象,所以我弹出了非过滤的“功能”列表并创建了一个新的要用循环填充的。
import json
# Use with to open file and close it automatically once its not needed
with open('file.geojson', 'r') as file:
data = json.load(file)
# Remove old features
features = data.pop("features")
# Make new empty list
data["features"] = []
for i in features:
# Keep if coordinates list has 3 or more items
if len(i["geometry"]["coordinates"]) > 2:
data["features"].append(i)
print(json.dumps(data, indent=4)