--- a/origin-src/transitfeed-1.2.6/shape_importer.py
+++ b/origin-src/transitfeed-1.2.6/shape_importer.py
@@ -1,1 +1,291 @@
-
+#!/usr/bin/python2.4
+#
+# Copyright 2007 Google Inc. All Rights Reserved.
+#
+# 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""A utility program to help add shapes to an existing GTFS feed.
+
+Requires the ogr python package.
+"""
+
+__author__ = 'chris.harrelson.code@gmail.com (Chris Harrelson)'
+
+import csv
+import glob
+import ogr
+import os
+import shutil
+import sys
+import tempfile
+import transitfeed
+from transitfeed import shapelib
+from transitfeed import util
+import zipfile
+
+
+class ShapeImporterError(Exception):
+ pass
+
+
+def PrintColumns(shapefile):
+ """
+ Print the columns of layer 0 of the shapefile to the screen.
+ """
+ ds = ogr.Open(shapefile)
+ layer = ds.GetLayer(0)
+ if len(layer) == 0:
+ raise ShapeImporterError("Layer 0 has no elements!")
+
+ feature = layer.GetFeature(0)
+ print "%d features" % feature.GetFieldCount()
+ for j in range(0, feature.GetFieldCount()):
+ print '--' + feature.GetFieldDefnRef(j).GetName() + \
+ ': ' + feature.GetFieldAsString(j)
+
+
+def AddShapefile(shapefile, graph, key_cols):
+ """
+ Adds shapes found in the given shape filename to the given polyline
+ graph object.
+ """
+ ds = ogr.Open(shapefile)
+ layer = ds.GetLayer(0)
+
+ for i in range(0, len(layer)):
+ feature = layer.GetFeature(i)
+
+ geometry = feature.GetGeometryRef()
+
+ if key_cols:
+ key_list = []
+ for col in key_cols:
+ key_list.append(str(feature.GetField(col)))
+ shape_id = '-'.join(key_list)
+ else:
+ shape_id = '%s-%d' % (shapefile, i)
+
+ poly = shapelib.Poly(name=shape_id)
+ for j in range(0, geometry.GetPointCount()):
+ (lat, lng) = (round(geometry.GetY(j), 15), round(geometry.GetX(j), 15))
+ poly.AddPoint(shapelib.Point.FromLatLng(lat, lng))
+ graph.AddPoly(poly)
+
+ return graph
+
+
+def GetMatchingShape(pattern_poly, trip, matches, max_distance, verbosity=0):
+ """
+ Tries to find a matching shape for the given pattern Poly object,
+ trip, and set of possibly matching Polys from which to choose a match.
+ """
+ if len(matches) == 0:
+ print ('No matching shape found within max-distance %d for trip %s '
+ % (max_distance, trip.trip_id))
+ return None
+
+ if verbosity >= 1:
+ for match in matches:
+ print "match: size %d" % match.GetNumPoints()
+ scores = [(pattern_poly.GreedyPolyMatchDist(match), match)
+ for match in matches]
+
+ scores.sort()
+
+ if scores[0][0] > max_distance:
+ print ('No matching shape found within max-distance %d for trip %s '
+ '(min score was %f)'
+ % (max_distance, trip.trip_id, scores[0][0]))
+ return None
+
+ return scores[0][1]
+
+def AddExtraShapes(extra_shapes_txt, graph):
+ """
+ Add extra shapes into our input set by parsing them out of a GTFS-formatted
+ shapes.txt file. Useful for manually adding lines to a shape file, since it's
+ a pain to edit .shp files.
+ """
+
+ print "Adding extra shapes from %s" % extra_shapes_txt
+ try:
+ tmpdir = tempfile.mkdtemp()
+ shutil.copy(extra_shapes_txt, os.path.join(tmpdir, 'shapes.txt'))
+ loader = transitfeed.ShapeLoader(tmpdir)
+ schedule = loader.Load()
+ for shape in schedule.GetShapeList():
+ print "Adding extra shape: %s" % shape.shape_id
+ graph.AddPoly(ShapeToPoly(shape))
+ finally:
+ if tmpdir:
+ shutil.rmtree(tmpdir)
+
+
+# Note: this method lives here to avoid cross-dependencies between
+# shapelib and transitfeed.
+def ShapeToPoly(shape):
+ poly = shapelib.Poly(name=shape.shape_id)
+ for lat, lng, distance in shape.points:
+ point = shapelib.Point.FromLatLng(round(lat, 15), round(lng, 15))
+ poly.AddPoint(point)
+ return poly
+
+
+def ValidateArgs(options_parser, options, args):
+ if not (args and options.source_gtfs and options.dest_gtfs):
+ options_parser.error("You must specify a source and dest GTFS file, "
+ "and at least one source shapefile")
+
+
+def DefineOptions():
+ usage = \
+"""%prog [options] --source_gtfs= --dest_gtfs=