Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a GeoDjango Point in a model

I am working with GeoDjango for the first time and am having trouble adding a PointField to my model. Here is my model code:

from django.db import models
from django.contrib.gis.geos import Point
from django.contrib.gis.db import models

class Crime(models.Model):
    Category = models.CharField(max_length=50)
    Description = models.CharField(max_length=150)
    District =models.CharField(max_length=50)
    Incident = models.IntegerField(default=0)
    Date = models.DateTimeField(max_length=150,default="")
    Location = models.PointField()

When I try to create a migration, I get the error:

ValueError: Cannot use object with type float for a geometry lookup parameter.

When I try to create a crime object in the Python shell and save it I get the error:

django.core.exceptions.ValidationError

I've been troubleshooting this for quite a while now. I put a debugger in the actual gis library code and found that right before the ValueError was thrown, it was trying to process the number 35.0 as a geometry object.

I'm wondering if this error either has something to do with needing to set defaults in Django or if something is possibly wrong with my database connection?

like image 354
snorkelzebra Avatar asked May 14 '15 06:05

snorkelzebra


People also ask

Why use GeoDjango?

GeoDjango is a very powerful framework for storing and working with geographic data using the Django ORM. It provides an easy-to-use API to find the distances between two points on a map, areas of polygons, the points within a polygon, and so on.

What is Srid in GeoDjango?

The SRID is an integer specifier that corresponds to the projection system that will be used to interpret the data in the spatial database.


1 Answers

It turns out than passing in a default as an array of lat + long , which normally works to create a Point was not a valid option for gis to process when interpreting the schema. Here is a good default for gis point fields should anyone need one.

Figured out this solution by reading through this very old conversation: http://south.aeracode.org/ticket/599

Location = models.PointField(default='POINT(0.0 0.0)')
like image 153
snorkelzebra Avatar answered Sep 22 '22 15:09

snorkelzebra