[GIS] How to append geojson file using featureselection

geojsonjsonmatplotlibpython

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?

Best Answer

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': {}}]