2016-11-13 69 views
0

我有以下结构的geojsonfile:追加geoJSON功能与Python?

{"crs": 
    {"type": "name", 
    "properties": 
    {"name": "urn:ogc:def:crs:EPSG::4326"} 
    }, 
    "type": "FeatureCollection", 
    "features": [ 
    {"geometry": 
     {"type": "Polygon", 
     "coordinates": [[[10.914622377957983, 45.682007076150505], 
         [10.927456267537572, 45.68179119797432], 
         [10.927147329501077, 45.672795442796335], 
         [10.914315493899755, 45.67301125363092], 
         [10.914622377957983, 45.682007076150505]]]},   
     "type": "Feature", 
     "id": 0, 
     "properties": {"cellId": 38} 
    }, 
     {"geometry": 
     {"type": "Polygon", 
     "coordinates": 
    ... etc. ... 

我想根据我在Python计算每个单元单独的属性阅读本GeoJSON的进入谷歌地图,并有颜色的每个单元格。所以我最关心的问题是:如何用Python读取geoJSON并向这些Polygons添加另一个属性(有12000个多边形,因此不能将它们逐个添加),然后编写新文件?

我想我在找的是一个可以处理geoJSON的Python库,所以我不必通过srting操作来添加这些功能。

+0

确定geojson颜色的值是什么? 另外,为什么不只是在循环中添加属性?肯定不会那么慢? – alexisdevarennes

+0

这就是我的问题。我如何将geoJSON文件读入Python,并添加一个特性并写入新文件? –

回答

0

geoJSON只是一个JSON文档(简化,但你需要这个目的)。 Python将其读取为dict对象。

由于dict在就地更新,我们不需要为地理对象存储新变量。

import json 

# read in json 
geo_objects = json.load(open("/path/to/files")) 

# code ... 

for d in geo_objects: 
    d["path"]["to"]["field"] = calculated_value 

json.dump(geofiles, open("/path/to/output/file")) 

不需要字符串操作,不需要加载新库!

0

有一种方式Python geojson package

这样,就可以读取以GeoJSON有一个对象:

import geojson 
loaded = geojson.loads("Any geojson file or geojson string") 

for feature in loaded.features[0:50]: #[0:50] for the only first 50th. 
    print feature 

有特色,和的FeatureCollection自定义类来帮助你添加的属性。