Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/cut_net.py
428331 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2007-2026 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 cut_net.py
15
# @author Michael Behrisch
16
# @date 2026-03-06
17
18
"""
19
This script cuts a network according to the first polygon or multilinestring found in the given geojson
20
"""
21
import json
22
import os
23
import subprocess
24
import sys
25
26
if 'SUMO_HOME' in os.environ:
27
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
28
import sumolib # noqa
29
30
31
def parse_args():
32
USAGE = "Usage: " + sys.argv[0] + " -n <net> <options>"
33
argParser = sumolib.options.ArgumentParser(usage=USAGE)
34
argParser.add_argument("-n", "--input-net-file", required=True,
35
category="input", type=argParser.net_file, help="The .net.xml file to cut")
36
argParser.add_argument("-j", "--geojson", category="input", required=True,
37
type=argParser.file, help="GeoJSON input file")
38
argParser.add_argument("-k", "--netconvert-configuration",
39
category="input", help="Additional netconvert parameter to use")
40
argParser.add_argument("-o", "--output-file", category="output",
41
type=argParser.file, help="The network output file name")
42
argParser.add_argument("-p", "--polygon-output-file", category="output",
43
type=argParser.file, help="The polygon output file name")
44
return argParser.parse_known_args()
45
46
47
if __name__ == "__main__":
48
options, nc_args = parse_args()
49
with open(options.geojson) as jsonf:
50
geo = json.load(jsonf)
51
for feat in geo.get("features", []):
52
if feat["geometry"]["type"] in ("Polygon", "MultiLineString"):
53
shape = feat["geometry"]["coordinates"][0]
54
break
55
if not options.output_file:
56
options.output_file = options.input_net_file.replace(".net.xml", "_cut.net.xml")
57
58
cmd = [sumolib.checkBinary("netconvert"), "-s", options.input_net_file, "-o", options.output_file,
59
"--keep-edges.in-geo-boundary", ",".join(["%s,%s" % (lon, lat) for lon, lat in shape])] + nc_args
60
if options.netconvert_configuration:
61
cmd += ["-c", options.netconvert_configuration]
62
subprocess.check_call(cmd)
63
if options.polygon_output_file:
64
with open(options.polygon_output_file, 'w') as outf:
65
sumolib.xml.writeHeader(outf, root="additional")
66
outf.write(' <poly id="cut" shape="%s" color="blue" geo="true" layer="100"/>\n' %
67
(" ".join(["%s,%s" % (lon, lat) for lon, lat in shape])))
68
outf.write('</additional>\n')
69
70