0

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.

Your Answer

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

Browse other questions tagged or ask your own question.