def test_extract_graph_links_with_flat_condition_and_list_value_specifying_to_ignore_mixed_types( ): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('2', 3, 4, attribs={'attributes': ['yes', 'no', 'bobby']}) links = graph_operations.extract_links_on_edge_attributes( n, conditions={'attributes': 'yes'}, mixed_dtypes=False) assert links == []
def test_extract_graph_links_with_flat_condition(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) links = graph_operations.extract_links_on_edge_attributes( n, conditions={'attributes': 'yes'}, ) assert links == ['2']
def generate_standard_outputs(n, output_dir, gtfs_day='19700101'): logging.info( f'Generating geojson outputs for the entire network in {output_dir}') save_network_to_geojson(n, output_dir) graph_nodes, graph_links = generate_geodataframes(n.graph) logging.info('Generating geojson outputs for car/driving modal subgraph') graph_output_dir = os.path.join(output_dir, 'graph') gdf_car = graph_links[graph_links.apply(lambda x: modal_subset(x, {'car'}), axis=1)] for attribute in ['freespeed', 'capacity', 'permlanes']: try: save_geodataframe(gdf_car[[attribute, 'geometry', 'id']], filename=f'car_{attribute}_subgraph', output_dir=graph_output_dir) except KeyError: logging.warning( f'Your network is missing a vital attribute {attribute}') logging.info( 'Generating geojson outputs for different highway tags in car modal subgraph' ) highway_tags = n.link_attribute_data_under_key( {'attributes': { 'osm:way:highway': 'text' }}) highway_tags = set( chain.from_iterable(highway_tags.apply(lambda x: setify(x)))) for tag in highway_tags: tag_links = graph_operations.extract_links_on_edge_attributes( network=n, conditions={'attributes': { 'osm:way:highway': { 'text': tag } }}, mixed_dtypes=True) save_geodataframe(graph_links[graph_links['id'].isin(tag_links)], filename=f'car_osm_highway_{tag}', output_dir=graph_output_dir) for mode in n.modes(): logging.info( f'Generating geometry-only geojson outputs for {mode} modal subgraph' ) gdf = graph_links[graph_links.apply(lambda x: modal_subset(x, {mode}), axis=1)] save_geodataframe(gdf[['geometry', 'id']], filename=f'subgraph_geometry_{mode}', output_dir=os.path.join(graph_output_dir, 'geometry_only_subgraphs')) # schedule outputs if n.schedule: generate_standard_outputs_for_schedule(n.schedule, output_dir=os.path.join( output_dir, 'schedule'), gtfs_day=gtfs_day)
def extract_network_id_from_osm_csv(network, attribute_name, osm_csv_path, outpath): """ Parse a Network() object and find edges whose ['attributes'][attribute_name]['text'] is present in a list of OSM way ids :param network: a Network() object with attribute_name tags :param attribute_name: a string corresponding to the name of the link attribute of interest :param osm_csv_path: path to a .csv config file where OSM way ids are stored in column `osm_ids` :param outpath: path to a folder :return: None, but will write .csv and .json files to `outpath` location """ osm_df = pd.read_csv(osm_csv_path, dtype=str) osm_df['network_id'] = pd.Series(dtype=str) target_osm_ids = set(osm_df['osm_ids'].values) osm_to_network_dict = {} with tqdm(total=len(target_osm_ids)) as pbar: for target_id in target_osm_ids: links = graph_operations.extract_links_on_edge_attributes( network, conditions={ 'attributes': { attribute_name: { 'text': target_id } } }, ) # links is now a list of strings if len(links) > 0: # store list of links in dictionary osm_to_network_dict[target_id] = links # mark the OSM id as "matched" in the dataframe osm_df.loc[osm_df['osm_ids'] == target_id, 'network_id'] = True else: # mark the OSM id as "ummatched" in the dataframe osm_df.loc[osm_df['osm_ids'] == target_id, 'network_id'] = False pbar.update(1) # check whether some of our OSM ids were not found unmatched_osm_df = osm_df[osm_df['network_id'] == 'no'] if unmatched_osm_df.shape[0] > 0: # print unmatched ids logging.info( 'these OSM way ids did not find a match in the network.xml') logging.info(unmatched_osm_df['osm_ids'].values) # write dataframe as .csv and dictionary as .json osm_df.to_csv(os.path.join(outpath, 'osm_tolls_with_network_ids.csv'), index=False) with open(os.path.join(outpath, 'osm_to_network_ids.json'), 'w') as write_file: json.dump(osm_to_network_dict, write_file)
def test_extract_graph_links_with_callable_condition_and_list_value_specifying_to_ignore_mixed_types( ): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': {9, 10} } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': [1, 2] } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) def condition(val): return val == 9 links = graph_operations.extract_links_on_edge_attributes( n, conditions={'attributes': { 'osm:way:highway': { 'text': condition } }}, mixed_dtypes=False) assert links == []
def test_extract_graph_links_with_callable_condition(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 9 } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 1 } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) def condition(val): return val == 9 links = graph_operations.extract_links_on_edge_attributes( n, conditions={'attributes': { 'osm:way:highway': { 'text': condition } }}) assert links == ['0']
def test_extract_graph_links_with_list_condition_and_list_value(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': ['primary', 'secondary'] } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': {'primary', 'other'} } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) links = graph_operations.extract_links_on_edge_attributes( n, conditions={ 'attributes': { 'osm:way:highway': { 'text': ['primary', 'some_other_highway'] } } }) assert links == ['0', '1']
def test_extract_graph_links_with_bound_condition_and_list_value(): n = Network('epsg:27700') n.add_link('0', 1, 2, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': [9, 1] } } }) n.add_link('1', 2, 3, attribs={ 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': [0, 1] } } }) n.add_link('2', 3, 4, attribs={'attributes': 'yes'}) links = graph_operations.extract_links_on_edge_attributes( n, conditions={'attributes': { 'osm:way:highway': { 'text': (2, 10) } }}) assert links == ['0']