Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store lat-lon geolocation point in a document for GAE search?

There where indications in the GoogleIO talk on Search API that we can do searches based on geolocation.

I can't find an appropriate field to store location info.

How can I store geolocation info in the document so I could issue queries based on distance from a particular GPS location?

like image 732
Janusz Skonieczny Avatar asked May 15 '12 15:05

Janusz Skonieczny


1 Answers

On June 28, 2012, Google integrated the GeoPoint class into the Google App Engine Search API library with the specific intent of making spatial points searchable.

GeoPoints are stored as GeoFields within the Search Document. Google provides this support documentation outlining the use of the GeoPoint with the Search API.

The following example declares a GeoPoint and assigns it to a GeoField in a Search Document. These new classes provide a lot more functionality than what is listed below, but this code is a starting point for a basic understanding of how to use the new spatial search functionality..

Constructing a document with an associated GeoPoint

## IMPORTS ##
from google.appengine.api import search

def CreateDocument(content, lat, long):
  geopoint = search.GeoPoint(lat, long)
  return search.Document(
    fields=[
            search.HtmlField(name='content', value=content),
            search.DateField(name='date', value=datetime.now().date())
            search.GeoField(name='location', value=geopoint)
           ])

Searching the GeoPoint document field (Slightly modified from the Search API docs)

## IMPORTS ##
from google.appengine.api import search

ndx = search.Index(DOCUMENT_INDEX)
loc = (-33.857, 151.215)

query = "distance(location, geopoint(-33.857, 151.215)) < 4500"

loc_expr = "distance(location, geopoint(-33.857, 151.215))"

sortexpr = search.SortExpression(
  expression=loc_expr,
  direction=search.SortExpression.ASCENDING, default_value=4501)

search_query = search.Query(
  query_string=query,
  options=search.QueryOptions(
    sort_options=search.SortOptions(expressions=[sortexpr])))

results = index.search(search_query)
like image 136
RLH Avatar answered Nov 14 '22 13:11

RLH