1

I am trying to do simple geodjango app, which contains list of shops, and when you put an address in it it returns closest shops to your location.

I am following tutorial which used PostgreSQL and Postgis, but I want to use SQLite and SpatiaLite (I want to use it later in another app which uses SQLite, so I don't know if I won't mess up, if I try to change it into PostgreSQL. I don't usually work with Python nor SQLite nor PostgreSQL)

I have a problem when I try to make a query and count the distance.

In my models.py I have:

from django.contrib.gis.db import models as gis_models
from django.contrib.gis import geos
from django.db import models

class Shop(models.Model):
    name = models.CharField(max_length=200)
    address = models.CharField(max_length=100)
    city = models.CharField(max_length=50)
    location = gis_models.PointField(u"longitude/latitude", geography=True, blank=True, null=True)
    gis = gis_models.GeoManager()
    objects = models.Manager()

And in views.py I have function:

def get_shops(longitude, latitude):
    current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude))
    distance_from_point = {'km': 10}
    shops = models.Shop.gis.filter(location__distance_lte=(current_point, measure.D(**distance_from_point)))
    shops = shops.distance(current_point).order_by('distance')
    return shops.distance(current_point)

I get error:

SQLite does not support linear distance calculations on geodetic coordinate systems.

I've read about SRID, I guess that I should change my model. But I have no idea how to write it down. And there is a possibility that it's there is a problem with how I try to get the sorted data.

JJD
  • 50,076
  • 60
  • 203
  • 339
ajgoralczyk
  • 35
  • 1
  • 10

1 Answers1

0

I think one problem is in your models.py Your model manager is still the default model manager. Your model should look something like this.

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


class Shop(models.Model):
    name = models.CharField(max_length=200)
    address = models.CharField(max_length=100)
    city = models.CharField(max_length=50)
    location = models.PointField(u"longitude/latitude", geography=True, blank=True, null=True)
    objects = models.GeoManager()

So this model is associate with the geodjango model-manager. It's also not required to import modelfields from different sources.

The other problem SQLite does not support linear distance calculations on geodetic coordinate systems you can rid of if you set your srid to something like 4326 (WGS84). Some more information you can find here. Or you implement your own distance calculation based on triangulation. Look at this for some further explanation.

Community
  • 1
  • 1
t_io
  • 1,952
  • 1
  • 19
  • 27
  • I have two managers, and they work. I have changed: PointField(u"longitude/latitude", geography=True, blank=True, null=True) into: PointField(srid=4326, blank=True, null=True), and I can add shops, but it still does not support linear distance calculations. When I change SRID to 3857 then sorting by distance works, but adding does not. – ajgoralczyk Feb 03 '15 at 15:49
  • sure, my mistake. I had overseen this line `models.Shop.gis.filter(location__distance_lte=(current_point, measure.D(**distance_from_point)))` where you query the correct filter. So the problem isn't the model. I update my answer – t_io Feb 03 '15 at 15:55