3

For example, I have a file as shown below:

{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 8.2781982421875, 52.58636344214018 ], [ 10.0250244140625, 52.281601868071434 ], [ 8.865966796875, 52.07612995654167 ], [ 8.519897460937498, 52.24125614966341 ] ] } } ] }

and I want to update this file get something like this.

{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 8.2781982421875, 52.58636344214018 ], [ 10.0250244140625, 52.281601868071434 ], [ 8.865966796875, 52.07612995654167 ], [ 8.519897460937498, 52.24125614966341 ] ] } }, { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 8.10791015625, 51.580483198305345 ], [ 7.6629638671875, 52.05249047600099 ], [ 7.717895507812499, 52.36553758871974 ], [ 8.0584716796875, 52.09638241034154 ], [ 8.1793212890625, 51.71681946274873 ] ] } } ] }

How to append this geojson file?

3
  • Are you working with any sort of module? Or just the built in Python JSON module? And this is essentially just a dictionary?
    – KHibma
    Aug 28, 2018 at 14:15
  • Yes, I am working on geoplotlib. Currently my code is appending in the end which is problem while parsing rather ( as shown below ) i just want to update features in the existent file. Aug 28, 2018 at 15:08
  • { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 8.2781982421875, 52.58636344214018 ], [ 10.0250244140625, 52.281601868071434 ], [ 8.865966796875, 52.07612995654167 ], [ 8.519897460937498, 52.24125614966341 ] ] } } ] }{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 8.10791015625, 51.580483198305345 ], [ 7.6629638671875, 52.05249047600099 ], ............ } } ] } Aug 28, 2018 at 15:13

1 Answer 1

2

I don't know the geoplotlib library; perhaps it has a built in function to make this "Easy". However, because you can display JSON as a python dictionary, you can just add to it using the built in dictionary and list functions.

Take your first bit of JSON:

z = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 8.2781982421875, 52.58636344214018 ], [ 10.0250244140625, 52.281601868071434 ], [ 8.865966796875, 52.07612995654167 ], [ 8.519897460937498, 52.24125614966341 ] ] } } ] }

You can then display just the features (the list you want to update):

z['features']

I assume you have your feature you want to add somewhere, say, as a variable?

newfeats =  { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ 8.10791015625, 51.580483198305345 ], [ 7.6629638671875, 52.05249047600099 ], [ 7.717895507812499, 52.36553758871974 ], [ 8.0584716796875, 52.09638241034154 ], [ 8.1793212890625, 51.71681946274873 ] ] } }

You just need to append your new feature dictionary to the feature list:

z['features'].append(newfeat)

There you go:

z['features']

[{'geometry': {'type': 'LineString', 'coordinates': [[8.2781982421875, 52.58636344214018], [10.0250244140625, 52.281601868071434], [8.865966796875, 52.07612995654167], [8.519897460937498, 52.24125614966341]]}, 'type': 'Feature', 'properties': {}}, {'geometry': {'type': 'LineString', 'coordinates': [[8.10791015625, 51.580483198305345], [7.6629638671875, 52.05249047600099], [7.717895507812499, 52.36553758871974], [8.0584716796875, 52.09638241034154], [8.1793212890625, 51.71681946274873]]}, 'type': 'Feature', 'properties': {}}]

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.