matsim-vsp / matsim-python-tools

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

About the plan reader #13

Closed fayexin closed 1 year ago

fayexin commented 3 years ago

Hi!

I'm using the plan reader to analyze the plan file. But I have met some problems here. I can't filter the plan file after I got the matsim.plan_reader generator. For example, I want to get all legs with mode= pt in the plan file and no matter what trip type it was. And I use the code as follows:

for person, plan in plans: pt_activities = filter( lambda e: e.tag == 'leg' and e.leg['mode'] == 'pt', plan)

It seems that I didn't filter the activities I want.

How should I work with that? I attached the plan file that I am using now.

~P0OVU$VX5SCTL 2SK 6ANG

Thanks a lot for the help!

Ohay-Angah commented 3 years ago

Same issue

billyc commented 3 years ago

Short version:

Your line of code above should reference e.attrib not e.leg for person, plan in plans: pt_activities = filter( lambda e: e.tag == 'leg' and e.attrib['mode'] == 'pt', plan)

Longer version:

Please read the docs available at https://pypi.org/project/matsim-tools/

Unfortunately it's not obvious how it works without reading the docs, since the library is returning the raw XML elements instead of a more "pythonic" data dictionary.

Each plan is returned as a tuple with its owning person (for now, is this ok?)

  • The name of the element is in its .tag (e.g. 'plan', 'leg', 'route', 'attributes')
  • An element's attributes are accessed using .attrib['attrib-name']
  • Use the element's .text field to get data outside of attributes (e.g. a route's list of links)
  • Every element can be iterated on to get its children (e.g. the plan's activities and legs)
  • Emits person even if that person has no plans

Future versions of the library may try to make this simpler.