Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/net2kml.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2007-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 net2kml.py
15
# @author Jakob Erdmann
16
# @date 2020-02-21
17
18
"""
19
This script converts a sumo network to KML
20
"""
21
from __future__ import absolute_import
22
from __future__ import print_function
23
import os
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
ap = sumolib.options.ArgumentParser(usage=USAGE)
34
ap.add_argument("-n", "--net-file", category="input", type=ap.net_file,
35
dest="netFile", help="The .net.xml file to convert")
36
ap.add_argument("-o", "--output-file", category="output", type=ap.file,
37
dest="outFile", help="The KML output file name")
38
ap.add_argument("-l", "--lanes", action="store_true", default=False,
39
help="Export lane geometries instead of edge geometries")
40
ap.add_argument("-i", "--internal", action="store_true", default=False,
41
help="Export internal geometries")
42
ap.add_argument("--color", category="input", default="0f0000ff", help="Color for normal edges")
43
ap.add_argument("--internal-color", category="input", dest="iColor",
44
default="5f0000ff", help="Color for internal edges")
45
46
options = ap.parse_args()
47
if not options.netFile:
48
print("Missing arguments")
49
ap.print_help()
50
exit()
51
return options
52
53
54
if __name__ == "__main__":
55
options = parse_args()
56
net = sumolib.net.readNet(options.netFile, withInternal=options.internal)
57
geomType = 'lane' if options.lanes else 'edge'
58
59
with open(options.outFile, 'w') as outf:
60
outf.write('<?xml version="1.0" encoding="UTF-8"?>\n')
61
outf.write('<kml xmlns="http://www.opengis.net/kml/2.2">\n')
62
outf.write('<Document>\n')
63
for id, geometry, width in net.getGeometries(options.lanes):
64
color = options.iColor if id[0] == ":" else options.color
65
outf.write("\t<name>%s</name>\n" % options.netFile)
66
outf.write("\t<open>1</open>\n")
67
outf.write("\t<description>network geometries</description>\n")
68
outf.write("\t<Style id=\"trace-%s\">\n" % id)
69
outf.write("\t\t<LineStyle>\n")
70
outf.write("\t\t<color>%s</color>\n" % color)
71
# outf.write("\t\t<colorMode>random</colorMode> \n")
72
outf.write("\t\t<width>%s</width>\n" % width)
73
outf.write("\t\t<gx:labelVisibility>1</gx:labelVisibility>\n")
74
outf.write("\t\t</LineStyle>\n")
75
outf.write("\t</Style>\n")
76
outf.write("\t<Placemark>\n")
77
outf.write("\t\t<name>%s %s</name>\n" % (geomType, id))
78
outf.write("\t\t<styleUrl>#trace-%s</styleUrl>\n" % id)
79
outf.write("\t\t<LineString>\n")
80
outf.write("\t\t<extrude>1</extrude>\n")
81
outf.write("\t\t<tessellate>1</tessellate>\n")
82
outf.write("\t\t\t<coordinates>\n")
83
for x, y in geometry:
84
lon, lat = net.convertXY2LonLat(x, y)
85
outf.write('\t\t\t\t%.10f,%.10f\n' % (lon, lat))
86
outf.write("\t\t\t</coordinates>\n")
87
outf.write("\t\t</LineString>\n")
88
outf.write("\t</Placemark>\n")
89
outf.write('</Document>\n')
90
outf.write('</kml>\n')
91
92