2

Is it possible create a GEOSGeometry from a FeatureCollection(GeoJSON)? For instance: GEOSGeometry(json.dumps(fc)) where:

fc = { "type": "FeatureCollection",
"features": [
  { "type": "Feature",
    "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
    "properties": {"prop0": "value0"}
    },
  { "type": "Feature",
    "geometry": {
      "type": "LineString",
      "coordinates": [
        [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
        ]
      },
    "properties": {
      "prop0": "value0",
      "prop1": 0.0
      }
    },
  { "type": "Feature",
     "geometry": {
       "type": "Polygon",
       "coordinates": [
         [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
           [100.0, 1.0], [100.0, 0.0] ]
         ]
     },
     "properties": {
       "prop0": "value0",
       "prop1": {"this": "that"}
       }
     }
   ]
 }
1

1 Answer 1

2

Something like this will do what you want:

import ast
from django.contrib.gis.geos import GeometryCollection, GEOSGeometry


def make_geometrycollection_from_featurecollection(feature_collection):
    geoms = []
    features = ast.literal_eval(feature_collection)
    for feature in features['features']:
        feature_geom = feature['geometry']
        geoms.append(GEOSGeometry(feature_geom))
    return GeometryCollection(tuple(geoms))  
1
  • Should be geoms.append(GEOSGeometry(feature_geom))
    – Rick Mohr
    Jan 4, 2017 at 17:07

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.