def startElement(self, name, attributes): table = self.table if name == "route": route = attributes["tag"] self.currentRoute = route for stop_row in self.gtfs_map.find_all_stops(): # Probably some overwriting here but the parent stations shouldn't change self.parent_stations[stop_row["stop_id"]] = stop_row["parent_station"] table.routes.route.value = attributes["tag"] table.routes.routetitle.value = attributes["title"] table.routes.color.value = int(attributes["color"], 16) table.routes.oppositecolor.value = int(attributes["oppositeColor"], 16) table.routes.listorder.value = self.startingOrder table.routes.agencyid.value = schema.BusAgencyId elif name == "stop": tag = attributes["tag"] if not self.currentDirection: if tag not in self.sharedStops: self.sharedStops[tag] = True table.stops.tag.value = tag table.stops.lat.value = attributes["lat"] table.stops.lon.value = attributes["lon"] table.stops.title.value = attributes["title"] stop_id = tag.split("_")[0] if stop_id not in self.parent_stations: print("WARNING: tag {tag} not in GTFS".format(tag=tag)) parent = "" else: parent = self.parent_stations[stop_id] table.stops.parent.value = parent self.cur.execute(table.stops.insert()) table.stopmapping.route.value = self.currentRoute table.stopmapping.tag.value = tag self.cur.execute(table.stopmapping.insert()) elif name == "direction": #band attributes["useForUI"] == "true": dirTag = attributes["tag"] self.currentDirection = dirTag if self.currentRoute: table.directions.dirTag.value = dirTag table.directions.dirTitleKey.value = attributes["title"] table.directions.dirRouteKey.value = self.currentRoute table.directions.dirNameKey.value = attributes["name"] table.directions.useAsUI.value = schema.getIntFromBool(attributes["useForUI"]) self.cur.execute(table.directions.insert()) elif name == "path": self.inPath = True self.currentPathPoints = [] elif name == "point": lat = float(attributes["lat"]) lon = float(attributes["lon"]) self.currentPathPoints.append((lat, lon))
def startElement(self, name, attributes): table = self.table if name == "route": route = attributes["tag"] self.currentRoute = route table.routes.route.value = attributes["tag"] routetitle, order = self.routeKeysToTitles[route] table.routes.routetitle.value = routetitle table.routes.color.value = int(attributes["color"], 16) table.routes.oppositecolor.value = int(attributes["oppositeColor"], 16) table.routes.listorder.value = self.startingOrder + order table.routes.agencyid.value = schema.BusAgencyId elif name == "stop": tag = attributes["tag"] if not self.currentDirection: if tag not in self.sharedStops: self.sharedStops[tag] = True table.stops.tag.value = tag table.stops.lat.value = attributes["lat"] table.stops.lon.value = attributes["lon"] table.stops.title.value = attributes["title"] table.stops.insert() table.stopmapping.route.value = self.currentRoute table.stopmapping.tag.value = tag table.stopmapping.dirTag.value = None table.stopmapping.insert() else: pass #table.directionsStops.dirTag.value = self.currentDirection #table.directionsStops.tag.value = tag #table.directionsStops.insert() elif name == "direction": #band attributes["useForUI"] == "true": dirTag = attributes["tag"] self.currentDirection = dirTag if self.currentRoute: table.directions.dirTag.value = dirTag table.directions.dirTitleKey.value = attributes["title"] table.directions.dirRouteKey.value = self.currentRoute table.directions.dirNameKey.value = attributes["name"] table.directions.useAsUI.value = schema.getIntFromBool(attributes["useForUI"]) table.directions.insert() elif name == "path": self.inPath = True self.currentPathPoints = [] elif name == "point": lat = float(attributes["lat"]) lon = float(attributes["lon"]) self.currentPathPoints.append((lat, lon))
def write_sql(data, routeTitles, startOrder): lines = data.split("\n") indexes = {} first_line = lines[0].strip() header_items = first_line.split(",") for i in xrange(len(header_items)): indexes[header_items[i]] = i obj = schema.getSchemaAsObject() routes_done = {} orderedStations = {} paths = {} for line in filter(lambda x: x, (line.strip() for line in lines[1:])): items = line.split(",") routeName = items[indexes["Line"]] if routeName not in routes_done: routeTitle, order = routeTitles[routeName] routes_done[routeName] = {"route":routeName, "routetitle":routeTitle, "color":subway_color[routeName], "oppositecolor":subway_color[routeName], "listorder":startOrder + order, "agencyid":schema.SubwayAgencyId} platformOrder = int(items[indexes["PlatformOrder"]]) latitudeAsDegrees = float(items[indexes["stop_lat"]]) longitudeAsDegrees = float(items[indexes["stop_lon"]]) tag = items[indexes["PlatformKey"]] title = items[indexes["stop_name"]] branch = items[indexes["Branch"]] obj.stops.tag.value = tag obj.stops.title.value = title obj.stops.lat.value = latitudeAsDegrees obj.stops.lon.value = longitudeAsDegrees obj.stops.insert() obj.subway.platformorder.value = platformOrder obj.subway.branch.value = branch obj.subway.tag.value = tag obj.subway.insert() obj.stopmapping.route.value = routeName obj.stopmapping.tag.value = tag obj.stopmapping.dirTag.value = None obj.stopmapping.insert() if routeName not in orderedStations: orderedStations[routeName] = {} paths[routeName] = [] innerMapping = orderedStations[routeName] direction = items[indexes["Direction"]] combinedDirectionHash = createDirectionHash(direction, branch) if combinedDirectionHash not in innerMapping: innerMapping[combinedDirectionHash] = {} innerInnerMapping = innerMapping[combinedDirectionHash] innerInnerMapping[platformOrder] = (latitudeAsDegrees, longitudeAsDegrees) # workaround directions = {RedNorthToAlewife : Direction("North toward Alewife", "", RedLine, True), RedNorthToAlewife2 : Direction("North toward Alewife", "", RedLine, True), RedSouthToBraintree : Direction("South toward Braintree", "", RedLine, True), RedSouthToAshmont : Direction("South toward Ashmont", "", RedLine, True), BlueEastToWonderland : Direction("East toward Wonderland", "", BlueLine, True), BlueWestToBowdoin : Direction("West toward Bowdoin", "", BlueLine, True), OrangeNorthToOakGrove : Direction("North toward Oak Grove", "", OrangeLine, True), OrangeSouthToForestHills : Direction("South toward Forest Hills", "", OrangeLine, True)} for dirKey, direction in directions.iteritems(): obj.directions.dirTag.value = dirKey obj.directions.dirNameKey.value = direction.name obj.directions.dirTitleKey.value = direction.title obj.directions.dirRouteKey.value = direction.route obj.directions.useAsUI.value = schema.getIntFromBool(direction.useForUI) obj.directions.insert() for route, innerMapping in orderedStations.iteritems(): for directionHash, stations in innerMapping.iteritems(): floats = [] for platformOrder in sorted(stations.keys()): floats.append(stations[platformOrder]) #this is kind of a hack. We need to connect the southern branches of the red line if directionHash == "NBAshmont" or directionHash == "NBBraintree": jfkNorthBoundOrder = 5 jfkStation = innerMapping["NBTrunk"][jfkNorthBoundOrder] jfkLat, jfkLon = jfkStation floats.append(jfkStation) paths[route].append(floats) for route, routedata in routes_done.iteritems(): for key, value in routedata.iteritems(): getattr(obj.routes, key).value = value if route in paths: obj.routes.pathblob.value = schema.Box(paths[route]).get_blob_string() obj.routes.insert()