2

Given a GeoJSON like following:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "city": "city1"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          113.5546875,
          63.704722429433225
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "city": "city2"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          56.953125,
          41.508577297439324
        ]
      }
    }
  ]
}

How can I delete (or generate a new GeoJSON) the feature with the city property value city2 with Python?

So far I have this, though I am a little stuck in my logical right now. Would apprecaite help or suggestions.

import json
city_name = "city2"
city_geojson = [city if city['properties']['city'] != city_name else '' for city in city_geojson['features']]
1

Just a quick hack that might suit you:

import itertools

t = {'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coordinates': [113.5546875, 63.704722429433225]}, 'type': 'Feature', 'properties': {'city': 'city1'}}, {'geometry': {'type': 'Point', 'coordinates': [56.953125, 41.508577297439324]}, 'type': 'Feature', 'properties': {'city': 'city2'}}]}

features = []
for k,v in itertools.groupby([x for x in t['features'] if not x['properties']['city'] == 'city2' ]):  # filter out city2
    features.append(k)
t['features'] = features

# t = {'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coordinates': [113.5546875, 63.704722429433225]}, 'type': 'Feature', 'properties': {'city': 'city1'}}]}
1

Based on @nickves answer I adapted my code to the following which worked for me.

import json
city_name = "city2"
city_geojson['features'] = [city for city in city_geojson['features'] if not city['properties']['city'] == city_name]
  • I don't think it goes any better than this – nickves Apr 16 '15 at 12:20
1

The result of json.loads(geojson) is a Python dictionary. You can simply use the json module (or the geojson module) and dictionaries as it was planned.

geojson = "{'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coordinates': [113.5546875, 63.704722429433225]}, 'type': 'Feature', 'properties': {'city': 'city1'}}, {'geometry': {'type': 'Point', 'coordinates': [56.953125, 41.508577297439324]}, 'type': 'Feature', 'properties': {'city': 'city2'}}]}"
res = json.loads(geojson)
# now res is a Python dictionary with the keys 
print res.keys()
[u'type', 'features']
print len(res['features'])
2 # two features
# select only the first feature 
geojson2 = {"type": res['type'],"features":res.features[0]}
print geojson2
{'type': 'FeatureCollection', 'features': {"geometry": {"coordinates": [113.5546875, 63.704722429433225], "type": "Point"}, "id": null, "properties": {"city": "city1"}, "type": "Feature"}}
# convert to JSON format
json.dumps(geojson2)
'{"type": "FeatureCollection", "features": {"geometry": {"type": "Point", "coordinates": [113.5546875, 63.704722429433225]}, "type": "Feature", "id": null, "properties": {"city": "city1"}}}'

The problem was that the result is no longer a FeatureCollection but a simple feature

 geojson3  = res.features[0]
 print  geojson3
{"geometry": {"coordinates": [113.5546875, 63.704722429433225], "type": "Point"}, "id": null, "properties": {"city": "city1"}, "type": "Feature"}

Change a value

 geojson3['id'] = 45
 print geojson3
 {"geometry": {"coordinates": [113.5546875, 63.704722429433225], "type": "Point"}, "id": 45, "properties": {"city": "city1"}, "type": "Feature"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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