def test_extract_graph_nodes_with_nested_condition(): n = Network('epsg:27700') n.add_node( 1, { 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_node( 2, { 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) nodes = graph_operations.extract_nodes_on_node_attributes( n, conditions={'attributes': { 'osm:way:highway': { 'text': 'primary' } }}, ) assert nodes == [1, 2]
def test_consolidating_node_ids_does_nothing_to_matching_nodes_in_matching_coordinate_system( ): n_left = Network('epsg:27700') n_left.epsg = 'epsg:27700' n_left.add_node( '101982', { 'id': '101982', 'x': '528704.1425925883', 'y': '182068.78193707118', 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }) n_right = Network('epsg:27700') n_right.epsg = 'epsg:27700' n_right.add_node( '101982', { 'id': '101982', 'x': '528704.1425925883', 'y': '182068.78193707118', 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }) output_n_right = graph_operations.consolidate_node_indices(n_left, n_right) assert output_n_right.graph.has_node('101982') assert output_n_right.node('101982') == { 'id': '101982', 'x': '528704.1425925883', 'y': '182068.78193707118', 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }
def test_saving_network_with_correct_attributes_and_geometry(tmpdir): # attributes are assumed to be a nested dictionary of very specific format. Due to the fact that user can # do virtually anything to edge attributes, or due to calculation error, this may not be the case. If it's not # of correct format, we don't expect it to get saved to the matsim network.xml network = Network('epsg:27700') network.add_node('0', attribs={ 'id': '0', 'x': 1, 'y': 2, 'lat': 1, 'lon': 2 }) network.add_node('1', attribs={ 'id': '1', 'x': 2, 'y': 2, 'lat': 2, 'lon': 2 }) link_attribs = { 'id': '0', 'from': '0', 'to': '1', 'length': 1, 'freespeed': 1, 'capacity': 20, 'permlanes': 1, 'oneway': '1', 'modes': ['car'], 'geometry': LineString([(1, 2), (2, 3), (3, 4)]), 'attributes': { 'osm:way:lanes': { 'name': 'osm:way:lanes', 'class': 'java.lang.String', 'text': '3' } } } network.add_link('0', '0', '1', attribs=link_attribs) network.write_to_matsim(tmpdir) assert_semantically_equal(dict(network.links()), {'0': link_attribs}) assert_semantically_equal( matsim_xml_writer.check_link_attributes(link_attribs), link_attribs) found_geometry_attrib = False for event, elem in ET.iterparse(os.path.join(tmpdir, 'network.xml'), events=('start', 'end')): if event == 'start': if elem.tag == 'attribute': if elem.attrib['name'] == 'geometry': assert elem.text == '_ibE_seK_ibE_ibE_ibE_ibE' found_geometry_attrib = True assert found_geometry_attrib
def test_saving_network_with_geometry_produces_polyline_if_link_already_has_other_attributes( tmpdir): network = Network('epsg:27700') network.add_node('0', attribs={ 'id': '0', 'x': 1, 'y': 2, 'lat': 1, 'lon': 2 }) network.add_node('1', attribs={ 'id': '1', 'x': 2, 'y': 2, 'lat': 2, 'lon': 2 }) network.add_link('0', '0', '1', attribs={ 'id': '0', 'from': '0', 'to': '1', 'length': 1, 'freespeed': 1, 'capacity': 20, 'permlanes': 1, 'oneway': '1', 'modes': ['car'], 'geometry': LineString([(1, 2), (2, 3), (3, 4)]), 'attributes': { 'osm:way:lanes': { 'name': 'osm:way:lanes', 'class': 'java.lang.String', 'text': '3' } } }) network.write_to_matsim(tmpdir) found_geometry_attrib = False for event, elem in ET.iterparse(os.path.join(tmpdir, 'network.xml'), events=('start', 'end')): if event == 'start': if elem.tag == 'attribute': if elem.attrib['name'] == 'geometry': assert elem.text == '_ibE_seK_ibE_ibE_ibE_ibE' found_geometry_attrib = True assert found_geometry_attrib
def test_network_with_elevation_data_produces_valid_matsim_network_xml_file( tmpdir, network_dtd): network = Network('epsg:27700') network.add_node('0', attribs={ 'id': '0', 'x': 1, 'y': 2, 'z': 3, 'lat': 1, 'lon': 2 }) network.add_node('1', attribs={ 'id': '1', 'x': 2, 'y': 2, 'z': 0, 'lat': 2, 'lon': 2 }) network.add_link('0', '0', '1', attribs={ 'id': '0', 'from': '0', 'to': '1', 'length': 1, 'freespeed': 1, 'capacity': 20, 'permlanes': 1, 'oneway': '1', 'modes': ['car'] }) network.write_to_matsim(tmpdir) generated_network_file_path = os.path.join(tmpdir, 'network.xml') xml_obj = lxml.etree.parse(generated_network_file_path) assert network_dtd.validate(xml_obj), \ 'Doc generated at {} is not valid against DTD due to {}'.format(generated_network_file_path, network_dtd.error_log.filter_from_errors())
def test_extract_graph_nodes_with_callable_condition(): n = Network('epsg:27700') n.add_node( 1, { 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 9 } } }) n.add_node( 2, { 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 1 } } }) n.add_node(3, {'attributes': 'yes'}) def condition(val): return val == 9 nodes = graph_operations.extract_nodes_on_node_attributes( n, conditions={'attributes': { 'osm:way:highway': { 'text': condition } }}) assert nodes == [1]
def test_add_reindexes_node_if_clashes_with_spatially_unmatched_nodes(): n_left = Network('epsg:27700') n_left.epsg = 'epsg:27700' n_left.add_node( '101982', { 'id': '101982', 'x': '528704.1425925883', 'y': '182068.78193707118', 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }) n_right = Network('epsg:27700') n_right.epsg = 'epsg:4326' n_right.add_node( '101982', { 'id': '101982', 'x': -0.1477018870962475, 'y': 51.5205729332399, 'lon': -0.14770188709624754, 'lat': 51.5205729332399, 's2_id': 5221390304444511271 }) n_right.reproject('epsg:27700') output_n_right = graph_operations.consolidate_node_indices(n_left, n_right) assert not output_n_right.graph.has_node('101982') the_node = [i for i, a in output_n_right.nodes()][0] assert_semantically_equal( output_n_right.node(the_node), { 'id': the_node, 'x': 528610.5722059759, 'y': 181809.83345613896, 'lon': -0.14770188709624754, 'lat': 51.5205729332399, 's2_id': 5221390304444511271 })
def test_add_reindexes_node_if_clashes_with_spatially_matched_nodes(): n_left = Network('epsg:27700') n_left.epsg = 'epsg:27700' n_left.add_node( '101982', { 'id': '101982', 'x': '528704.1434730452', 'y': '182068.78144827875', 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }) n_right = Network('epsg:27700') n_right.epsg = 'epsg:4326' n_right.add_node( '101990', { 'id': '101990', 'x': -0.14625948709424305, 'y': 51.52287873323954, 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }) n_right.reproject('epsg:27700') output_n_right = graph_operations.consolidate_node_indices(n_left, n_right) assert output_n_right.graph.has_node('101982') assert not output_n_right.graph.has_node('101990') assert_semantically_equal( output_n_right.node('101982'), { 'id': '101982', 'x': 528704.1434730452, 'y': 182068.78144827875, 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 })
def test_consolidating_node_ids_updates_nodes_data_for_overlapping_nodes_of_different_coordinate_system( ): n_left = Network('epsg:27700') n_left.epsg = 'epsg:27700' n_left.add_node( '101982', { 'id': '101982', 'x': '528704.1434730452', 'y': '182068.78144827875', 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }) n_right = Network('epsg:27700') n_right.epsg = 'epsg:4326' n_right.add_node( '101982', { 'id': '101982', 'x': -0.14625948709424305, 'y': 51.52287873323954, 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 }) n_right.reproject('epsg:27700') output_n_right = graph_operations.consolidate_node_indices(n_left, n_right) assert_semantically_equal( output_n_right.node('101982'), { 'id': '101982', 'x': 528704.1434730452, 'y': 182068.78144827875, 'lon': -0.14625948709424305, 'lat': 51.52287873323954, 's2_id': 5221390329378179879 })
def test_consolidating_node_ids_reprojects_non_overlapping_nodes(): n_left = Network('epsg:27700') n_left.epsg = 'epsg:27700' n_left.add_node( '101986', { 'id': '101986', 'x': '528835.203274008', 'y': '182006.27331298392', 'lon': -0.14439428709377497, 'lat': 51.52228713323965, 's2_id': 5221390328605860387 }) n_right = Network('epsg:27700') n_right.epsg = 'epsg:4326' n_right.add_node( '101990', { 'id': '101990', 'x': -0.14770188709624754, 'y': 51.5205729332399, 'lon': -0.14770188709624754, 'lat': 51.5205729332399, 's2_id': 5221390304444511271 }) n_right.reproject('epsg:27700') output_n_right = graph_operations.consolidate_node_indices(n_left, n_right) assert_semantically_equal( output_n_right.node('101990'), { 'id': '101990', 'x': 528610.5722059759, 'y': 181809.83345613896, 'lon': -0.14770188709624754, 'lat': 51.5205729332399, 's2_id': 5221390304444511271 })
def test_extract_graph_nodes_with_list_of_conditions_strict(): n = Network('epsg:27700') n.add_node( 1, { 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway:to:hell', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_node( 2, { 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'primary' } } }) n.add_node(3, {'attributes': 'yes'}) nodes = graph_operations.extract_nodes_on_node_attributes( n, conditions=[{ 'attributes': { 'osm:way:highway': { 'text': 'primary' } } }, { 'attributes': { 'osm:way:highway': { 'name': 'osm:way:highway' } } }], how=all) assert nodes == [2]
def test_nodes_with_elevation_written_correctly_to_xml_network( tmpdir, network_dtd): network = Network('epsg:27700') network.add_node('0', attribs={ 'id': '0', 'x': 1, 'y': 2, 'z': 3, 'lat': 1, 'lon': 2 }) network.add_node('1', attribs={ 'id': '1', 'x': 2, 'y': 2, 'z': 0, 'lat': 2, 'lon': 2 }) network.add_link('0', '0', '1', attribs={ 'id': '0', 'from': '0', 'to': '1', 'length': 1, 'freespeed': 1, 'capacity': 20, 'permlanes': 1, 'oneway': '1', 'modes': ['car'] }) network.write_to_matsim(tmpdir) generated_network_file_path = os.path.join(tmpdir, 'network.xml') _network_from_file = read.read_matsim( path_to_network=generated_network_file_path, epsg='epsg:27700') assert_semantically_equal( dict(_network_from_file.nodes()), { '0': { 'id': '0', 'x': 1.0, 'y': 2.0, 'z': 3.0, 'lon': -7.557148039524952, 'lat': 49.766825803756994, 's2_id': 5205973754090365183 }, '1': { 'id': '1', 'x': 2.0, 'y': 2.0, 'z': 0.0, 'lon': -7.557134218911724, 'lat': 49.766826468710484, 's2_id': 5205973754090480551 } })
def test_network_with_extra_attribs_produces_valid_matsim_network_xml_file( tmpdir, network_dtd): network = Network('epsg:27700') network.add_node('0', attribs={ 'id': '0', 'x': 1, 'y': 2, 'lat': 1, 'lon': 2 }) network.add_node('1', attribs={ 'id': '1', 'x': 2, 'y': 2, 'lat': 2, 'lon': 2 }) network.add_link('0', '0', '1', attribs={ 'id': '0', 'from': '0', 'to': '1', 'length': 1, 'freespeed': 1, 'capacity': 20, 'permlanes': 1, 'oneway': '1', 'modes': ['car'], 'extra_Special_attrib': 12 }) network.write_to_matsim(tmpdir) generated_network_file_path = os.path.join(tmpdir, 'network.xml') xml_obj = lxml.etree.parse(generated_network_file_path) assert network_dtd.validate(xml_obj), \ 'Doc generated at {} is not valid against DTD due to {}'.format(generated_network_file_path, network_dtd.error_log.filter_from_errors()) _network_from_file = read.read_matsim( path_to_network=generated_network_file_path, epsg='epsg:27700') assert_semantically_equal( dict(_network_from_file.nodes()), { '0': { 'id': '0', 'x': 1.0, 'y': 2.0, 'lon': -7.557148039524952, 'lat': 49.766825803756994, 's2_id': 5205973754090365183 }, '1': { 'id': '1', 'x': 2.0, 'y': 2.0, 'lon': -7.557134218911724, 'lat': 49.766826468710484, 's2_id': 5205973754090480551 } }) assert_semantically_equal( dict(_network_from_file.links()), { '0': { 'id': '0', 'from': '0', 'to': '1', 'freespeed': 1.0, 'capacity': 20.0, 'permlanes': 1.0, 'oneway': '1', 'modes': {'car'}, 's2_from': 5205973754090365183, 's2_to': 5205973754090480551, 'length': 1.0 } })
def test_saving_network_with_geometry_doesnt_change_data_on_the_network( tmpdir): network = Network('epsg:27700') network.add_node('0', attribs={ 'id': '0', 'x': 1, 'y': 2, 'lat': 1, 'lon': 2 }) network.add_node('1', attribs={ 'id': '1', 'x': 2, 'y': 2, 'lat': 2, 'lon': 2 }) network.add_link('0', '0', '1', attribs={ 'id': '0', 'from': '0', 'to': '1', 'length': 1, 'freespeed': 1, 'capacity': 20, 'permlanes': 1, 'oneway': '1', 'modes': ['car'], 'geometry': LineString([(1, 2), (2, 3), (3, 4)]), 'extra_Special_attrib': 12 }) network.add_link('0', '0', '1', attribs={ 'id': '0', 'from': '0', 'to': '1', 'length': 1, 'freespeed': 1, 'capacity': 20, 'permlanes': 1, 'oneway': '1', 'modes': ['car'], 'geometry': LineString([(1, 2), (2, 3), (3, 4)]), 'attributes': { 'osm:way:lanes': { 'name': 'osm:way:lanes', 'class': 'java.lang.String', 'text': '3' } } }) link_attributes = deepcopy(dict(network.links())) node_attributes = deepcopy(dict(network.nodes())) network.write_to_matsim(tmpdir) link_attributes_post_save = dict(network.links()) node_attributes_post_save = dict(network.nodes()) assert_semantically_equal(link_attributes_post_save, link_attributes) assert_semantically_equal(node_attributes_post_save, node_attributes)