コード例 #1
0
def test_extract_graph_links_with_list_of_conditions_strict():
    n = Network('epsg:27700')
    n.add_link('0',
               1,
               2,
               attribs={
                   'attributes': {
                       'osm:way:highway': {
                           'name': 'osm:way:highway:to:hell',
                           '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': {
                'osm:way:highway': {
                    'text': 'primary'
                }
            }
        }, {
            'attributes': {
                'osm:way:highway': {
                    'name': 'osm:way:highway'
                }
            }
        }],
        how=all)

    assert links == ['1']
コード例 #2
0
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 == []
コード例 #3
0
def test_generating_requests_on_non_simplified_graphs():
    n = Network('epsg:27700')
    n.add_link('0', 1, 2, attribs={'modes': ['car']})
    n.add_link('1', 2, 3, attribs={'modes': ['car']})
    n.add_link('2', 4, 3, attribs={'modes': ['car']})
    n.add_link('3', 5, 4, attribs={'modes': ['car']})
    n.add_link('4', 1, 10, attribs={'modes': ['car']})

    for node in n.graph.nodes:
        n.apply_attributes_to_node(node, {'lat': 1, 'lon': 2})

    api_requests = google_directions.generate_requests(n)

    assert_semantically_equal(api_requests, {
        (1, 10): {'path_nodes': (1, 10), 'path_polyline': '_ibE_seK??', 'origin': {'lat': 1, 'lon': 2},
                  'destination': {'lat': 1, 'lon': 2}},
        (1, 3): {'path_nodes': [1, 2, 3], 'path_polyline': '_ibE_seK????', 'origin': {'lat': 1, 'lon': 2},
                 'destination': {'lat': 1, 'lon': 2}},
        (5, 3): {'path_nodes': [5, 4, 3], 'path_polyline': '_ibE_seK????', 'origin': {'lat': 1, 'lon': 2},
                 'destination': {'lat': 1, 'lon': 2}}})
コード例 #4
0
def test_extract_graph_links_with_list_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', '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']
                }
            }
        },
        mixed_dtypes=False)

    assert links == []
コード例 #5
0
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']
コード例 #6
0
def test_extract_graph_links_with_list_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': {
                'osm:way:highway': {
                    'text': ['primary', 'some_other_highway']
                }
            }
        })

    assert links == ['0', '1']
コード例 #7
0
def test_extract_graph_links_with_bound_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, 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)
            }
        }},
        mixed_dtypes=False)

    assert links == []
コード例 #8
0
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']
コード例 #9
0
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
            }
        })
コード例 #10
0
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
            }
        })
コード例 #11
0
def test_saving_network_with_bonkers_attributes_with_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': float('nan')
    }

    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), {
            '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)])
        })

    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
コード例 #12
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)
コード例 #13
0
def test_generating_requests_on_simplified_graphs():
    n = Network('epsg:27700')
    n.add_link('0',
               1,
               3,
               attribs={
                   'modes': ['car'],
                   'geometry':
                   LineString([(528915.9309752393, 181899.48948011652),
                               (528888.1581643537, 181892.3086225874),
                               (528780.3405144282, 181859.84184561518),
                               (528780.3405144282, 181859.84184561518)])
               })
    n.add_link('3',
               5,
               3,
               attribs={
                   'modes': ['car'],
                   'geometry':
                   LineString([(528888.1581643537, 181892.3086225874),
                               (528915.9309752393, 181899.48948011652),
                               (528780.3405144282, 181859.84184561518)])
               })
    n.add_link('4', 1, 10, attribs={'modes': ['car']})
    n.add_link('5', 10, 1, attribs={'modes': ['walk']})
    n.graph.graph["simplified"] = True

    n.apply_attributes_to_nodes({
        1: {
            'x': 528915.9309752393,
            'y': 181899.48948011652
        },
        3: {
            'x': 528780.3405144282,
            'y': 181859.84184561518
        },
        5: {
            'x': 528888.1581643537,
            'y': 181892.3086225874
        },
        10: {
            'x': 528780.3405144282,
            'y': 181892.3086225874
        },
    })

    n.apply_attributes_to_nodes({
        1: {
            'lon': -0.14327038749428384,
            'lat': 51.52130909540579
        },
        3: {
            'lon': -0.14523808749533396,
            'lat': 51.520983695405526
        },
        5: {
            'lon': -0.14367308749449406,
            'lat': 51.52125089540575
        },
        10: {
            'lon': -0.14522623292591474,
            'lat': 51.521275465129236
        }
    })

    api_requests = google_directions.generate_requests(n)

    assert_semantically_equal(
        api_requests, {
            (1, 3): {
                'path_nodes': (1, 3),
                'origin': {
                    'x': 528915.9309752393,
                    'y': 181899.48948011652,
                    'lon': -0.14327038749428384,
                    'lat': 51.52130909540579
                },
                'destination': {
                    'x': 528780.3405144282,
                    'y': 181859.84184561518,
                    'lon': -0.14523808749533396,
                    'lat': 51.520983695405526
                },
                'path_polyline': 'ewmyHl~ZJnAt@xH??'
            },
            (5, 3): {
                'path_nodes': (5, 3),
                'origin': {
                    'x': 528888.1581643537,
                    'y': 181892.3086225874,
                    'lon': -0.14367308749449406,
                    'lat': 51.52125089540575
                },
                'destination': {
                    'x': 528780.3405144282,
                    'y': 181859.84184561518,
                    'lon': -0.14523808749533396,
                    'lat': 51.520983695405526
                },
                'path_polyline': 'yvmyH|`[KoA`AhK'
            },
            (1, 10): {
                'path_nodes': (1, 10),
                'origin': {
                    'x': 528915.9309752393,
                    'y': 181899.48948011652,
                    'lon': -0.14327038749428384,
                    'lat': 51.52130909540579
                },
                'destination': {
                    'x': 528780.3405144282,
                    'y': 181892.3086225874,
                    'lon': -0.14522623292591474,
                    'lat': 51.521275465129236
                },
                'path_polyline': 'ewmyHl~ZDfK'
            }
        })