6

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
2
  • Can you post the contents of your test file? I suspect your test file already contains a feature collection, so when you attempt to build a feature collection from it, you just end up wrapping your existing feature collection inside another feature collection. Instead, you should use geojson.load in place of json.load to create a geojson object directly from your initial file. Commented Nov 23, 2015 at 18:31
  • Yes, it does already contain a feature collection (file was generated by QGIS, but this won't always be the case). And I was able to get it to work this morning by FeatureCollection(data['features'])--python isn't my strength, so I wasn't sure if this was the proper way to use the geojson module. Commented Nov 23, 2015 at 18:38

3 Answers 3

2

What is your FeatureCollection object? Is that a custom class you created or from an open source python package? The JSON objects for the feature collection become python dictionaries so they can be modified easily.

For example, if you wanted to change the gval field value you can iterate through the features like this:

for feature in data['features']['features']:

    # change the gval attribute
    feature['properties']['gval'] = 3.5


# You can also access the first feature by index
feature1 = data['features']['features'][0]

To save your changes back to json, you can just call json.dump() to save it back to a .json file.

2
  • I am using from the FeatureCollection from geoson module (1.3.1). And using JSON objects is my fallback (I am also questioning if there is any reason for me to use the geojson module). Commented Nov 23, 2015 at 18:31
  • I think you are right that you do not need to use the geojson module. Just follow the example @crmackey gave you. It is more useful if you are creating geojson from scratch instead of modifying existing geojson. Commented Nov 23, 2015 at 18:33
1

I accepted the non-geojson answer, but here's what worked for me using the geojson FeatureCollection:

import sys
from geojson import Feature, Point, FeatureCollection, Polygon
import json
import geojson
from pprint import pprint

#print(sys.version)

path=r'd:\temp\test.json'

with open(path,'r') as data_file:
    data = json.load(data_file)

feature_collection = FeatureCollection(data['features'])

print feature_collection
1

Here's a round-trip editing example that doesn't use the geojson library.

import os, json

with open(os.environ['HOME']+'/Desktop/f.json', 'r') as data_file:    
    data = json.load(data_file)

print data['features'][0]['properties']['gval']
data['features'][0]['properties']['gval'] = 2.2
print data['features'][0]['properties']['gval']

outfile = open(os.environ['HOME']+'/Desktop/fnew.json', 'w+')
outfile.write(json.dumps(data))

And here's the test JSON file (f.json)

{
    "features": [
        {
            "geometry": {
                "coordinates": [
                    []
                ],
                "type": "Polygon"
            },
            "properties": {
                "area": 2186.81933167,
                "gval": 2.2,
                "hectares": 0.218681933167,
                "id": 1.0,
                "xid": 0
            },
            "type": "Feature"
        }
    ],
    "type": "FeatureCollection"
}

outputs:

bash: 1.0
bash: 2.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.