def test_area_inserted_touching_left_right(): area_1 = Area(10, 20, 30, 'Area 1') area_2 = Area(50, 20, 10, 'Area 2') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) main_range.add(area_2, area_2.min_lat, area_2.max_lat) assert main_range.space == [-20, 40, 60] assert main_range.areas == {(-20, 40): {area_1}, (40, 60): {area_2}}
def test_area_inserted_aligns(): area_1 = Area(10, 20, 30, 'Area 1') area_2 = Area(10, 20, 30, 'Area 2') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) main_range.add(area_2, area_2.min_lat, area_2.max_lat) assert main_range.space == [-20, 40] assert main_range.areas == {(-20, 40): {area_1, area_2}}
def test_area_inserted_after(): area_1 = Area(10, 20, 30, 'Area 1') area_2 = Area(50, 20, 5, 'Area 2') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) main_range.add(area_2, area_2.min_lat, area_2.max_lat) assert main_range.space == [-20, 40, 45, 55] assert main_range.areas == {(-20, 40): {area_1}, (45, 55): {area_2}}
def test_area_inserted_before(): area_1 = Area(10, 20, 30, 'Area 1') area_2 = Area(-30, 20, 5, 'Area 2') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) main_range.add(area_2, area_2.min_lat, area_2.max_lat) assert main_range.space == [-35, -25, -20, 40] assert main_range.areas == {(-20, 40): {area_1}, (-35, -25): {area_2}}
def test_area_inserted_outside(): area_1 = Area(10, 20, 30, 'Area 1') area_2 = Area(10, 20, 40, 'Area 2') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) main_range.add(area_2, area_2.min_lat, area_2.max_lat) assert main_range.space == [-30, -20, 40, 50] assert main_range.areas == { (-30, -20): {area_2}, (-20, 40): {area_1, area_2}, (40, 50): {area_2} }
def test_area_inserted_crossing_on_the_right(): area_1 = Area(10, 20, 30, 'Area 1') area_2 = Area(30, 20, 20, 'Area 2') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) main_range.add(area_2, area_2.min_lat, area_2.max_lat) assert main_range.space == [-20, 10, 40, 50] assert main_range.areas == { (-20, 10): {area_1}, (10, 40): {area_1, area_2}, (40, 50): {area_2} }
def test_area_inserted_crossing_on_the_left(): area_1 = Area(10, 20, 30, 'Area 1') area_2 = Area(-30, 20, 20, 'Area 2') main_range = Range(area_1, area_1.min_lat, area_1.max_lat) main_range.add(area_2, area_2.min_lat, area_2.max_lat) assert main_range.space == [-50, -20, -10, 40] assert main_range.areas == { (-50, -20): {area_2}, (-20, -10): {area_1, area_2}, (-10, 40): {area_1} }
def test_area_inserted_between(): left = Area(-20, 20, 10, 'Left') right = Area(20, 20, 10, 'Right') middle = Area(0, 20, 5, 'Middle') main_range = Range(left, left.min_lat, left.max_lat) main_range.add(right, right.min_lat, right.max_lat) main_range.add(middle, middle.min_lat, middle.max_lat) assert main_range.space == [-30, -10, -5, 5, 10, 30] assert main_range.areas == { (-30, -10): {left}, (-10, -5): set(), (-5, 5): {middle}, (5, 10): set(), (10, 30): {right} }
def test_area_inserted_crossing_on_both_sides_and_the_middle(): 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.space == [-50, -20, -10, 10, 20, 50] assert main_range.areas == { (-50, -20): {left_cross}, (-20, -10): {main_area, left_cross}, (-10, 10): {main_area, left_cross, right_cross}, (10, 20): {main_area, right_cross}, (20, 50): {right_cross} }
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}
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_area_inserted_touching_on_all_sides(): main_area = Area(0, 20, 10, 'Area 1') left = Area(-20, 20, 10, 'Area 2') left_right = Area(-6, 20, 4, 'Area 3') right_left = Area(6, 20, 4, 'Area 4') right = Area(20, 20, 10, 'Area 5') main_range = Range(main_area, main_area.min_lat, main_area.max_lat) main_range.add(left, left.min_lat, left.max_lat) main_range.add(left_right, left_right.min_lat, left_right.max_lat) main_range.add(right_left, right_left.min_lat, right_left.max_lat) main_range.add(right, right.min_lat, right.max_lat) assert main_range.space == [-30, -10, -2, 2, 10, 30] assert main_range.areas == { (-30, -10): {left}, (-10, -2): {main_area, left_right}, (-2, 2): {main_area}, (2, 10): {main_area, right_left}, (10, 30): {right} }