def get_initial_node(self, location): closest = -1 dist = sys.float_info.max for n in self.map.nodes(): coord_n = osmgraph.tools.coordinates(self.map, [n])[0] if geog.distance(coord_n, location) < dist: dist = geog.distance(coord_n, location) closest = n return closest
def import_file(self, path): self.map = osmgraph.parse_file(path) # add distances for n1, n2 in self.map.edges(): c1, c2 = osmgraph.tools.coordinates(self.map, (n1, n2)) self.map[n1][n2]['length'] = geog.distance(c1, c2)
def summary_weather_report(results, daily_df, current_df, reporting_location, actual_hourly) -> str: summary = '' today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) utoday = datetime.timestamp(today) current = current_df.iloc[0].to_dict() daily = daily_df.iloc[0].to_dict() del daily["sunrise"] del daily["sunset"] summary += f'Current Conditions {current["dt"]}\n{conditions_summary(current)}\n' summary += f'Forecast {current["dt"]}\n{conditions_summary(daily)}\n' for hr in [7, 10, 13, 16]: # 7,8,11,12,15,16 dt_str = today.replace(hour=hr, minute=0, second=0, microsecond=0).strftime('%Y-%m-%d %H:%M:%S') df = actual_hourly[actual_hourly.dt == dt_str] if df.shape[0] > 0: summary += f'Conditions at {dt_str}\n' summary += conditions_summary(df.iloc[0].to_dict()) summary += '\n' weather_station_location = (results["lat"], results["lon"]) # 1.0001*lat to move a bit summary += f'Weather station location: {weather_station_location}\n' summary += f'Reporting location : {reporting_location}\n' # distance in meters dist_to_station_m = geog.distance(weather_station_location, reporting_location) dist_to_station = meters_to_miles(dist_to_station_m) summary += f'Reporting location is {dist_to_station:.2f} miles from weather station\n' return summary
def test_distance_n_to_n(): points0 = [locs['bos'], locs['dc'], locs['la']] points1 = [locs['dc'], locs['la'], locs['bos']] result = geog.distance(points0, points1) expected = [_d['bos']['dc'], _d['dc']['la'], _d['la']['bos']] assert np.allclose(result, expected)
def test_distance_one(): result = geog.distance(locs['bos'], locs['la']) # Make sure we didn't get back an array with pytest.raises(TypeError): len(result) expected = _d['bos']['la'] assert np.allclose(result, expected)
def make_graphs(filenames, purge_func=default_purge): graphs = [osmgraph.parse_file(filename).to_undirected() for filename in filenames] accumulator = networkx.empty_graph() for graph in graphs: accumulator = networkx.compose(accumulator, graph) pure_g = accumulator names = make_node_dict(pure_g) for n1 in pure_g: pure_g.node[n1]['pretty_name'] = names[n1] # Assign lengths to all edges, even the ones we are going to purge. for n1, n2 in pure_g.edges_iter(): c1, c2 = osmgraph.tools.coordinates(pure_g, (n1, n2)) pure_g[n1][n2]['length'] = geog.distance(c1, c2) # Never modify pure_g again. g = pure_g.copy() purge_func(g) all_edge_names = set([g[n1][n2].get('name') for (n1, n2) in g.edges()]) print "DEBUG: We are left with these %s streets: %s" % (len(all_edge_names), sorted(all_edge_names) ) # If a node has been isolated, just remove it for efficiency. for n1 in list(g.nodes()): if len(g[n1]) == 0: g.remove_node(n1) # assert(networkx.is_connected(g)) # It's inefficient to do this twice but we kind of need it earlier for # debugging, but the names are prettier after purging. names = make_node_dict(g) for n1 in g: g.node[n1]['pretty_name'] = names[n1] out_g = networkx.MultiGraph(data=g) return pure_g, out_g
#lon_lat is a list of (longitude,latitude) a = utm.from_latlon(lon_lat[i + 1][1], lon_lat[i + 1][0]) b = utm.from_latlon(lon_lat[i][1], lon_lat[i][0]) m = (a[1] - b[1]) / a[0] - b[0] print(m) #tan inverse of slop to get angle and checking the quadrant of angle if a[0] - b[0] > 0: angle = atan(m) #print(angle) else: angle = np.pi + atan(m) if angle > 2 * np.pi: angle = angle - 2 * np.pi print(angle) dist = geog.distance(lon_lat[i], lon_lat[i + 1]) #print(dist) #print(rang) for l in np.arange(rang + 1, rang + dist + 1): #m= (latitude2 - latitude1)/(longitude2 - longitude1) a = utm.from_latlon(lon_lat[i + 1][1], lon_lat[i + 1][0]) b = utm.from_latlon(line_points[int(l - 1)][1], line_points[int(l - 1)][0]) m = (a[1] - b[1]) / (a[0] - b[0]) if a[0] - b[0] > 0: angle = atan(m) else: angle = np.pi + atan(m) if angle >= (2 * np.pi): angle = angle - (2 * np.pi)
n=i #print(n) l=0 line_points_utm.append([lon_lat_utm[0][0],lon_lat_utm[0][1]]) #line_points_coordi.append(lon_lat[0]) #new_point_coordi=lon_lat[0] #print(lon_lat_utm,"\n") for i in range(n-1): #print("--------------------------------- ",i," --------------------------------") #m=(lon_lat[i+1][1]-lon_lat[i][1])/(lon_lat[i+1][0]-lon_lat[i][0]) #if lon_lat[i+1][0]-lon_lat[i][0]>0: # line_points_utm.append(line_points_utm[i][0]+dx(distance,m), line_points_utm[i][1]+dy(distance,m)) #else: # line_points_utm.append(line_points_utm[i][0]-dx(distance,m), line_points_utm[i][1]-dy(distance,m)) # going the other way dist=geog.distance(lon_lat[i],lon_lat[i+1]) #print(dist) new_point_coordi=lon_lat[i] #print(new_point_coordi) #print(lon_lat[0],"--------------",lon_lat[1]) flag=True while flag==True: if geog.distance(lon_lat[i+1],new_point_coordi) > distance/2: #print(new_point_coordi,lon_lat[i+1]) #print(geog.distance(new_point_coordi,lon_lat[i+1]),"--------- ",l) l+=1 #print(l) m=(lon_lat_utm[i+1][1]-line_points_utm[int(l-1)][1])/(lon_lat_utm[i+1][0]-line_points_utm[int(l-1)][0]) if lon_lat_utm[i+1][0]-lon_lat_utm[i][0]>0: new_point=[line_points_utm[int(l-1)][0]+dx(distance,m), line_points_utm[int(l-1)][1]+dy(distance,m)]
user_data.head() # In[20]: user_data_np = user_data.as_matrix() airport_data_np = airport_data.as_matrix() # In[99]: xx = airport_data_np[:, 1:3] xx # In[50]: geog.distance(zz, xx) # In[94]: airport_data_np[:, 1:3] # In[ ]: dist_min = [] for i in user_data_np: dist_i = geog.distance(airport_data_np[:, 1:3].astype('float64'), i[1:3].astype('float64')) dist_min.append(np.amin(dist_i)) # In[111]:
def heuristic(self, src, dst): c1, c2 = osmgraph.tools.coordinates(self.graph, (src, dst)) return geog.distance(c1, c2)
if next not in cost_so_far or new_cost < cost_so_far[next]: cost_so_far[next] = new_cost priority = new_cost + self.heuristic(goal, next) frontier.put(next, priority) came_from[next] = current return self.path_as_list(came_from, goal), cost_so_far g = nx.Graph() g.add_node(1, coordinate=(0, 0)) g.add_node(3, coordinate=(1, 1)) g.add_node(5, coordinate=(1, 2)) g.add_node(7, coordinate=(0, 3)) g.add_node(9, coordinate=(-2, 5)) g.add_node(8, coordinate=(-3, 3)) # g.add_node(2, coordinate=(-2,1.5)) g.add_node(2, coordinate=(-0.01, 2.99)) g.add_edges_from([(1, 2), (1, 3), (2, 9), (9, 7), (3, 5), (5, 7), (2, 8)]) valid_nodes = [] for n1, n2 in g.edges(): if n1 not in valid_nodes: valid_nodes.append(n1) if n2 not in valid_nodes: valid_nodes.append(n2) c1, c2 = osmgraph.tools.coordinates(g, (n1, n2)) g[n1][n2]['length'] = geog.distance(c1, c2) a_star = A_star(g, valid_nodes) shortest_path, _ = a_star.a_star_search(1, 7) print(shortest_path)
def test_distance_one_to_n(): points = [locs['bos'], locs['dc'], locs['la']] result = geog.distance(locs['bos'], points) expected = [0.0, _d['dc']['bos'], _d['la']['bos']] assert np.allclose(result, expected)
def dc_fixes(g): bad_road_types = set(['residential', 'service']) streets_to_skip = set(['11th St Alley', '15th Street Northwest Cycle Track', 'Piney Branch Trail', 'Valley Trail', ]) edges_to_purge = [] edges_to_rename = { # TODO: Fix all of these in Open Street Map. 'Belmont Rd NW': 'Belmont Road Northwest', 'Bryant Street': 'Bryant Street Northwest', 'Florida Ave NW' : 'Florida Avenue Northwest', 'Howard Pl NW': 'Howard Place Northwest', 'Lamont St NW': 'Lamont Street Northwest', 'Monroe St NW': 'Monroe Street Northwest', 'Mozart Pl NW' : 'Mozart Place Northwest', 'Oak St NW' : 'Oak Street Northwest', 'Ontario Rd NW': 'Ontario Road Northwest', 'Spring Place' : 'Spring Place Northwest', } for n1, n2 in g.edges_iter(): if ((not g[n1][n2].get('name')) or g[n1][n2].get('name') in streets_to_skip): # and (g[n1][n2].get('highway') in bad_road_types): edges_to_purge.append((n1, n2)) if g[n1][n2].get('name') in edges_to_rename: old_name = g[n1][n2]['name'] g[n1][n2]['name'] = edges_to_rename[old_name] lon1, lat1 = g.node[n1].get('coordinate') if (edge_goes_direction_off_street(g, n1, n2, 'west', 'Connecticut Avenue Northwest') or (edge_goes_direction_off_street(g, n1, n2, 'north', 'Spring Road Northwest') and lon1 < -77.025532) or # TODO: Figure out why 1021561599 is listed as "dead end" when it's # still connected to 49776943. (edge_goes_direction_off_street(g, n1, n2, 'west', 'New Hampshire Avenue Northwest') and lat1 > 38.935466) or (edge_goes_direction_off_street(g, n1, n2, 'north', 'Rock Creek Church Road Northwest') and lat1 > 38.935328) or # East from Park Place north of Michigan Ave but we have to exclude that # one weird spot where Park Place becomes TWO PARK PLACES, or else we # will cut off a legitimate piece of Rock Creek Church Road. (edge_goes_direction_off_street(g, n1, n2, 'east', 'Park Place Northwest') and ((lat1 > 38.928931 and lat1 < 38.936678) or (lat1 > 38.9373))) or (edge_goes_direction_off_street(g, n1, n2, 'north', 'Michigan Avenue Northwest') and lon1 > -77.018030) or edge_goes_direction_off_street(g, n1, n2, 'east', '1st Street Northwest') or edge_goes_direction_off_street(g, n1, n2, 'east', '2nd Street Northwest') or edge_goes_direction_off_street(g, n1, n2, 'south', 'Rhode Island Avenue Northwest') or (edge_goes_direction_off_street(g, n1, n2, 'south', 'Florida Avenue Northwest') and lon1 > -77.020592) or edge_goes_direction_off_street(g, n1, n2, 'south', 'S Street Northwest') or (edge_goes_direction_off_street(g, n1, n2, 'west', '14th Street Northwest') and lat1 < 38.916850) or (edge_goes_direction_off_street(g, n1, n2, 'south', 'U Street Northwest') and lon1 > -77.040668 and lon1 < -77.032636) or (edge_goes_direction_off_street(g, n1, n2, 'south', 'Florida Avenue Northwest') and lon1 > -77.043839 and lon1 < -77.041453) or (edge_goes_direction_off_street(g, n1, n2, 'south', 'Florida Avenue Northwest') and lon1 < -77.044568) or (edge_goes_direction_off_street(g, n1, n2, 'east', 'Florida Avenue Northwest') and lon1 < -77.041453) ): edges_to_purge.append((n1, n2)) for n1, n2 in set(edges_to_purge): g.remove_edge(n1, n2) # 49821049 is on a weird offshoot of Connecticut Ave that goes outside the # ward. nodes_to_purge = [49821049] for node in g: streets = cpl.adjoining_streets(g, node) lon, lat = g.node[node].get('coordinate') if (('Rock Creek Trail' in streets and lat > 38.933061) or ('Connecticut Avenue Northwest' in streets and lon < -77.050140) or ('Calvert Street Northwest' in streets and lon < -77.048509) or ('1st Street Northwest' in streets and lat < 38.921247) or ('S Street Northwest' in streets and lon > -77.020756) or ('Beach Drive Northwest' in streets and lon < -77.048509)): nodes_to_purge.append(node) for node in set(nodes_to_purge): g.remove_node(node) # Having cut off the borders, we cull everything unreachable from within the # borders. ward1_nodes = set(networkx.dfs_preorder_nodes(g, 49745335)) non_ward1_nodes = set(g.nodes()) - ward1_nodes for node in non_ward1_nodes: g.remove_node(node) for n1, n2 in g.edges_iter(): c1, c2 = osmgraph.tools.coordinates(g, (n1, n2)) g[n1][n2]['length'] = geog.distance(c1, c2) return g