def get_nearest_port(nodes, lat, lon): min_dist = sys.maxint port = None for node in nodes: dist = geo.latlon_distance(node['lat'], node['lon'], lat, lon) if dist < min_dist: min_dist = dist port = node return port
def merge_nodes(nodes): # FIXME: This is wrong. The remove needs to redirect parents to the new node, but not children node_lookup = {} for node in nodes: node_lookup[node['node']] = node while(True): to_del = [] for nodeA_id in node_lookup: nodeA = node_lookup[nodeA_id] for nodeB_id in node_lookup: if nodeA_id == nodeB_id: continue nodeB = node_lookup[nodeB_id] if geo.latlon_distance(nodeA['lat'], nodeA['lon'], nodeB['lat'], nodeB['lon']) < 100: # Remove from node a if nodeB_id in nodeA['edges']: nodeA['edges'].remove(nodeB_id) # Copy node b's edges from node a for edge in nodeB['edges']: if edge not in nodeA['edges']: nodeA['edges'].append(edge) # Find all nodes that have edge to node b for node_id in node_lookup: if node_id == nodeA_id or node_id == nodeB_id: continue node = node_lookup[node_id] # check if the node has edge to node b if nodeB_id in node['edges']: # Remove the edge to node b and replace with node a node['edges'].remove(nodeB_id) if nodeA_id not in node['edges']: node['edges'].append(nodeA_id) to_del.append(nodeB_id) if len(to_del) > 0: break if len(to_del) == 0: break for node_id in to_del: if node_id in node_lookup: print('removing node %s' % node_id) del node_lookup[node_id] result = [] for node_id in node_lookup: result.append(node_lookup[node_id]) return result
def _get_distance(nodes, from_n, to_n): from_node = nodes[from_n] to_node = nodes[to_n] return geo.latlon_distance(from_node['lat'], from_node['lon'], to_node['lat'], to_node['lon'])