def test_random(self): # Create 100 random nodes nodes = NodeSet() for i in range(100): nodes.add(i, (random.randint(-180, 180), random.randint(-90, 90))) # Randomly choose the min and max latitude and longitude min_lat = random.randint(-90, 90) min_lng = random.randint(-180, 180) max_lat = min_lat + random.randint(0, 90-min_lat) max_lng = min_lng + random.randint(0, 180-min_lng) # Extract all nodes within the randomly generated rectangle selected_nodes = select_nodes_in_rectangle(nodes, min_lat, max_lat, min_lng, max_lng) # Make sure that all nodes that should be inside are inside for n in nodes.nodes.values(): if n.id in selected_nodes.nodes.keys(): self.assertTrue(node_inside(n, min_lat, max_lat, min_lng, max_lng)) else: self.assertFalse(node_inside(n, min_lat, max_lat, min_lng, max_lng))
def test_empty_clip_nodes(self): nodes = NodeSet() nodes.add(1, (-10, -10)) nodes.add(2, (-5, -5)) selected_nodes = select_nodes_in_rectangle(nodes, 0, 10, 0, 10) self.assertFalse(selected_nodes.nodes)
def test_all_nodes_inside(self): nodes = NodeSet() nodes.add(1, (-10, -10)) nodes.add(2, (-5, -5)) selected_nodes = select_nodes_in_rectangle(nodes, -10, 10, -10, 10) self.assertTrue(nodes.nodes.keys() == selected_nodes.nodes.keys())
def select_nodes_in_rectangle(nodes, min_lat, max_lat, min_lng, max_lng): # actual min and max latitude and longitude of coordinates #if needed change <= to < nodes_in_rectangle = NodeSet() for k, node in nodes.get_nodes().items(): if (min_lat <= node.lat and node.lat <= max_lat and min_lng <= node.lng and node.lng <= max_lng): nodes_in_rectangle.add(k, node.lat, node.lng) return nodes_in_rectangle
def extract_osm_nodes(f_name): # Parse the supplied OSM file print("Loading data...") parser = get_default_parser(f_name) node_set = NodeSet() for node in parser.iter_nodes(): node_set.add(node['id'], node['lat'], node['lon']) return node_set
def test_all_nodes_inside(self): nodes = NodeSet() nodes.add(1, -10, -10) nodes.add(2, -5, -5) selected_nodes = select_nodes_in_rectangle(nodes, -10, 10, -10, 10) self.assertTrue( nodes.get_nodes().keys() == selected_nodes.get_nodes().keys())
def test_some_nodes_inside(self): nodes = NodeSet() nodes.add(1, -10, -10) nodes.add(2, -5, -5) selected_nodes = select_nodes_in_rectangle(nodes, -10, 10, -10, 10) self.assertTrue( any(x in selected_nodes.get_nodes().keys() for x in nodes.get_nodes().keys()))
def test_some_nodes_inside(self): nodes = NodeSet() nodes.add(1, 8, 12) nodes.add(2, 10, 15) selected_nodes = select_nodes_in_rectangle(nodes, 4, 9, 8, 14) self.assertTrue(selected_nodes.nodes)
def test_empty_clip_nodes(self): nodes = NodeSet() nodes.add(1, -10, -10) nodes.add(2, -5, -5) selected_nodes = select_nodes_in_rectangle(nodes, 0, 10, 0, 10) self.assertFalse(selected_nodes.nodes)
def test_random(self): #create 100 random nodes nodes = NodeSet() for i in range(100): nodes.add(i, random.randint(-180, 180), random.randint(-90, 90)) #random min and max lat and long min_lat = random.randint(-90, 90) min_lng = random.randint(-180, 180) max_lat = min_lat + random.randint(0, 90 - min_lat) max_lng = min_lng + random.randint(0, 180 - min_lng) selected_nodes = select_nodes_in_rectangle(nodes, min_lat, max_lat, min_lng, max_lng) #test that all nodes are inside for n in nodes.nodes.values(): if n.id in selected_nodes.nodes.keys(): self.assertTrue( node_inside(n, min_lat, max_lat, min_lng, max_lng)) else: self.assertFalse( node_inside(n, min_lat, max_lat, min_lng, max_lng))
def test_random(self): # Create 100 random nodes nodes = NodeSet() for i in range(100): nodes.add(i, random.randint(-180, 180), random.randint(-90, 90)) # Randomly choose the min and max latitude and longitude min_lat = random.randint(-90, 90) min_lng = random.randint(-180, 180) max_lat = min_lat + random.randint(0, 90 - min_lat) max_lng = min_lng + random.randint(0, 180 - min_lng) # Extract all nodes within the randomly generated rectangle selected_nodes = select_nodes_in_rectangle(nodes, min_lat, max_lat, min_lng, max_lng) # Make sure that all nodes that should be inside are inside for n in nodes.nodes.values(): if n.id in selected_nodes.nodes.keys(): self.assertTrue( node_inside(n, min_lat, max_lat, min_lng, max_lng)) else: self.assertFalse( node_inside(n, min_lat, max_lat, min_lng, max_lng))
def test_empty_nodes(self): nodes = NodeSet() selected_nodes = select_nodes_in_rectangle(nodes, -10, 10, -10, 10) self.assertEqual(nodes.nodes, selected_nodes.nodes)
def test_rand_cases(self): nodes = NodeSet() nodes.add(1, random.randint(-90, 90), random.randint(-180, 180)) nodes.add(2, random.randint(-90, 90), random.randint(-180, 180)) for k, n in nodes.get_nodes().items(): self.assertTrue(-90 <= n.lat <= 90 and -180 <= n.lng <= 180)
def test_edge_bounds(self): nodes = NodeSet() nodes.add(1, -90, -180) nodes.add(2, 90, 180) for k, n in nodes.get_nodes().items(): self.assertTrue(-90 <= n.lat <= 90 and -180 <= n.lng <= 180)