class RangeBasedAreaIndex(BaseAreaIndex): def __init__(self): self.latitudes = None self.longitudes = None def bulk_create(self, areas): """ :param list(tuple) areas: area description should contain latitude, longitude, radius and id. >>> RangeBasedAreaIndex().bulk_create([(-24.5, 134.8, 20, 'Australia')]) """ areas = self._create_areas(areas) first_area = areas.pop() self.latitudes = Range(first_area, first_area.min_lat, first_area.max_lat) self.longitudes = Range(first_area, first_area.min_long, first_area.max_long) for area in areas: self.latitudes.add(area, area.min_lat, area.max_lat) self.longitudes.add(area, area.min_long, area.max_long) def query(self, lat, long): """ :param float lat: :param float long: :return: all areas that include given point. :rtype: set(Area) >>> RangeBasedAreaIndex().query(0.0, 0.0) set() """ if self.longitudes and self.latitudes: possible_areas = self.latitudes.search(lat).union( self.longitudes.search(long)) return self._find_areas(possible_areas, lat, long) else: return set()
def test_query_found_in_multiple_areas(): main_area = Area(0, 20, 20, 'Main') left_cross = Area(-20, 20, 30, 'Left Cross') right_cross = Area(20, 20, 30, 'Right Cross') main_range = Range(main_area, main_area.min_lat, main_area.max_lat) main_range.add(left_cross, left_cross.min_lat, left_cross.max_lat) main_range.add(right_cross, right_cross.min_lat, right_cross.max_lat) assert main_range.search(-50) == {left_cross} assert main_range.search(-40) == {left_cross} assert main_range.search(-20) == {main_area, left_cross} assert main_range.search(-15) == {main_area, left_cross} assert main_range.search(-10) == {main_area, left_cross, right_cross} assert main_range.search(0) == {main_area, left_cross, right_cross} assert main_range.search(10) == {main_area, left_cross, right_cross} assert main_range.search(15) == {main_area, right_cross} assert main_range.search(20) == {main_area, right_cross} assert main_range.search(40) == {right_cross} assert main_range.search(50) == {right_cross}
def test_query_found_in_one_area(): area_1 = Area(0, 20, 10, 'Area 1') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) assert main_range.search(0) == {area_1} assert main_range.search(-10) == {area_1} assert main_range.search(10) == {area_1}
def test_query_not_found(): area_1 = Area(0, 20, 10, 'Area 1') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) assert main_range.search(-11) == [] assert main_range.search(11) == []