def test_center(self): nwcorner = Point(0, 0) secorner = Point(10, 0) bb=BoundingBox(nwcorner,secorner) center=bb.center() self.assertEqual(center.lat,0) self.assertEqual(float(truncate(center.lng,DEGREE_DIGITS)),5) nwcorner = Point(0, 10) secorner = Point(10, 0) bb=BoundingBox(nwcorner,secorner) center=bb.center() self.assertEqual(float(truncate(center.lat,DEGREE_DIGITS)),5.0190007) self.assertEqual(float(truncate(center.lng,DEGREE_DIGITS)),5.0383688)
def test_center(self): nwcorner = Point(0, 0) secorner = Point(10, 0) bb = BoundingBox(nwcorner, secorner) center = bb.center() self.assertEqual(center.lat, 0) self.assertEqual(float(truncate(center.lng, DEGREE_DIGITS)), 5) nwcorner = Point(0, 10) secorner = Point(10, 0) bb = BoundingBox(nwcorner, secorner) center = bb.center() self.assertEqual(float(truncate(center.lat, DEGREE_DIGITS)), 5.0190007) self.assertEqual(float(truncate(center.lng, DEGREE_DIGITS)), 5.0383688)
def test_intersection(self): bb0=BoundingBox.create(0,10,10,0) bb1=BoundingBox.create(5,15,15,5) bb2=BoundingBox.create(7,8,8,7) bb_list = [bb0,bb1,bb2] i = BoundingBox.intersect_all(bb_list) self.assertEqual(i.nw.get_lng(), 7) self.assertEqual(i.nw.get_lat(), 8) self.assertEqual(i.se.get_lng(), 8) self.assertEqual(i.se.get_lat(), 7) print 'nw: %s se: %s' % (i.nw, i.se) nwcorner = Point(0, 10) secorner = Point(10, 0) bb1=BoundingBox(nwcorner,secorner) nwcorner = Point(5, 15) secorner = Point(15, 5) bb2=BoundingBox(nwcorner,secorner) i = bb1.intersection(bb2) self.assertEqual(i.nw.get_lng(), 5) self.assertEqual(i.nw.get_lat(), 10) self.assertEqual(i.se.get_lng(), 10) self.assertEqual(i.se.get_lat(), 5) print 'nw: %s se: %s' % (i.nw, i.se) nwcorner = Point(170, 10) secorner = Point(-170, -10) bb1=BoundingBox(nwcorner,secorner) nwcorner = Point(-175, 5) secorner = Point(-165, -5) bb2=BoundingBox(nwcorner,secorner) i = bb1.intersection(bb2) self.assertEqual(i.nw.get_lng(), -175) self.assertEqual(i.nw.get_lat(), 5) self.assertEqual(i.se.get_lng(), -170) self.assertEqual(i.se.get_lat(), -5) print 'nw: %s se: %s' % (i.nw, i.se)
def get(self): keywords = [] place = self.request.get('place', None) category = self.request.get('type', None) source = self.request.get('source', None) limit = self.request.get_range('limit', min_value=1, max_value=100, default=10) offset = self.request.get_range('offset', min_value=0, default=0) if place: # Search Feature on id where id is each name in palce (names comma separated) features = Feature.search(place) logging.info('FEATURES=%s' % [f.key for f in features]) n = len(features) results = [] if n == 1: # Exact match on one Feature results.append(simplejson.loads(features[0].j)) elif n > 1: # Exact match on multiple Features bboxes = [] for feature in features: data = simplejson.loads(feature.j) bboxes.append( BoundingBox.create( data['minx'], data['maxy'], data['maxx'], data['miny'])) if BoundingBox.intersect_all(bboxes): # Return all features with intersection=True results = dict( features=[simplejson.loads(feature.j) for feature in features], intersection=True) else: # Return all features with intersection=False results = dict( features=[simplejson.loads(feature.j) for feature in features], intersection=False) if len(results) > 0: # If exact results return them self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(results)) return # Search FeatureIndex on keywords derived from each place name if place: results = set() search_results = FeatureIndex.search_place_keywords(place) if len(search_results) == 1: # Keyword FeatureIndex hit on single name name, features = search_results.popitem() results = [simplejson.loads(feature.j) for feature in features] self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(results)) return # Perform cross-product intersection tests and return all matching pairs for name,features in search_results.iteritems(): for feature in features: for other_name, other_features in search_results.iteritems(): if name == other_name: continue data = simplejson.loads(feature.j) fbbox = BoundingBox.create(data['minx'], data['maxy'], data['maxx'], data['miny']) for other_feature in other_features: data = simplejson.loads(other_feature.j) obbox = BoundingBox.create(data['minx'], data['maxy'], data['maxx'], data['miny']) logging.info('feature=%s, other_feature=%s, fbbox=%s, obbox=%s' % (feature, other_feature, fbbox, obbox)) if BoundingBox.intersect_all([fbbox, obbox]): results.update([feature, other_feature]) self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(list(results)))
def test_intersection(self): bb0 = BoundingBox.create(0, 10, 10, 0) bb1 = BoundingBox.create(5, 15, 15, 5) bb2 = BoundingBox.create(7, 8, 8, 7) bb_list = [bb0, bb1, bb2] i = BoundingBox.intersect_all(bb_list) self.assertEqual(i.nw.get_lng(), 7) self.assertEqual(i.nw.get_lat(), 8) self.assertEqual(i.se.get_lng(), 8) self.assertEqual(i.se.get_lat(), 7) print 'nw: %s se: %s' % (i.nw, i.se) nwcorner = Point(0, 10) secorner = Point(10, 0) bb1 = BoundingBox(nwcorner, secorner) nwcorner = Point(5, 15) secorner = Point(15, 5) bb2 = BoundingBox(nwcorner, secorner) i = bb1.intersection(bb2) self.assertEqual(i.nw.get_lng(), 5) self.assertEqual(i.nw.get_lat(), 10) self.assertEqual(i.se.get_lng(), 10) self.assertEqual(i.se.get_lat(), 5) print 'nw: %s se: %s' % (i.nw, i.se) nwcorner = Point(170, 10) secorner = Point(-170, -10) bb1 = BoundingBox(nwcorner, secorner) nwcorner = Point(-175, 5) secorner = Point(-165, -5) bb2 = BoundingBox(nwcorner, secorner) i = bb1.intersection(bb2) self.assertEqual(i.nw.get_lng(), -175) self.assertEqual(i.nw.get_lat(), 5) self.assertEqual(i.se.get_lng(), -170) self.assertEqual(i.se.get_lat(), -5) print 'nw: %s se: %s' % (i.nw, i.se)