Load multipolygons from GeoJSON into GeoDjango modelHelp simplifying multipolygons in geodjangoGenerating...

Output visual diagram of picture

Are hand made posters acceptable in Academia?

What should be the ideal length of sentences in a blog post for ease of reading?

Friend wants my recommendation but I don't want to give it to him

Pre-Employment Background Check With Consent For Future Checks

Do people actually use the word "kaputt" in conversation?

Strange behavior in TikZ draw command

Could a welfare state co-exist with mega corporations?

Started in 1987 vs. Starting in 1987

Why is "la Gestapo" feminine?

Would this string work as string?

If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?

"Oh no!" in Latin

Is this saw blade faulty?

How do you justify more code being written by following clean code practices?

Reason why a kingside attack is not justified

What is the period/term used describe Giuseppe Arcimboldo's style of painting?

Toggle window scroll bar

Taking the numerator and the denominator

Why is participating in the European Parliamentary elections used as a threat?

In the event of Brexit being postponed beyond the EU elections, will UK voters in EU countries be eligible to participate?

Why is indicated airspeed rather than ground speed used during the takeoff roll?

Relations between homogeneous polynomials

Magnifying glass in hyperbolic space



Load multipolygons from GeoJSON into GeoDjango model


Help simplifying multipolygons in geodjangoGenerating GeoJSON tiles from GeoDjango - basic overview?Receiving data from php script for geoJSON TileLayer?From featureclass (or shp) to WKT e GeoJSONInvalid GeoJSON when inserting data to a CartoDB (PostGIS) table from LeafletLeaflet: Change displayed column on clickfrom geometrycollection to polygon geometry with pythonGeoJSON MultiPolygon/Polygon to PostgreSQL using Python/Psycopg2Parse .geojson file in RBind parsed Feature Collection JSON object to infowindow content













1















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.










share|improve this question
















bumped to the homepage by Community 3 mins ago


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




















    1















    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.










    share|improve this question
















    bumped to the homepage by Community 3 mins ago


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


















      1












      1








      1








      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.










      share|improve this question
















      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.







      geojson geodjango geos






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 13 at 2:39









      Vince

      14.7k32749




      14.7k32749










      asked Feb 12 at 22:37









      its30its30

      338113




      338113





      bumped to the homepage by Community 3 mins ago


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







      bumped to the homepage by Community 3 mins ago


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
























          1 Answer
          1






          active

          oldest

          votes


















          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.






          share|improve this answer


























          • 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








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311970%2fload-multipolygons-from-geojson-into-geodjango-model%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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.






          share|improve this answer


























          • 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
















          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.






          share|improve this answer


























          • 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














          0












          0








          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 17 at 23:38

























          answered Feb 17 at 22:07









          Mike FabrikantMike Fabrikant

          1012




          1012













          • 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



















          • 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

















          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 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





          I think you could also create a Multipolygon directly no? E.g. feature.geom = MultiPolygon(feature.geom)

          – Alexander
          Mar 14 at 9:57


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311970%2fload-multipolygons-from-geojson-into-geodjango-model%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          (145452) 2005 RN43 Классификация | Примечания | Ссылки |...

          Щит и меч (фильм) Содержание Названия серий | Сюжет |...

          Энтрерриос (город) Содержание История | Географическое...