0

My model has two fields (latitude and longitude) that I want to combine to form a point object. However, I cannot figure out how to filter based on a combination of those values:

For example:

>>> from django.contrib.gis.geos import Point
>>> lat = 5
>>> lon = 1
>>> pnt = Point(lat, lon)
>>> buf = pnt.buffer(0.0001)
>>> z = Thing.objects.filter(pnt__intersects=buf) 

FieldError: Cannot resolve keyword 'pnt' into field.   ## I dont have a model field named pnt

I realize this is not the right approach, but I think it illustrates the problem that I am having. How can I combine two model fields — lat + lon — into a Point object then filter based on that point?


EDIT: adding thing model

class Thing(models.Model):
    lat = models.FloatField()
    lon = models.FloatField()
Nick B
  • 9,267
  • 17
  • 64
  • 105
  • 1
    how is `Thing` model defined ? – karthikr Oct 03 '13 at 19:01
  • Added the relevant model fields above @karthikr. Let me know if I have overlooked any important information and I will add it. Thanks for any ideas that might help! – Nick B Oct 03 '13 at 19:03
  • `Thing.objects.filter(pnt` is expecting `pnt` to be a model attribute. – karthikr Oct 03 '13 at 19:09
  • Thanks! Yeh I understand that but I'm trying to figure out how to do something similar. Do you have any ideas? How does one usually do something like this? I am hoping to keep `lat` and `lon` as the model fields to make it easier to plug into google maps later.. – Nick B Oct 03 '13 at 19:11
  • is that the actual `Thing` model ? – karthikr Oct 03 '13 at 19:12
  • Yes pretty much. There are also `image`, `color`, `slug`, `updated_on`, `created_on`, and `user` fields, but I didn't include them as they seemed irrelevant to the question.. Let me know if I can provide any more information and I will gladly. Thanks for helping with any ideas! – Nick B Oct 03 '13 at 19:17
  • 2
    `z = Thing.objects.filter(lat=pnt.get_x(), lng = pnt.get_y())` – karthikr Oct 03 '13 at 19:43
  • What is the logic behin `__intersects`? You will have to translate that to a query using `lat` and `lon`. – augustomen Oct 03 '13 at 21:11
  • After reading all over the web trying to figure out how to do what I need to do, I came across [this google groups post](https://groups.google.com/forum/#!searchin/geodjango/distance$20search$20geodjango$20hekevintran/geodjango/butW7zJQhSw/4Os60LTdEywJ). Since it was written by Justin Bronn, the "lead developer of geodjango," I considered it a trustworthy source. Please feel free to correct me in the answer section, however, as I am not an authority on the topic. I will definitely upvote a better way of doing this. Thanks for both of your comments! – Nick B Oct 04 '13 at 17:01

1 Answers1

1

The most straightforward way to do this is as @karthikr has said in the comments to your question, just AND the two:

z = Thing.objects.filter(lat=pnt.get_x(), lng = pnt.get_y())

Alternatively, I don't know how much leeway you have in the database, but you could also store the points separately from your Thing object, and then just link the Thing object to a Point?

psuedocode:

class Thing(models.Model):
   point = models.ForeignKey('Point')

class Point(models.Model):
   lat = models.FloatField()
   lon = models.FloatField()


z = Thing.objects.filter(point = Point.objects.get(lat, long))

Otherwise, I don't think there's a way to do what you're asking.

NotSimon
  • 1,795
  • 1
  • 21
  • 31