0

I have a geojson of multipolygons like so

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "ADMIN": "Aruba",
        "ISO_A3": "ABW"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -69.99693762899992,
              12.577582098000036
            ],
            [
              -69.93639075399994,
              12.53172435100005
...

Using Python, I would like to simply add more properties to every feature in the feature collection. I would like to add these properties from a Python dictionary.

So I would take dictionary values and write them to the properties in each feature.

I've been a little lost so far frankly.

closed as off-topic by ahmadhanb, Vince, xunilk, pLumo, ArMoraer Jun 22 '18 at 7:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – ahmadhanb, Vince, xunilk, pLumo, ArMoraer
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Wecome to GIS SE. As a newish user, please take the Tour. Questions here need to not only frame the goal, but show work toward accomplishing that goal, and explain the exact problem encountered in that attempt. Processing JSON text to a dictionary with Python is a single function (json.loads()). – Vince Jun 22 '18 at 2:45
0

Here is a short code that adds properties to a GeoJSON from a Python dictionary:

import json

with open('yourgeojson.geojson', 'r') as f:
    data = json.load(f)

#A Python dictionary containing properties to be added to each GeoJSON Feature
properties_dict={
    "property1": "foo",
    "property2": 10,
    "property3": 100
    }
#Convert the dictionary to a list
properties_list=zip(properties_dict.keys(),properties_dict.values())

#Loop over GeoJSON features and add the new properties
for feat in data['features']:
    for i in range(len(properties_list)):
        feat ['properties'][properties_list[i][0]]=properties_list[i][1]

#Write result to a new file
with open('new.geojson', 'w') as f:
    json.dump(data, f)
  • Thanks for your quick response and I appreciate the help! Would you have any guidance as to how to do this if the properties for each feature need to be different? To put it in better context I am trying to append World Bank literacy data to each country in a GeoJson of all world countries. So I have many years with values for each year for each individual country. – clayton groth Jun 21 '18 at 22:34
  • That's a separate question than your original question. If I solved your current question please accept the answer (green arrow). In general, you can achieve that by means of having a mutual field in the dictionary and the geojson, and then joining them together by looping on both. If that fails, feel free to ask a new question or edit this one, demonstrating your attempts. – 15Step Jun 22 '18 at 9:13

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