Skip to content
phughes edited this page Sep 14, 2010 · 3 revisions

Using geobox2.py:

To compute the set of surrounding geoboxes simply initialize a new geobox object with the desired latitude and longitude, and request the list of geobox strings surrounding that point.

gb = geobox2.Geobox(latitude, longitude)@
#
geobox_list = gb.storage_geoboxes()
# geobox_list gets stored in a string list property of your model
my_instance.geoboxes = geobox_list
my_instance.put()

Once this info is stored in the the database, searching is similarly simple. Initialize a geobox object with the desired coordinates, and request the (single) geobox string with the span of your map as a parameter.

gb = geobox2.Geobox(latitude, longitude)
box = gb.search_geobox(span)  
#
query = MyGeoSearchableModelObject.all().filter("geoboxes = ", box)
results = query.fetch(50)

Customization:
While the implementation can be used as is, there are a few properties you may want to change.

SCOPE_SIZES = [decimal.Decimal('0.00625'), decimal.Decimal('0.0125'), decimal.Decimal('0.025'), decimal.Decimal('0.05')]

Currently the SCOPE_SIZES variable spans from about a block to about 4km (or 2 miles).The sizes should be in order from smallest to largest. They are Change as you see fit.

NUM_PLACES = decimal.Decimal('1.000') # use 3 decimal places

The NUM_PLACES variable determines the number of decimal places will be used in the geobox. Unless you need extremely high accuracy I don’t see a need to increase this. It may be advisable to reduce the number of decimal places if your locations are far apart.

MARGIN = decimal.Decimal('0.3')

The MARGIN variable determines how close to the edge of the bounding box a point has to be before it’s neighboring bounding box is also added to the list. A value of 0.3 means one third of the scope away from each edge will trigger adjacent bounding boxes. It wouldn’t be advisable to increase this much, since at 0.5 we get the greatest number of results (the box, plus all 8 adjacent boxes) when we need it least (when we are directly in the center of our box.)

License:
This code is licensed under a BSD License. You may use it as you see fit.

Clone this wiki locally