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
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
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.
add a comment |
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
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.
add a comment |
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
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
geojson geodjango geos
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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