def test_relation_spatial_index(self): atlas = Atlas("resources/test.atlas") lower_left = geometry.location_with_degrees(37.999, -118.001) upper_right = geometry.location_with_degrees(38.001, -117.999) test_results = atlas.relations_with_entities_intersecting( Rectangle(lower_left, upper_right)) self.assertEqual({atlas.relation(2)}, test_results)
def test_entity_bounding_calculation_on_relations(self): atlas = Atlas("resources/test.atlas") relation = atlas.relation(1) expected_rect = Rectangle( Location(390000000, -1190300000), Location(390500000, -1180000000)) computed_rect = geometry.bounds_atlasentities([relation]) self.assertEqual(expected_rect, computed_rect) relation = atlas.relation(2) expected_rect = Rectangle( Location(380000000, -1180100000), Location(380100000, -1180000000)) computed_rect = geometry.bounds_atlasentities([relation]) self.assertEqual(expected_rect, computed_rect)
def test_edge_spatial_index(self): atlas = Atlas("resources/test.atlas") test_location = geometry.location_with_degrees(39, -119.05) test_results = atlas.edges_containing(test_location) self.assertEqual({atlas.edge(1), atlas.edge(3)}, test_results) poly = Rectangle( geometry.location_with_degrees(38, -120), geometry.location_with_degrees(40, -117)) test_results = atlas.edges_intersecting(poly) self.assertEqual({atlas.edge(1), atlas.edge(2), atlas.edge(3)}, test_results)
def test_rtree(self): # The bounding box defined in this test should only encompass # test Points 1, 2, and 3 from the test atlas atlas = Atlas("resources/test.atlas") tree = _RTree(atlas.points()) lower_left = geometry.location_with_degrees(37, -118.02) upper_right = geometry.location_with_degrees(39, -118) test_results = [] for element in tree.get(Rectangle(lower_left, upper_right)): test_results.append(element) self.assertEqual({1, 2, 3}, set(test_results))
def test_area_spatial_index(self): atlas = Atlas("resources/test.atlas") test_location = geometry.location_with_degrees(38.15, -118.03) test_results = atlas.areas_covering(test_location) self.assertEqual({atlas.area(2)}, test_results) test_results = atlas.areas_intersecting(atlas.area(2).as_polygon()) self.assertEqual({atlas.area(1), atlas.area(2)}, test_results)
def test_upfront_loading(self): atlas = Atlas("resources/test.atlas", lazy_loading=False) _touch_all_atlas_features(atlas) self.assertEqual(atlas.number_of_points(), 5) self.assertEqual(atlas.number_of_lines(), 2) self.assertEqual(atlas.number_of_areas(), 2) self.assertEqual(atlas.number_of_nodes(), 4) self.assertEqual(atlas.number_of_edges(), 3) self.assertEqual(atlas.number_of_relations(), 2)
def test_point_spatial_index(self): atlas = Atlas("resources/test.atlas") lower_left = geometry.location_with_degrees(37, -118.02) upper_right = geometry.location_with_degrees(39, -118) # NOTE point 1 does not show up in the results because it lies on the polygon border test_results = atlas.points_within(Rectangle(lower_left, upper_right)) self.assertEqual({atlas.point(2), atlas.point(3)}, test_results) test_results = atlas.points_within( Rectangle(lower_left, upper_right), lambda p: p.get_identifier() % 2 != 0) self.assertEqual({atlas.point(3)}, test_results) test_results = atlas.points_at(geometry.location_with_degrees(38, -118)) self.assertEqual({atlas.point(1)}, test_results)
def test_line_spatial_index(self): atlas = Atlas("resources/test.atlas") test_location = geometry.location_with_degrees(38.02, -118.02) test_results = atlas.lines_containing(test_location) self.assertEqual({atlas.line(1)}, test_results) poly = Rectangle( geometry.location_with_degrees(38, -118), geometry.location_with_degrees(39, -119)) test_results = atlas.lines_intersecting(poly) self.assertEqual({atlas.line(1), atlas.line(2)}, test_results)
def test_node_spatial_index(self): atlas = Atlas("resources/test.atlas") lower_left = geometry.location_with_degrees(39, -119.04) upper_right = geometry.location_with_degrees(39.05, -119) # NOTE node 4 does not show up in results because it lies on the the polygon border test_results = atlas.nodes_within(Rectangle(lower_left, upper_right)) self.assertEqual({atlas.node(2)}, test_results) test_results = atlas.nodes_within( Rectangle(lower_left, upper_right), lambda n: n.get_identifier() == 3) self.assertEqual(frozenset(), test_results) test_results = atlas.nodes_at(geometry.location_with_degrees(39, -119.05)) self.assertEqual({atlas.node(3)}, test_results)
def test_basic_spatial_index_operations(self): atlas = Atlas("resources/test.atlas") index = SpatialIndex(atlas, EntityType.POINT, atlas.points()) index.initialize_rtree() lower_left = geometry.location_with_degrees(37, -118.02) upper_right = geometry.location_with_degrees(39, -118) test_results = index.get(Rectangle(lower_left, upper_right)) self.assertEqual( {atlas.point(2), atlas.point(3), atlas.point(1)}, test_results) test_results = index.get(Rectangle(lower_left, upper_right), lambda p: p.get_identifier() == 2) self.assertEqual({atlas.point(2)}, test_results)