def import_shapes(dirPath, myMap): print("Importing shapes...") shapeDict = {} shapes = CvtdUtil.parse_csv(dirPath + "/shapes.txt") try: for ix, sid in enumerate(shapes['shape_id']): if sid not in shapeDict: shapeDict[sid] = GtfsShape() lat = float(shapes['shape_pt_lat'][ix]) lon = float(shapes['shape_pt_lon'][ix]) shapeDict[sid].pointList.append(CvtdNode(lat, lon)) except (KeyError, IndexError, ValueError): print(f"Error: import_shapes, ix {ix}, sid {sid}") pass for shapeId in shapeDict: for cmpShapeId in shapeDict: if shapeId != cmpShapeId: if shapeDict[shapeId].copyOfId is None and shapeDict[ cmpShapeId].copyOfId is None: if shapeDict[shapeId].compare_point_list( shapeDict[cmpShapeId].pointList): shapeDict[cmpShapeId].pointList = [] shapeDict[cmpShapeId].copyOfId = shapeId print(f"Shape {cmpShapeId} is a copy of {shapeId}") myMap.shapeDict = shapeDict
def import_calendar(dirPath, myMap): print("Importing calendar...") calendarDict = {} calendar = CvtdUtil.parse_csv(dirPath + "/calendar.txt") strFormat = '%Y%m%d' try: for ix, sid in enumerate(calendar['service_id']): if sid not in calendarDict: calendarDict[sid] = GtfsCalendarEntry() monday = int(calendar['monday'][ix]) tuesday = int(calendar['tuesday'][ix]) wednesday = int(calendar['wednesday'][ix]) thursday = int(calendar['thursday'][ix]) friday = int(calendar['friday'][ix]) saturday = int(calendar['saturday'][ix]) sunday = int(calendar['sunday'][ix]) calendarDict[sid].generateDaysOfWeek(monday, tuesday, wednesday, thursday, friday, saturday, sunday) calendarDict[sid].startDate = datetime.datetime.strptime( calendar['start_date'][ix], strFormat) calendarDict[sid].endDate = datetime.datetime.strptime( calendar['end_date'][ix], strFormat) except (KeyError, IndexError, ValueError): print(f"Error: import_calendar, ix {ix}, sid {sid}") pass myMap.calendarDict = calendarDict
def import_stops(dirPath, myMap): print("Importing stops...") stopDict = {} stops = CvtdUtil.parse_csv(dirPath + "/stops.txt") try: for ix, sid in enumerate(stops['stop_id']): if sid not in stopDict: stopDict[sid] = GtfsStop() if stops['stop_code'][ix] is not '': stopDict[sid].stopCode = int(stops['stop_code'][ix]) stopDict[sid].stopName = stops['stop_name'][ix] stopDict[sid].lat = float(stops['stop_lat'][ix]) stopDict[sid].lon = float(stops['stop_lon'][ix]) if stops['location_type'][ix] is not '': stopDict[sid].locationType = int( stops['location_type'][ix]) if stops['parent_station'][ix] is not '': stopDict[sid].parentStation = int( stops['parent_station'][ix]) except (KeyError, IndexError, ValueError): print(f"Error: import_stops, ix {ix}, sid {sid}") pass myMap.stopDict = stopDict
def test_parse_csv(self): with open('tmpTest1.csv', 'w') as f: f.write('a,b,c\n') f.write('5,,6\n') f.write('1,2,\n') f.write(',,\n') f.write('x,y,z\n') f.write('"3,3","4,4","5,5"\n') result = CvtdUtil.parse_csv('tmpTest1.csv') os.remove('tmpTest1.csv') self.assertListEqual(['5', '1', '', 'x', '"3,3"'], result['a']) self.assertListEqual(['', '2', '', 'y', '"4,4"'], result['b']) self.assertListEqual(['6', '', '', 'z', '"5,5"'], result['c'])
def import_trips(dirPath, myMap): print("Importing trips...") tripDict = {} trips = CvtdUtil.parse_csv(dirPath + "/trips.txt") try: for ix, tid in enumerate(trips['trip_id']): if tid not in tripDict: tripDict[tid] = GtfsTrip() tripDict[tid].routeId = trips['route_id'][ix] tripDict[tid].serviceId = trips['service_id'][ix] tripDict[tid].directionId = int(trips['direction_id'][ix]) tripDict[tid].blockId = trips['block_id'][ix].replace('"', '') tripDict[tid].shapeId = int(trips['shape_id'][ix]) except (KeyError, IndexError, ValueError): print(f"Error: import_trips, ix {ix}, tid {tid}") pass myMap.tripDict = tripDict
def import_routes(dirPath, myMap): print("Importing routes...") routeDict = {} routes = CvtdUtil.parse_csv(dirPath + "/routes.txt") try: for ix, rid in enumerate(routes['route_id']): if rid not in routeDict: routeDict[rid] = GtfsStop() routeDict[rid].agencyId = routes['agency_id'][ix] routeDict[rid].routeShortName = routes['route_short_name'][ix] routeDict[rid].routeLongName = routes['route_long_name'][ix] routeDict[rid].routeDesc = routes['route_desc'][ix] routeDict[rid].routeType = int(routes['route_type'][ix]) except (KeyError, IndexError, ValueError): print(f"Error: import_routes, ix {ix}, rid {rid}") pass myMap.routeDict = routeDict
def import_stop_times(dirPath, myMap): print("Importing stop times...") stopTimeDict = {} stopTimes = CvtdUtil.parse_csv(dirPath + "/stop_times.txt") try: for ix, tid in enumerate(stopTimes['trip_id']): sid = stopTimes['stop_id'][ix] if (tid, sid) not in stopTimeDict: stopTimeDict[(tid, sid)] = GtfsStopTime() stopTimeDict[(tid, sid)].departure = datetime.datetime.strptime( stopTimes['departure_time'][ix], "%H:%M:%S").time() stopTimeDict[(tid, sid)].arrival = datetime.datetime.strptime( stopTimes['arrival_time'][ix], "%H:%M:%S").time() stopTimeDict[( tid, sid)].timepoint = stopTimes['timepoint'][ix] == '1' except (KeyError, IndexError, ValueError): print(f"Error: import_stop_times, ix {ix}, tid {tid}, sid {sid}") pass myMap.stopTimeDict = stopTimeDict