Skip to content

Commit

Permalink
add zone information to data model
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Feb 18, 2024
1 parent 25ec94b commit ed2e526
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions matsim/scenariogen/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ class Household:
type: HouseholdType
region_type: int
location: str
zone: str = None
""" A detailed zone, which can be more accurate than location. """
income: float = None
geom: object = None

Expand Down Expand Up @@ -328,6 +330,8 @@ class Trip:
purpose: Purpose
sd_group: SourceDestinationGroup
valid: bool
from_zone: str = None
to_zone: str = None


@dataclass
Expand Down
38 changes: 36 additions & 2 deletions matsim/scenariogen/data/formats/srv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import os

import pandas as pd
import numpy as np
import pandas as pd

from .. import *

# Has households, persons and trips
INPUT_FILES = 3


def is_format(f: os.DirEntry):
fp = f.name
if not f.path.endswith(".csv"):
Expand Down Expand Up @@ -92,6 +93,7 @@ def convert(data: tuple, regio=None):
SrV2018.household_type(h.E_HHTYP),
SrV2018.region_type(h, regio, random_state),
h.ST_CODE_NAME,
zone=SrV2018.parse_zone(h),
income=SrV2018.income(h.V_EINK),
)
)
Expand All @@ -116,17 +118,33 @@ def convert(data: tuple, regio=None):
SrV2018.trip_purpose(t.V_ZWECK),
SrV2018.sd_group(int(t.E_QZG_17)),
# Trip is valid if length and duration are present
0 <= t.GIS_LAENGE and t.E_DAUER > 0
0 <= t.GIS_LAENGE and t.E_DAUER > 0,
from_zone=SrV2018.parse_zone(t, "V_START_"),
to_zone=SrV2018.parse_zone(t, "V_ZIEL_")
)
)

return pd.DataFrame(hhs).set_index("hh_id"), ps, pd.DataFrame(ts).set_index("t_id")


def pint(x):
""" Convert to positive integer"""
return max(0, int(x))


def parse_int_str(x):
""" Return parsed int or string """
try:
i = int(x)
if i >= 0:
return str(i)
return None
except ValueError:
if not x:
return None
return x


class SrV2018:
""" Maps SrV data to standard format"""

Expand Down Expand Up @@ -420,3 +438,19 @@ def income(x):
return 5600

return -1

@staticmethod
def parse_zone(h, prefix=""):
ob = parse_int_str(getattr(h, prefix + "OBERBEZIRK"))

zone = None
if ob:
zone = ob
ub = parse_int_str(getattr(h, prefix + "UNTERBEZIRK"))
if ub:
zone += "-" + ub
tb = parse_int_str(getattr(h, prefix + "TEILBEZIRK"))
if tb:
zone += "-" + tb

return zone

0 comments on commit ed2e526

Please sign in to comment.