Nest a dictionary in json with python for geojson properties

by Oliver Burdekin   Last Updated September 11, 2019 09:22 AM

Using the Python geojson library I am creating a feature collection of MultiPolygons. I cannot find a way to include properties (attributes) in the features. I have tried:

features = df.apply(
    lambda row: Feature(geometry=MultiPolygon(row['myGeom'])),axis=1).tolist()

properties = df.drop(['columns to drop'], axis=1).to_dict('records')
feature_collection = FeatureCollection(features=features, properties=properties)

This creates properties and features but they are not nested as the should be in a geojson. A list of properties is created:

"properties": [
{
  "meta_instanceID": "01"
},
{
  "meta_instanceID": "02"
},
{
  "meta_instanceID": "03"
},
{
  "meta_instanceID": "04"
}
],

And a list of features (note the empty properties dict at the end):

"features": [
{
  "type": "Feature",
  "geometry": {
    "type": "MultiPolygon",
    "coordinates": [
      [
        -1.289176,
        51.781223
      ],
      [
        -1.266689,
        51.784727
      ],
      [
        -1.240768,
        51.782497
      ],
      .
      ..
      ...
    ]
    },
      "properties": {}
},
{
  "type": "Feature",
  "geometry": {
    "type": "MultiPolygon",
    "coordinates": [
      [
        -1.270251,
        51.769805
      ],
      .
      ..
      ...etc

I have tried putting the properties dict in the features directly:

features = df.apply(
    lambda row: Feature(properties=df.drop(['columns to drop'],axis=1).to_dict('records'), geometry=MultiPolygon(row['myGeom'])),axis=1).tolist()

feature_collection = FeatureCollection(features=features)

But this adds all properties for all features to every feature:

{
  "type": "Feature",
  "geometry": {
    "type": "MultiPolygon",
    "coordinates": [
      [
        -1.270251,
        51.769805
      ],
      [
        -1.263256,
        51.771079
      ],
    ]
},
      "properties": [
    {
      "meta_instanceID": "01"
    },
    {
      "meta_instanceID": "02"
    },
    {
      "meta_instanceID": "03"
    },
    {
      "meta_instanceID": "04"
    }
  ]
},

What I need is for the property to match the feature.



Related Questions


Updated June 24, 2018 15:22 PM

Updated September 11, 2017 17:22 PM

Updated April 03, 2017 17:22 PM

Updated June 21, 2018 20:22 PM

Updated August 28, 2018 14:22 PM