def get_adjusted_routing_location(latLon, graph=None, edge_gdf=None, node_gdf=None): wgs_point = geom_utils.get_point_from_lat_lon(latLon) etrs_point = geom_utils.project_to_etrs(wgs_point) point_buffer = etrs_point.buffer(90) buffer_random_coords = point_buffer.exterior.coords[0] new_point = Point(buffer_random_coords) point_xy = geom_utils.get_xy_from_geom(new_point) try: node = rt.get_nearest_node(graph, point_xy, edge_gdf, node_gdf, logging=False) node_geom = nw.get_node_geom(graph, node['node']) node_distance = round(node_geom.distance(etrs_point)) node_geom_wgs = geom_utils.project_to_wgs(node_geom) node_latLon = geom_utils.get_lat_lon_from_geom(node_geom_wgs) if (node_distance < 130): return node_latLon except Exception: print('no adjusted origin/destination found') return latLon print('no adjusted origin/destination found') return latLon
def get_new_node_attrs(graph, point): new_node_id = get_new_node_id(graph) wgs_point = geom_utils.project_to_wgs(point) geom_attrs = { **geom_utils.get_xy_from_geom(point), **geom_utils.get_lat_lon_from_geom(wgs_point) } return {'id': new_node_id, **geom_attrs}
def get_hel_poly(WGS84=False, buffer_m=None): # return buffered polygon of Helsinki in either epsg:3879 or WGS84 poly = list(hel['geometry'])[0] if (buffer_m is not None): poly = poly.buffer(buffer_m) if (WGS84 == True): poly = geom_utils.project_to_wgs(poly) return poly
def get_valid_distr_geom(districts, workplaces_distr_join): workplace_distr_g = workplaces_distr_join.groupby('id_distr') district_dicts = [] for idx, distr in districts.iterrows(): # if (distr['id_distr'] != '091_OULUNKYLÄ'): # continue d = { 'id_distr': distr['id_distr'], 'geom_distr_poly': distr['geom_distr_poly'] } try: distr_works = workplace_distr_g.get_group(distr['id_distr']) distr_works = gpd.GeoDataFrame(distr_works, geometry='geom_work', crs=from_epsg(3067)) works_convex_poly = distr_works[ 'geom_work'].unary_union.convex_hull # print(works_convex_poly) works_convex_poly_buffer = works_convex_poly.buffer(20) works_center_point = works_convex_poly_buffer.centroid # print(works_center_point) distr_works['work_center_dist'] = [ round(geom.distance(works_center_point)) for geom in distr_works['geom_work'] ] distr_works = distr_works.sort_values(by='work_center_dist') # print(distr_works.head(70)) center_work = distr_works.iloc[0] d['work_center'] = center_work['geom_work'] d['has_works'] = 'yes' except Exception: d['work_center'] = distr['geom_distr_poly'].centroid d['has_works'] = 'no' district_dicts.append(d) districts_gdf = gpd.GeoDataFrame(district_dicts, geometry='geom_distr_poly', crs=from_epsg(3067)) districts_gdf['distr_latLon'] = [ geom_utils.get_lat_lon_from_geom( geom_utils.project_to_wgs(geom, epsg=3067)) for geom in districts_gdf['work_center'] ] return districts_gdf
nodes_sind = node_gdf.sindex print('Spatial index built.') #%% read YKR work commute data commutes = pd.read_csv('data_ykr/T06_tma_e_TOL2008_2016_hel.csv') # commutes['axyind'] = [int(xyind) for xyind in commutes['axyind']] # commutes['txyind'] = [int(xyind) for xyind in commutes['txyind']] # commutes = commutes.loc[commutes['akunta'] == 91] # commutes = commutes.loc[commutes['sp'] == 0] # commutes.to_csv('data_ykr/T06_tma_e_TOL2008_2016_hel.csv') commutes['geom_home'] = commutes.apply(lambda row: Point(row['ax'], row['ay']), axis=1) commutes['geom_work'] = commutes.apply(lambda row: Point(row['tx'], row['ty']), axis=1) commutes['home_latLon'] = [ geom_utils.get_lat_lon_from_geom(geom_utils.project_to_wgs(geom, epsg=3067)) for geom in commutes['geom_home'] ] commutes['work_latLon'] = [ geom_utils.get_lat_lon_from_geom(geom_utils.project_to_wgs(geom, epsg=3067)) for geom in commutes['geom_work'] ] commutes = commutes[[ 'txyind', 'axyind', 'geom_home', 'geom_work', 'home_latLon', 'work_latLon', 'yht' ]] commutes.head() #%% read grid grid = files.get_statfi_grid()
def get_valid_latLon_for_DT(latLon, distance=60, datetime=None, graph=None, edge_gdf=None, node_gdf=None): # try if initial latLon works try: itins = DT_routing.get_route_itineraries(latLon, { 'lat': 60.278320, 'lon': 24.853545 }, '1.16666', datetime, itins_count=3, max_walk_distance=2500) if (len(itins) > 0): print('initial latLon works wiht DT') return latLon except Exception: print('proceed to finding altenative latLon for DT') # if original latLon did not work, proceed to finding alternative latLon wgs_point = geom_utils.get_point_from_lat_lon(latLon) etrs_point = geom_utils.project_to_etrs(wgs_point) # create a circle for finding alternative points within a distanece from the original point point_buffer = etrs_point.buffer(distance) circle_coords = point_buffer.exterior.coords # find four points at the buffer distance to try as alternative points in routing for n in [0, 1, 2, 3]: circle_quarter = len(circle_coords) / 4 circle_place = math.floor(n * circle_quarter) circle_point_coords = circle_coords[circle_place] circle_point = Point(circle_point_coords) point_xy = geom_utils.get_xy_from_geom(circle_point) try: # find nearest node in the network node = rt.get_nearest_node(graph, point_xy, edge_gdf, node_gdf, logging=False) node_geom = nw.get_node_geom(graph, node['node']) node_distance = round(node_geom.distance(etrs_point)) node_geom_wgs = geom_utils.project_to_wgs(node_geom) node_latLon = geom_utils.get_lat_lon_from_geom(node_geom_wgs) # try DT routing to node location if (node_distance < 90): time.sleep(0.2) try: itins = DT_routing.get_route_itineraries( node_latLon, { 'lat': 60.278320, 'lon': 24.853545 }, '1.16666', datetime, itins_count=3, max_walk_distance=2500) if (len(itins) > 0): print('found DT valid latLon at distance:', node_distance, '-', node_latLon) return node_latLon else: continue except Exception: continue except Exception: print('no node/edge found') continue print('no DT valid latLon found') return None
def get_koskela_kumpula_box(): # return polygon of Kumpula & Koskela area in epsg:3879 rows = bboxes.loc[bboxes['name'] == 'koskela_kumpula'] poly = list(rows['geometry'])[0] bounds = geom_utils.project_to_wgs(poly).bounds return box(*bounds)
import pandas as pd import geopandas as gpd import osmnx as ox import utils.geometry as geom_utils import utils.files as files import utils.networks as nw from multiprocessing import current_process, Pool import networkx as nx import time # READ EXTENT hel_poly = files.get_hel_poly() hel_poly_buff = hel_poly.buffer(1500) extent = geom_utils.project_to_wgs(hel_poly_buff) # GET GRAPH graph = nw.get_walk_network(extent) ox.save_graphml(graph, filename='hel.graphml', folder='graphs', gephi=False) # UNDIRECTED graph_u = ox.get_undirected(graph) ox.save_graphml(graph_u, filename='hel_u.graphml', folder='graphs', gephi=False) # GET EDGE DICTS edge_dicts = nw.get_all_edge_dicts(graph_u) print('Edges in the graph:', len(edge_dicts)) edge_dicts[:2]