0

I'm attempting to do a bounding box fetch in the GAE using geomodel in python. It is my understanding that you define a box and then the geomodel fetch will return all results with co-ordinates that lie within this box. I am currently inputting a GPS latitude and longitude (55.497527,-3.114624), and then establishing a bounding box with N,S,E,W within a given range of this co-ordinate like so:

latRange = 1.0
longRange = 0.10
provlat = float(self.request.get('latitude'))
provlon = float(self.request.get('longitude'))
logging.info("Doing proximity lookup")
theBox = geotypes.Box(provlat+latRange, provlon-longRange, provlat-latRange, provlon+longRange)
logging.info("Box created with N:%f E:%f S:%f, W:%f" % (theBox.north, theBox.east, theBox.south, theBox.west))
query = GeoVenue.all().filter('Country =', provcountry)
results = GeoVenue.bounding_box_fetch(query, theBox, max_results=10)
if (len(results) == 0):
    jsonencode = json.dumps([{"error":"no results"}])
    self.response.out.write(jsonencode)
    return;
...

This always returns an empty result set, even though I know for a fact there are results within the range specified in the box logging output :

INFO 2011-07-19 20:45:41,129 main.py:117] Box created with N:56.497527 E:-3.214624 S:54.497527, W:-3.014624

The entries in my datastore include: {"venueLat": 55.9570323, "venueCity": "Edinburgh", "venueZip": "EH1 3AA", "venueLong": -3.1850223, "venueName": "Edinburgh Playhouse", "venueState": "", "venueCountry": "UK"} and {"venueLat": 55.9466506, "venueCity": "Edinburgh", "venueZip": "EH8 9FT", "venueLong": -3.1863224, "venueName": "Festival Theatre Edinburgh", "venueState": "", "venueCountry": "UK"}

Both of which most definitely have positions that are within the bounding box defined above. I have turned debug on and the bounding box fetch does seem to search geocells since I get output along the lines of :

INFO 2011-07-19 20:47:09,487 geomodel.py:114] bbox query looked in 4 geocells

However, no results ever seem to get returned. I have ensured I ran update_location() for all models to make sure the underlying geocell data was correct. Does anyone have any ideas?

Thanks

Sean Bedford
  • 176
  • 9

1 Answers1

0

Code to add to the database -

from google.appengine.ext import db
from models.place import Place

place = Place(location=db.GeoPt(LAT, LON)) # location is a required field 
                                           # LAT, LON are floats
place.state = "New York"
place.zip_code = 10003
#... set other fields
place.update_location() # This is required even when 
                        # you are creating the object and 
                        # not just when you are changing it
place.put()

Code to search nearby objects

base_query = Place.all() # apply appropriate filters if needed
center = geotypes.Point(float(40.658895),float(-74.042760))
max_results = 50
max_distance = 8000

results = Place.proximity_fetch(base_query, center, max_results=max_results,
                                max_distance=max_distance)

It should work with bounding box queries as well, just remember to call update_location before adding the object to the database.

Aman
  • 153
  • 6