def test_import_trips_txt_maximal(self): trips_txt = StringIO.StringIO( """\ route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,\ block_id,shape_id,wheelchair_accessible,bikes_allowed R1,S1,T1,Headsign,HS,0,B1,S1,1,2 """ ) service = Service.objects.create( feed=self.feed, service_id="S1", start_date=date(2011, 4, 14), end_date=date(2011, 12, 31) ) block = Block.objects.create(feed=self.feed, block_id="B1") shape = Shape.objects.create(feed=self.feed, shape_id="S1") Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() self.assertEqual(trip.route, self.route) self.assertEqual(list(trip.services.all()), [service]) self.assertEqual(trip.trip_id, "T1") self.assertEqual(trip.headsign, "Headsign") self.assertEqual(trip.short_name, "HS") self.assertEqual(trip.direction, "0") self.assertEqual(trip.block, block) self.assertEqual(trip.shape, shape) self.assertEqual(trip.wheelchair_accessible, "1") self.assertEqual(trip.bikes_allowed, "2")
def test_import_trips_txt_multiple_services(self): ''' If a trip is associated with several services, only one is created Before 0.4.0, the trip was related to both services After 0.4.0, the trip is related to only the first service ''' trips_txt = StringIO("""\ route_id,service_id,trip_id R1,S1,T1 R1,S2,T1 """) service1 = Service.objects.create(feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) service2 = Service.objects.create(feed=self.feed, service_id='S2', start_date=date(2012, 1, 1), end_date=date(2012, 4, 14)) Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() self.assertEqual(trip.route, self.route) self.assertEqual(trip.service, service1) self.assertFalse(service2.trip_set.exists())
def test_import_trips_txt_duplicate(self): trips_txt = StringIO("""\ route_id,service_id,trip_id R1,S1,T1 R1,S1,T1 """) Service.objects.create( feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() # Just one self.assertEqual(trip.trip_id, 'T1')
def test_import_trips_txt_quoted_direction_id(self): ''' A direction_id should be stripped of quotation marks Issue 64 ''' trips_txt = StringIO("""\ route_id,service_id,trip_id,shape_id,trip_headsign,direction_id R1,"S1","T3","46-860-y11-1.2.I","Aston Quay", "1" """) Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get(trip_id='T3') self.assertEqual(trip.direction, '1')
def test_import_trips_txt_duplicate(self): trips_txt = StringIO("""\ route_id,service_id,trip_id R1,S1,T1 R1,S1,T1 """) Service.objects.create(feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() # Just one self.assertEqual(trip.trip_id, 'T1')
def test_import_trips_txt_minimal(self): trips_txt = StringIO.StringIO("""\ route_id,service_id,trip_id R1,S1,T1 """) service = Service.objects.create( feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() self.assertEqual(trip.route, self.route) self.assertEqual(list(trip.services.all()), [service]) self.assertEqual(trip.trip_id, 'T1') self.assertEqual(trip.headsign, '') self.assertEqual(trip.short_name, '') self.assertEqual(trip.direction, '') self.assertEqual(trip.block, None) self.assertEqual(trip.shape, None)
def test_export_trips_txt_minimal(self): service = Service.objects.create( feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) Trip.objects.create(route=self.route, service=service, trip_id='T1') trips_txt = Trip.export_txt(self.feed) self.assertEqual(trips_txt, """\ route_id,service_id,trip_id R1,S1,T1 """)
def test_import_trips_txt_multiple_services(self): '''If a trip is associated with several services, one is created''' trips_txt = StringIO.StringIO("""\ route_id,service_id,trip_id R1,S1,T1 R1,S2,T1 """) service1 = Service.objects.create( feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) service2 = Service.objects.create( feed=self.feed, service_id='S2', start_date=date(2012, 1, 1), end_date=date(2012, 4, 14)) Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() self.assertEqual(trip.route, self.route) self.assertEqual(trip.services.count(), 2) self.assertTrue(service1 in trip.services.all()) self.assertTrue(service2 in trip.services.all())
def test_export_trips_txt_minimal(self): service = Service.objects.create(feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) Trip.objects.create(route=self.route, service=service, trip_id='T1') trips_txt = Trip.export_txt(self.feed) self.assertEqual(trips_txt, """\ route_id,service_id,trip_id R1,S1,T1 """)
def test_import_trips_txt_maximal(self): trips_txt = StringIO.StringIO("""\ route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,\ block_id,shape_id R1,S1,T1,Headsign,HS,0,B1,S1 """) service = Service.objects.create( feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) block = Block.objects.create(feed=self.feed, block_id='B1') shape = Shape.objects.create(feed=self.feed, shape_id='S1') Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() self.assertEqual(trip.route, self.route) self.assertEqual(list(trip.services.all()), [service]) self.assertEqual(trip.trip_id, 'T1') self.assertEqual(trip.headsign, 'Headsign') self.assertEqual(trip.short_name, 'HS') self.assertEqual(trip.direction, '0') self.assertEqual(trip.block, block) self.assertEqual(trip.shape, shape)
def test_import_trips_txt_multiple_services(self): ''' If a trip is associated with several services, only one is created Before 0.4.0, the trip was related to both services After 0.4.0, the trip is related to only the first service ''' trips_txt = StringIO("""\ route_id,service_id,trip_id R1,S1,T1 R1,S2,T1 """) service1 = Service.objects.create( feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) service2 = Service.objects.create( feed=self.feed, service_id='S2', start_date=date(2012, 1, 1), end_date=date(2012, 4, 14)) Trip.import_txt(trips_txt, self.feed) trip = Trip.objects.get() self.assertEqual(trip.route, self.route) self.assertEqual(trip.service, service1) self.assertFalse(service2.trip_set.exists())
def test_export_trips_txt_maximal(self): service = Service.objects.create( feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) block = Block.objects.create(feed=self.feed, block_id='B1') shape = Shape.objects.create(feed=self.feed, shape_id='S1') Trip.objects.create( route=self.route, service=service, trip_id='T1', headsign='Headsign', short_name='HS', direction=0, block=block, shape=shape, wheelchair_accessible='2', bikes_allowed='1') trips_txt = Trip.export_txt(self.feed) self.assertEqual(trips_txt, """\ route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,\ block_id,shape_id,wheelchair_accessible,bikes_allowed R1,S1,T1,Headsign,HS,0,B1,S1,2,1 """)
def test_export_trips_txt_maximal(self): service = Service.objects.create(feed=self.feed, service_id='S1', start_date=date(2011, 4, 14), end_date=date(2011, 12, 31)) block = Block.objects.create(feed=self.feed, block_id='B1') shape = Shape.objects.create(feed=self.feed, shape_id='S1') Trip.objects.create(route=self.route, service=service, trip_id='T1', headsign='Headsign', short_name='HS', direction=0, block=block, shape=shape, wheelchair_accessible='2', bikes_allowed='1') trips_txt = Trip.export_txt(self.feed) self.assertEqual( trips_txt, """\ route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,\ block_id,shape_id,wheelchair_accessible,bikes_allowed R1,S1,T1,Headsign,HS,0,B1,S1,2,1 """)
def new_trip(request, **kwargs): route = Route.objects.get(id=kwargs.get('route_id')) # Create route + shape context = dict() context.update(kwargs) context['headsign_options'] = route.desc.split('-') context['service_times'] = Service.objects.all() if request.method == 'POST': request_params = request.POST.dict() shape_file = shapefile.Reader(shp=request.FILES['shape-file'], dbf=request.FILES['shape-file-dbf']) stops_reader = DictReaderStrip(request.FILES['stops-file']) # Check required fields are present in the stops csv expected_fields = set([ 'stop_sequence', 'lat', 'lon', 'stop_name', 'designation', 'location_type' ]) current_fields = set(stops_reader.fieldnames) if not expected_fields.issubset(current_fields): missing_fields = expected_fields.difference(current_fields) context[ 'error_message'] = 'The following columns are missing from the uploaded stops file: {}.'.format( missing_fields) return render(request, 'myapp/new-trip.html', context) # Trip variables headsign = request_params['headsign'] service_id = request_params['service-id'] direction = request_params['inbound'] route_id = kwargs['route_id'] # corridor + 4 characters for the route number corridor = route.route_id[0] route_number = route.route_id[5:9] origin = request_params['origin'] route_variation = request_params['route-variation'] shape_id = "{}{}{}{}{}".format(corridor, route_number, origin, route_variation, direction) trip_id = shape_id with transaction.atomic(): # Create new shape shape = Shape(feed_id=kwargs['feed_id'], shape_id=shape_id) shape.save() trip = Trip(trip_id=trip_id, headsign=headsign, service_id=service_id, direction=direction, route_id=route_id, shape_id=shape.id) trip.save() # Create shape points from the uploaded shape files shapes = shape_file.shapes() sequence_start = 1001 # The trip line string is stored in layer 1 for idx, point in enumerate(shapes[1].points): shape_point = ShapePoint(point='POINT ({} {})'.format( point[0], point[1]), shape_id=shape.id, sequence=sequence_start + idx) shape_point.save() shape.update_geometry() start_seconds = 6 * 3600 # First trip is at 6am delta = 5 * 60 # % minutes for row in stops_reader: tmp = list(row['stop_name'].upper().replace(' ', '')) random.shuffle(tmp) stop_suffix = "".join( tmp[:3]) # pick 3 characters from the shuffled stop name stop, created = Stop.objects.get_or_create( point=geos.fromstr('POINT({} {})'.format( row['lon'], row['lat'])), feed_id=kwargs['feed_id'], defaults={ 'stop_id': '{}{}{}{}'.format(corridor.zfill(2), row['designation'] or 0, direction, stop_suffix), 'name': row['stop_name'], 'location_type': row['location_type'], }) trip.stoptime_set.add( StopTime(stop_id=stop.id, trip_id=trip.id, stop_sequence=int(row['stop_sequence']) + 1, arrival_time=start_seconds, departure_time=start_seconds)) start_seconds += delta trip.save() trip.update_geometry() trip.refresh_from_db() return http.HttpResponseRedirect( reverse('route_detail', kwargs={ 'pk': kwargs['route_id'], 'feed_id': kwargs['feed_id'], 'agency_id': kwargs['agency_id'] })) return render(request, 'myapp/new-trip.html', context)
def test_export_trips_txt_empty(self): trips_txt = Trip.export_txt(self.feed) self.assertFalse(trips_txt)