Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
executable file 168 lines (135 sloc) 4.63 KB
# Copyright (C) 2007 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
This package provides implementation of a converter from a kml
file format into Google transit feed format.
The KmlParser class is the main class implementing the parser.
For point geometries, information about stops is extracted from a kml file.
The extractor expects the stops to be represented as placemarks with
a single point.
For line geometries, information about shapes is extracted from a kml file.
from __future__ import print_function
import re
import string
import sys
import transitfeed
from transitfeed import util
import xml.dom.minidom as minidom
import zipfile
class Placemark(object):
def __init__(self): = ""
self.coordinates = []
def IsPoint(self):
return len(self.coordinates) == 1
def IsLine(self):
return len(self.coordinates) > 1
class KmlParser(object):
def __init__(self, stopNameRe = '(.*)'):
stopNameRe - a regular expression to extract a stop name from a
placemaker name
self.stopNameRe = re.compile(stopNameRe)
def Parse(self, filename, feed):
Reads the kml file, parses it and updated the Google transit feed
object with the extracted information.
filename - kml file name
feed - an instance of Schedule class to be updated
dom = minidom.parse(filename)
self.ParseDom(dom, feed)
def ParseDom(self, dom, feed):
Parses the given kml dom tree and updates the Google transit feed object.
dom - kml dom tree
feed - an instance of Schedule class to be updated
shape_num = 0
for node in dom.getElementsByTagName('Placemark'):
p = self.ParsePlacemark(node)
if p.IsPoint():
(lon, lat) = p.coordinates[0]
m =
feed.AddStop(lat, lon,
elif p.IsLine():
self.ConvertPlacemarkToShape(p, feed)
def ParsePlacemark(self, node):
ret = Placemark()
for child in node.childNodes:
if child.nodeName == 'name': = self.ExtractText(child)
if child.nodeName == 'Point' or child.nodeName == 'LineString':
ret.coordinates = self.ExtractCoordinates(child)
return ret
def ExtractText(self, node):
for child in node.childNodes:
if child.nodeType == child.TEXT_NODE:
return child.wholeText # is a unicode string
return ""
def ExtractCoordinates(self, node):
coordinatesText = ""
for child in node.childNodes:
if child.nodeName == 'coordinates':
coordinatesText = self.ExtractText(child)
ret = []
for point in coordinatesText.split():
coords = point.split(',')
ret.append((float(coords[0]), float(coords[1])))
return ret
def ConvertPlacemarkToShape(self, p, feed):
shape = transitfeed.Shape(
for (lon, lat) in p.coordinates:
shape.AddPoint(lat, lon)
existing_shape = feed.GetShape(
# If the existing shape has the same points, we don't need to add a new
# shape.
if existing_shape == shape:
# If the shape has different points, we need to modify our shape id so as
# to avoid duplication.
shape.shape_id += '_%d' % len(feed.GetShapeList())
except KeyError:
# No existing shape with that id, so no worries.
def main():
usage = \
"""%prog <input.kml> <output>
Reads KML file <input.kml> and creates GTFS file <output> with
placemarks in the KML represented as stops.
parser = util.OptionParserLongError(
usage=usage, version='%prog '+transitfeed.__version__)
(options, args) = parser.parse_args()
if len(args) != 2:
parser.error('You did not provide all required command line arguments.')
if args[0] == 'IWantMyCrash':
raise Exception('For testCrashHandler')
parser = KmlParser()
feed = transitfeed.Schedule()
feed.save_all_stops = True
parser.Parse(args[0], feed)
if __name__ == '__main__':