Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/osmBuild.py
169659 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-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 osmBuild.py
15
# @author Daniel Krajzewicz
16
# @author Jakob Erdmann
17
# @author Michael Behrisch
18
# @date 2009-08-01
19
20
from __future__ import absolute_import
21
22
import os
23
import subprocess
24
import sumolib
25
26
27
SUMO_HOME = os.environ.get("SUMO_HOME", os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
28
vclassRemove = {"passenger": ["--keep-edges.by-vclass", "passenger"],
29
"publicTransport": ["--keep-edges.by-vclass", "passenger,bus,tram,rail_urban,subway,cable_car,rail"],
30
"road": ["--remove-edges.by-vclass", "tram,rail_urban,subway,cable_car,rail_electric,bicycle,pedestrian"], # noqa
31
"all": []}
32
possibleVClassOptions = '|'.join(vclassRemove.keys())
33
34
DEFAULT_NETCONVERT_OPTS = ('--geometry.remove,--roundabouts.guess,--ramps.guess,--junctions.join,'
35
'--tls.guess-signals,--tls.discard-simple,--tls.join,--output.original-names,'
36
'--junctions.corner-detail,5,--output.street-names')
37
38
39
optParser = sumolib.options.ArgumentParser(description="Import a OpenStreetMap file into SUMO")
40
optParser.add_argument("-p", "--prefix", category="processing", default="osm", help="for output file")
41
# don't know whether area or bbox call was used
42
optParser.add_argument("-f", "--osm-file", category="input", help="full name of the osm file to import")
43
optParser.add_argument("-m", "--typemap", category="input",
44
help="typemap file for the extraction of colored areas (optional)")
45
optParser.add_argument("--netconvert-typemap", category="input", help="typemap files for netconverter (optional)")
46
optParser.add_argument("-o", "--oldapi-prefix",
47
help="prefix that was used for retrieval with the old API")
48
optParser.add_argument("-t", "--tiles", category="processing", type=int, default=1,
49
help="number of tiles used for retrieving OSM-data via the old api")
50
optParser.add_argument("--vehicle-classes", category="processing", default='all',
51
help="[(%s)]extract network for a reduced set of vehicle classes" % possibleVClassOptions)
52
optParser.add_argument("-d", "--output-directory", category="output", default=os.getcwd(),
53
help="directory in which to put the output files")
54
optParser.add_argument("-n", "--netconvert-options", category="processing",
55
default=DEFAULT_NETCONVERT_OPTS, help="comma-separated options for netconvert")
56
optParser.add_argument("--pedestrians", category="processing", action="store_true",
57
default=False, help="add pedestrian infrastructure to the network")
58
optParser.add_argument("-y", "--polyconvert-options", category="processing",
59
default="-v,--osm.keep-full-type", help="comma-separated options for polyconvert")
60
optParser.add_argument("-z", "--gzip", action="store_true", category="processing",
61
default=False, help="save gzipped network")
62
optParser.add_argument("-v", "--verbose", category="processing", action="store_true",
63
default=False, help="enable verbose netconvert output")
64
65
66
def getRelative(dirname, option):
67
ld = len(dirname)
68
if option[:ld] == dirname:
69
return option[ld+1:]
70
else:
71
return option
72
73
74
def build(args=None, bindir=None):
75
options = optParser.parse_args(args=args)
76
77
if ((options.oldapi_prefix and options.osm_file) or
78
not (options.oldapi_prefix or options.osm_file)):
79
optParser.error(
80
"exactly one of the options --osm-file and --oldapi-prefix must be supplied")
81
if options.typemap and not os.path.isfile(options.typemap.replace("${SUMO_HOME}", SUMO_HOME)):
82
# fail early because netconvert may take a long time
83
optParser.error('typemap file "%s" not found' % options.typemap)
84
if options.vehicle_classes not in vclassRemove:
85
optParser.error('invalid vehicle class "%s" given' % options.vehicle_classes)
86
if not os.path.isdir(options.output_directory):
87
optParser.error('output directory "%s" does not exist' %
88
options.output_directory)
89
90
netconvert = sumolib.checkBinary('netconvert', bindir)
91
polyconvert = sumolib.checkBinary('polyconvert', bindir)
92
93
netconvertOpts = [netconvert]
94
if options.pedestrians:
95
netconvertOpts += ['--sidewalks.guess', '--crossings.guess']
96
if options.netconvert_typemap:
97
netconvertOpts += ["-t", options.netconvert_typemap]
98
netconvertOpts += options.netconvert_options.strip().split(',') + ['--osm-files']
99
polyconvertOpts = ([polyconvert] + options.polyconvert_options.strip().split(',') +
100
['--type-file', options.typemap, '--osm-files'])
101
102
prefix = options.oldapi_prefix
103
if prefix: # used old API
104
num = options.tiles
105
tiles = ",".join(["%s%s_%s.osm.xml" % (prefix, i, num)
106
for i in range(num)])
107
netconvertOpts += [tiles]
108
polyconvertOpts += [tiles]
109
else: # used new API
110
netconvertOpts += [options.osm_file]
111
polyconvertOpts += [options.osm_file]
112
prefix = os.path.basename(options.osm_file).replace('.osm.xml', '')
113
114
if options.prefix:
115
prefix = options.prefix
116
117
netfile = prefix + '.net.xml'
118
if options.gzip:
119
netfile += ".gz"
120
netconvertOpts += vclassRemove[options.vehicle_classes] + ["-o", netfile]
121
122
# write config
123
cfg = prefix + ".netccfg"
124
# use relative paths where possible
125
netconvertOpts = [getRelative(options.output_directory, o) for o in netconvertOpts]
126
subprocess.call(netconvertOpts + ["--save-configuration", cfg], cwd=options.output_directory)
127
subprocess.call([netconvert, "-c", cfg], cwd=options.output_directory)
128
129
if options.typemap:
130
# write config
131
cfg = prefix + ".polycfg"
132
polyconvertOpts += ["-n", netfile, "-o", prefix + '.poly.xml']
133
if options.gzip:
134
polyconvertOpts[-1] += ".gz"
135
# use relative paths where possible
136
polyconvertOpts = [getRelative(options.output_directory, o) for o in polyconvertOpts]
137
subprocess.call(polyconvertOpts + ["--save-configuration", cfg], cwd=options.output_directory)
138
subprocess.call([polyconvert, "-c", cfg], cwd=options.output_directory)
139
140
141
if __name__ == "__main__":
142
build()
143
144