abstreet/headless/examples/generate_traffic.py
Dustin Carlino 39f5d50fcd The grand country split. #326
City names are now disambiguated by a two-letter country code. This
commit handles almost everything needed to make this transition. Main
next steps are fixing up map edits automatically and making the city
picker UI understand the extra level of hierarchy.

A little bit of fallout: lakeslice gridlocks again; this regression is
actually from the recent traffic signal changes, but I'm just now
regenerating everything. Will fix soon.
2021-02-13 15:45:59 -08:00

66 lines
2.2 KiB
Python
Executable File

#!/usr/bin/python3
# This example loads an exported JSON map, finds different buildings, and
# generates a simple travel demand model.
#
# 1) cargo run --bin dump_map data/system/us/seattle/maps/montlake.bin > montlake.json
# 2) ./headless/examples/generate_traffic.py --map=montlake.json --out=traffic.json
# 3) cargo run --bin import_traffic -- --map=data/system/us/seattle/maps/montlake.bin --input=traffic.json
# 4) Use data/system/us/seattle/scenarios/montlake/monday.bin in the game or from the API.
#
# Keep this script formatted with autopep8 -i
import argparse
import json
import random
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--map', type=str, required=True)
parser.add_argument('--out', type=str, required=True)
args = parser.parse_args()
# Load the map and find all buildings
residential_building_ids = []
commercial_building_ids = []
with open(args.map, encoding='utf8') as f:
map = json.load(f)
for b in map['buildings']:
# These categories are inferred from OpenStreetMap tags
if 'Residential' in b['bldg_type'] or 'ResidentialCommercial' in b['bldg_type']:
residential_building_ids.append(b['id'])
if 'Commercial' in b['bldg_type'] or 'ResidentialCommercial' in b['bldg_type']:
commercial_building_ids.append(b['id'])
# Randomly generate a few people who take just one trip
scenario = {
'scenario_name': 'monday',
'people': []
}
for _ in range(100):
src = random.choice(residential_building_ids)
dst = random.choice(commercial_building_ids)
scenario['people'].append({
'origin': {
'TripEndpoint': {
'Bldg': src,
}
},
'trips': [{
'departure': 1.0,
'destination': {
'TripEndpoint': {
'Bldg': dst,
}
},
'mode': 'Bike'
}]
})
with open(args.out, 'w') as f:
f.write(json.dumps(scenario, indent=2))
if __name__ == '__main__':
main()