如何在python中将属性追加到geojson文件?

时间:2021-03-30 23:46:20

For example i have geojson file with features as shown below.

例如,我有geojson文件,其功能如下所示。

{ "type": "FeatureCollection", "working_width": 20, "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 28.4766, 12.5645456 ] } } ]

{“type”:“FeatureCollection”,“working_width”:20,“features”:[{“type”:“Feature”,“geometry”:{“type”:“Point”,“coordinates”:[28.4766,12.5645456 ]}}]

How to add the properties to this above file as shown below.

如何将属性添加到上面的文件,如下所示。

{ "type": "FeatureCollection", "working_width": 20, "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 28.4766, 12.5645456 ] }, "properties": { "fieldID": "2115145", "segmentId": "255c2s4c", "speed": 21.4586954, "elevation": 52.4586642, "time": "2018-05" } } ] }

{“type”:“FeatureCollection”,“working_width”:20,“features”:[{“type”:“Feature”,“geometry”:{“type”:“Point”,“coordinates”:[28.4766,12.5645456 ]},“properties”:{“fieldID”:“2115145”,“segmentId”:“255c2s4c”,“speed”:21.4586954,“elevation”:52.4586642,“time”:“2018-05”}}]}

1 个解决方案

#1


0  

The data structure is just a regular python dictionary so you can update it as normal:

数据结构只是一个常规的python字典,因此您可以正常更新它:

>>> geojson 
{'type': 'FeatureCollection',
 'working_width': 20,
 'features': [{'type': 'Feature',
  'geometry': {'type': 'Point', 'coordinates': [28.4766, 12.5645456]}}]}

>>> geojson["properties"] =  { "fieldID": "2115145", 
                               "segmentId": "255c2s4c", 
                               "speed": 21.4586954, 
                               "elevation": 52.4586642, 
                               "time": "2018-05" }

>>> geojson
{'type': 'FeatureCollection' 'working_width': 20,
 'features': [{'type': 'Feature',
  'geometry': {'type': 'Point', 'coordinates': [28.4766, 12.5645456]}}],
 'properties': {'fieldID': '2115145',
  'segmentId': '255c2s4c',
  'speed': 21.4586954,
  'elevation': 52.4586642,
  'time': '2018-05'}}

#1


0  

The data structure is just a regular python dictionary so you can update it as normal:

数据结构只是一个常规的python字典,因此您可以正常更新它:

>>> geojson 
{'type': 'FeatureCollection',
 'working_width': 20,
 'features': [{'type': 'Feature',
  'geometry': {'type': 'Point', 'coordinates': [28.4766, 12.5645456]}}]}

>>> geojson["properties"] =  { "fieldID": "2115145", 
                               "segmentId": "255c2s4c", 
                               "speed": 21.4586954, 
                               "elevation": 52.4586642, 
                               "time": "2018-05" }

>>> geojson
{'type': 'FeatureCollection' 'working_width': 20,
 'features': [{'type': 'Feature',
  'geometry': {'type': 'Point', 'coordinates': [28.4766, 12.5645456]}}],
 'properties': {'fieldID': '2115145',
  'segmentId': '255c2s4c',
  'speed': 21.4586954,
  'elevation': 52.4586642,
  'time': '2018-05'}}