Skip to content

Commit

Permalink
Implemented foh interpretation and supporting dictionaries for headin…
Browse files Browse the repository at this point in the history
…gs and distance units.
  • Loading branch information
tucotuco committed Jun 4, 2011
1 parent 8b3a12c commit 7f765b9
Show file tree
Hide file tree
Showing 4 changed files with 647 additions and 199 deletions.
52 changes: 37 additions & 15 deletions python/app/app.py
Expand Up @@ -17,7 +17,6 @@
import geomancer
from geomancer import Locality


class GoogleGeocodeApi(object):

@classmethod
Expand Down Expand Up @@ -148,6 +147,7 @@ def predict(cls, query):
if not cls.AUTH:
cls.AUTH = GooglePredictionApi.GetAuthentication('eightysteele@gmail.com', '')
model = 'biogeomancer/locs.csv'
# model = 'tuco-geomancer/CALocsForPrediction.csv'
results = GooglePredictionApi.Predict(cls.AUTH, model, query)
memcache.add(mkey, results)
return results
Expand All @@ -174,10 +174,11 @@ def post(self):
self.response.out.write('Unable to georeference %s (%s)' % (q, geocode.get('status')))
return
if GoogleGeocodeApi.ispartialmatch(geocode.get('results')[0]):
# partial match requires prediction and further processing
loctype = LocalityTypeApi.predict(q)[0]
if loctype == 'foh':
meters = ['m', 'm.', 'meter', 'meters', 'mts', 'mts.', 'metre', 'metres']
west = ['w', 'w.', 'west', 'western', 'w 1/2']
# meters = ['m', 'm.', 'meter', 'meters', 'mts', 'mts.', 'metre', 'metres']
# west = ['w', 'w.', 'west', 'western', 'w 1/2']
tokens = [x.strip().lower() for x in q.split() if x not in ['of']]

offsetunit = None
Expand All @@ -187,18 +188,37 @@ def post(self):

for token in tokens:
if token.isdigit():
offsetval = float(token)
# TODO: fails for tokens that are distances as words (five).
# and tokens that consist of mixed numbers (5 1/2)
offsetval = token
continue
if geomancer.get_unit(token):
# TODO: fails for tokens that consist of more than one word (nautical miles).
offsetunit = token
continue
if geomancer.get_heading(token):
# TODO: fails for tokens that look like directions but are part of the name (South Haven).
heading = token
continue
for unit in meters:
logging.info('%s=%s'%(token, unit))
if token == unit:
offsetunit = 'meter'
continue
for direction in west:
if token == direction:
heading = 'west'
continue
# TODO: Fails for features consisting of more than one word (Los Altos)
feature = token
# If the the component parts are all here, can't georeference.
if not offsetunit:
self.error(400)
self.response.out.write('Unable to georeference %s as %s, missing offset unit' % (q, loctype) )
return
if not offsetval:
self.error(400)
self.response.out.write('Unable to georeference %s as %s, missing offset value' % (q, loctype) )
return
if not heading:
self.error(400)
self.response.out.write('Unable to georeference %s as %s, missing heading value' % (q, loctype) )
return
if not feature:
self.error(400)
self.response.out.write('Unable to georeference %s as %s, missing feature value' % (q, loctype) )
return
parts = {
'locality': q,
'locality_type': loctype,
Expand All @@ -210,15 +230,17 @@ def post(self):
'geocode': GoogleGeocodeApi.execute(feature)
}
}
logging.info('parts: %s'%(parts))
locality = Locality(q, loctype=loctype, parts=parts, geocode=geocode)
georef = geomancer.georeference(locality)
logging.info('georef: %s'%(georef))
result = {
'interpretation': parts,
'georeference':
{
'error':georef.error,
'lat':georef.point.lat,
'lng':georef.point.lng
'lat':georef.point[1],
'lng':georef.point[0]
}
}
memcache.add(mkey, result)
Expand Down
222 changes: 222 additions & 0 deletions python/app/geomancer-test/__init__.py
Expand Up @@ -7,13 +7,235 @@
import geomancer
import logging
import unittest
import simplejson

class TestGeomancer(unittest.TestCase):
def test_headings(self):
headings = geomancer.constants.Headings.all()
for heading in headings:
logging.info(heading)

def test_datums(self):
datums = geomancer.constants.Datums.all()
for datum in datums:
logging.info(datum)

def test_georef(self):
geocode = simplejson.loads("""{
"results" : [
{
"address_components" : [
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "San Jose",
"short_name" : "San Jose",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Santa Clara",
"short_name" : "Santa Clara",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Mountain View, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.4698870,
"lng" : -122.0446720
},
"southwest" : {
"lat" : 37.35654100000001,
"lng" : -122.1178620
}
},
"location" : {
"lat" : 37.38605170,
"lng" : -122.08385110
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.42150620,
"lng" : -122.01982140
},
"southwest" : {
"lat" : 37.35058040,
"lng" : -122.14788080
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}""")
logging.info(georeference(geocode))

def test_point2wgs84(self):
agd66point = Point(144.966666667, -37.8)
wgs84point = point2wgs84(agd66point, Datums.AGD84)
logging.info(wgs84point)
logging.info(Datums.AGD84)
# 144.96797984155188, -37.798491994062296
# 144.96798640000000, -37.798480400000000

def test_distanceprecision(self):
d = '110'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))


d = '0'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))

d = '1'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '2'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '5'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '9'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '11'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '12'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '19'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '20'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '21'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '49'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '50'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '51'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '99'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '100'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '101'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '109'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '110'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '111'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '149'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '150'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '151'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '199'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '200'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '201'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '210'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '999'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '1000'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))


d = '10.000'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.001'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.00'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.01'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.0'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.1'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.9'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.125'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.25'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.3333'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '10.5'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '0.625'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '0.75'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '0.5'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))
d = '0.66667'
p = getDistancePrecision(d)
logging.info(d+" "+str(p))

if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
unittest.main()
Expand Down

0 comments on commit 7f765b9

Please sign in to comment.