matsim-vsp / matsim-python-tools

Tools for working with matsim in python
57 stars 20 forks source link

Fix reading attributes from network.xml #14

Closed ksauzz closed 3 years ago

ksauzz commented 3 years ago

Issue Summary

read_network doesn't return correct link_attrs. It seems all link_ids are shifted.

How to reproduce

Test Data: network.xml
```xml primary 1 2 way1 secondary 2 2 way2 trunk 3 2 way3 path 4 2 way4 ```

Tried to read attributes from the network.xml, but they are shifted.

In [4]: matsim.read_network('tests/data/network.xml').link_attrs
Out[4]: 
   link_id             name      value
0        4  osm:way:highway    primary            <-- this link_id should be l1. It looks the last node_id
1        4       osm:way:id        1.0
2        4    osm:way:lanes          2
3        4     osm:way:name       way1
4       l1  osm:way:highway  secondary
5       l1       osm:way:id        2.0
6       l1    osm:way:lanes          2
7       l1     osm:way:name       way2
8       l2  osm:way:highway      trunk
9       l2       osm:way:id        3.0
10      l2    osm:way:lanes          2
11      l2     osm:way:name       way3
12      l3  osm:way:highway       path
13      l3       osm:way:id        4.0
14      l3    osm:way:lanes          2
15      l3     osm:way:name       way4

Resolution

_I ran testMatsimNetworkReader.py only since I'm not sure how to prepare the test environment for this project properly.

ksauzz commented 3 years ago

Pushed the fix for missing text in attribute element because I only used start event.

see: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse

Note iterparse() only guarantees that it has seen the “>” character of a starting tag when it emits a “start” event, so the attributes are defined, but the contents of the text and tail attributes are undefined at that point. The same applies to the element children; they may or may not be present. If you need a fully populated element, look for “end” events instead.

ksauzz commented 3 years ago

Thanks!!