0

I have a simple model:

from django.contrib.gis.db import models

class CountryShapes(models.Model):
    geonameid = models.PositiveIntegerField(primary_key=True)
    geom = models.MultiPolygonField()

and a GeoJSON file from here: http://download.geonames.org/export/dump/shapes_simplified_low.json.zip

I am attempting to load the data into my model using this:

        import json
        from django.contrib.gis.geos import GEOSGeometry

        with open('shapes_simplified_low.json', 'r') as fd:
            data = json.load(fd)
            for feature in data['features']:

                geom = GEOSGeometry(str(feature['geometry']))
                geonameid = feature['properties']['geoNameId']

                country = CountryShapes( # throws error here
                    geonameid=geonameid,
                    geom=GEOSGeometry(geom))
                country.save()

and I get this error:

TypeError: Cannot set CountryShapes SpatialProxy (MULTIPOLYGON) with value of type: class 'django.contrib.gis.geos.polygon.Polygon'

GEOSGeometry seems to create the geometry fine, its when I pass the geom argument to my model it throws the error.

bumped to the homepage by Community 10 mins ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

0

The issue: You're trying to save a feature of type 'Polygon' to a field defined for MultiPolygons. (You'll notice a list of geojson features often have both Polygons and MultiPolygons.)

I'm using this hack: If a feature is type Polygon, change it to type MultiPolygon by changing type and putting the coordinates within an array.

    geom = GEOSGeometry(str(feature['geometry']))
    if geom.type == 'Polygon':
      geom['type'] = 'MultiPolygon'
      geom['coordinates'] = [feature['geometry']['coordinates']]

    geonameid = feature['properties']['geoNameId']

    country = CountryShapes( # throws error here
        geonameid=geonameid,
        geom=GEOSGeometry(geom))
    country.save()

PS: I have almost no experience in Python.

  • I put this project on hold for now but will revisit it at some point and let you know if this works. Thanks for the suggestion. – its30 Feb 28 at 21:10
  • I think you could also create a Multipolygon directly no? E.g. feature.geom = MultiPolygon(feature.geom) – Alexander Mar 14 at 9:57

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.