Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/geoTrip2POI.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file geoTrip2POI.py
15
# @author Jakob Erdmann
16
# @date 2025-01-16
17
18
"""
19
Load a file with trips define with fromLonLat / toLonLat and convert it to a poi file
20
with POIs scaled to the relative amount of departs / arrivals
21
"""
22
from __future__ import absolute_import
23
import sys
24
import os
25
import random
26
import colorsys
27
from collections import defaultdict
28
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
29
import sumolib # noqa
30
from sumolib.xml import parse # noqa
31
32
33
def parse_args(args):
34
op = sumolib.options.ArgumentParser(description="convert geoTrips to POIs")
35
op.add_argument("routeFiles", nargs="+", category="input", type=op.file,
36
help="trip files to analyze")
37
op.add_argument("-o", "--output-file", category="output", dest="outfile",
38
help="output poi file")
39
op.add_argument("-l", "--layer", default=100, help="layer for generated polygons")
40
op.add_argument("--scale-width", type=float, dest="scaleWidth", default=10,
41
help="Scale with of POIs by factor")
42
op.add_argument("--filter-output.file", dest="filterOutputFile",
43
help="only write output for edges in the given selection file")
44
op.add_argument("--seed", type=int, help="random seed")
45
options = op.parse_args()
46
if options.seed:
47
random.seed(options.seed)
48
if options.outfile is None:
49
options.outfile = options.routefiles[0] + ".poi.xml"
50
51
return options
52
53
54
def main(args):
55
options = parse_args(args)
56
departLocs = defaultdict(lambda: 0)
57
arrivalLocs = defaultdict(lambda: 0)
58
59
for routefile in options.routeFiles:
60
for trip in parse(routefile, 'trip'):
61
departLocs[trip.fromLonLat] += 1
62
arrivalLocs[trip.toLonLat] += 1
63
64
maxCount = max(
65
max(departLocs.values()),
66
max(arrivalLocs.values()))
67
68
with open(options.outfile, 'w') as outf:
69
sumolib.writeXMLHeader(outf, root="additional", options=options)
70
for comment, counts, rgb in (
71
('departures', departLocs, (1, 0, 0)),
72
('arrivals', arrivalLocs, (0, 0, 1))):
73
74
outf.write('<!-- %s -->\n' % comment)
75
hue, sat, val = colorsys.rgb_to_hsv(*rgb)
76
for i, (lonLat, count) in enumerate(counts.items()):
77
frac = 0.2 + (count / maxCount) * 0.8
78
lon, lat = lonLat.split(',')
79
outf.write(' <poi id="%s%s" layer="%s" color="%s,1" width="%s" type="%s" lon="%s" lat="%s"/>\n' % (
80
comment[0], i,
81
options.layer,
82
','.join(map(str, colorsys.hsv_to_rgb(hue, frac, val))),
83
options.scaleWidth * frac + 1,
84
count,
85
lon, lat))
86
outf.write('</additional>\n')
87
88
89
if __name__ == "__main__":
90
main(sys.argv[1:])
91
92