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_on_attributes(
        n.links(),
        conditions={'attributes': 'yes'},
    )

    assert links == ['2']
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_on_attributes(
        n.links(), conditions={'attributes': 'yes'}, mixed_dtypes=False)

    assert links == []
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_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_on_attributes(
        n.links(),
        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_on_attributes(
        n.links(),
        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_on_attributes(
        n.links(),
        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_on_attributes(
        n.links(),
        conditions={'attributes': {
            'osm:way:highway': {
                'text': (2, 10)
            }
        }})

    assert links == ['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)
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 = Network(epsg='epsg:27700')
    _network_from_file.read_matsim_network(generated_network_file_path)
    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
            }
        })