I am trying to read in a json FeatureCollection (geojson 1.3.1
) Python (2.7), add some properties, and write it back out to a json file (or string).
I can read the file, and modify the properties, but I end up with 2 nested FeatureCollection objects:
{
"type": "FeatureCollection",
"features": {
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Polygon",
"coordinates": [
[
]
]
},
"type": "Feature",
"properties": {
"gval": 1.0,
"hectares": 0.218681933167,
"xid": 0,
"id": 1.0,
"area": 2186.81933167
}
}
]
}
}
And I think I've worked out that I creating my FeatureCollection incorrectly:
path=r'd:\temp\test.json'
with open(path) as data_file:
data = json.load(data_file)
print data
feature_collection = FeatureCollection(data)
print feature_collection
The feature_collection
ends with nested features, which is probably why the resulting geojson.dumps has the nested FeatureCollection
{"features": {"features": [{"geometry":
I can think of a number of ways to hack it, but what is the correct way to create that FeatureCollection? Modify feature attribtues, and then write it back out to a file?
Content of the test.json file (minus coordinate values):
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Polygon",
"coordinates": [
]
},
"type": "Feature",
"properties": {
"area": 2186.81933167,
"id": 1.0,
"xid": 0,
"gval": 1.0
}
}
]
}
Version of geojson:
Metadata-Version: 2.0
Name: geojson
Version: 1.3.1
Summary: Python bindings and utilities for GeoJSON
Home-page: https://github.com/frewsxcv/python-geojson
Author: Corey Farwell
Author-email: [email protected]
License: BSD
Location: c:\python27\arcgis10.2\lib\site-packages
Requires: setuptools
geojson.load
in place ofjson.load
to create a geojson object directly from your initial file.FeatureCollection(data['features'])
--python isn't my strength, so I wasn't sure if this was the proper way to use the geojson module.