Пример #1
0
 def get(self, stop_id):
     stop_id = unicode(unquote(stop_id))
     stop = Stop.get_by_key_name(stop_id)
     if stop:
         trips = Trip.all().filter('stops =', stop_id).fetch(1000)
         prefetch_refprop(trips, Trip.route)
         template_values = {'stop': stop, 'trips': trips}
         path = os.path.join(os.path.dirname(__file__), 'templates/stop_details.html')
         self.response.out.write(template.render(path, template_values))
     else:
         self.error(404)
         return self.response.out.write('Erro - Pagina nao encontrada')
Пример #2
0
 def save_stops(route_xml, route_obj, api_call):
     stops = route_xml.findall('stop')
     for stop in stops:
         s =  Stop.get_or_create(db.session,
             title = stop.get('title'),
             lat = float(stop.get('lat')),
             lon = float(stop.get('lon')),
             stop_id = stop.get('stopId'),
             api_call_id = api_call.id)
         db.session.flush()
         rs = RouteStop.get_or_create(db.session,
             route_id = route_obj.id,
             stop_id = s.id,
             stop_tag = stop.get('tag'))
Пример #3
0
def get_stops():
    ''' The initial query to set up the stop db '''
    all_stops = Stop.query.all()
    all_ids = [stop.id for stop in all_stops]
    url = config.stops_txt_url
    req = requests.get(url)
    req.encoding = 'utf-8'
    text = req.text.split('\n')
    reader = csv.DictReader(text, delimiter=',', quotechar='"')
    counter = 0
    for line in reader:
        stop = Stop(line)
        if stop.isStation and stop.id not in all_ids:
            stop.matches = stop.is_in_osm()
            if stop.matches > 0:
                print_success(stop.name + ": " + str(stop.matches))
            else:
                print_failure(stop.name + ":  0")
            stop.last_run = datetime.datetime.now().replace(microsecond=0)
            db.session.add(stop)
            counter += 1
            if counter % 20 == 0:
                db.session.commit()
Пример #4
0
	def _retry_form(self, agency, vehicle, card_id, known_stop, stop_name, comment, error_msg):
		known_stops = list(Stop.query(Stop.stop_type == vehicle, Stop.agency == agency.key).order(Stop.name))

		context = dict(
		    agency_name=agency.name,
			agency_id=agency.key.id(),
			vehicle=urllib.quote_plus(vehicle),
			card_id=urllib.quote_plus(str(card_id)),
			known_stop=known_stop, # select element
			stop_name=urllib.quote_plus(stop_name),
			comment=comment, # textarea
			error_msg=error_msg,
			known_stops=known_stops,
		)
		
		self.render_to_response('agency_report.html', context)
Пример #5
0
	def get(self, agency_id, vehicle):
		try:
			agency = Agency.get_by_id(int(agency_id))
			if agency is None or vehicle not in VEHICLE_TYPES or vehicle not in agency.vehicle_types:
				return self.send_404()
		except:
			return self.send_404()

		# Check for known stops
		known_stops = list(Stop.query(Stop.stop_type == vehicle, Stop.agency == agency.key).order(Stop.name))

		context = dict(
			agency_name=agency.name,
			agency_id=agency.key.id(),
			vehicle=vehicle,
			known_stops=known_stops,
		)
		self.render_to_response('agency_report.html', context)
Пример #6
0
def addStop(r, bmark, re=None, agency=None, route=None, 
                                                    direction=None, stop=None):
    error = ''
    if (stop is not None):
        url = nextbus.timeURL % (agency, route, direction, stop)
        q = db.Query(Bmark)
        q.filter('name = ', bmark)
        b = q.get()
        q = db.Query(Stop)
        q.filter('bmark = ', b)
        q.filter('url = ', url)
        stopObj = q.get()
        logging.info(stopObj)
        if (stopObj):
            logging.info("Stop Already Exists")
            error = "This stop has already been added"
            #it'll fall through to picking a stop again
        else:
            stopObj = Stop()
            stopObj.bmark = b
            stopObj.url = url
            stopObj.system = "nextbus"
            stopObj.put()
            #TODO: redirect to an edit page that doesnt involve the scrape
            return HttpResponseRedirect('/catch/%s' % (bmark))
    if (direction is not None):
        data = nextbus.getStops(agency, route, direction)
        prefix = '/nb/addstop/%s/%s/%s/%s/%s'\
            % (bmark, re, agency, route, direction)
        instructions = 'pick a stop'
    elif (route is not None):
        data = nextbus.getDirections(agency, route)
        prefix = '/nb/addstop/%s/%s/%s/%s' % (bmark, re, agency, route)
        instructions = 'pick a direction'
    elif (agency is not None):
        data = nextbus.getRoutes(agency)
        prefix = '/nb/addstop/%s/%s/%s' % (bmark, re, agency)
        instructions = 'pick a route'
    items = []
    if not (data):
        #todo: throw 500
        return False
    for key, value in data:
        items.append({'url_piece': key, 'title': value})
    t = loader.get_template('listoflinks.html')
    c = Context({ 'items':items, 'prefix': prefix })
    items_t = t.render(c)
    params = { 'prefix': prefix,
               'instructions': instructions,
               'list': items_t,
               'error': error }
    return render_with_user('user/addstop.html', params)
Пример #7
0
    def get(self, trip_id):
        "Returns encoded polyline path from trip_id"

        trip_id = unquote(trip_id)
        trip = Trip.get_by_key_name('trip_' + trip_id)
        stops = Stop.get_by_key_name(trip.stops)
        stops = filter(lambda s: s is not None, stops)
        stops = filter(lambda s: s.location is not None, stops)
        def serializable_stop(stop):
            return (stop.id,
                    stop.location.lat,
                    stop.location.lon,
                    stop.name)

        stop_locations = [serializable_stop(stop) for stop in stops]

        if trip:
            data = {'points': trip.shape_encoded_polyline,
                    'levels': trip.shape_encoded_levels,
                    'color': trip.route.color,
                    'stops': stop_locations}
            self.response.out.write(simplejson.dumps(data))
Пример #8
0
	def post(self, agency_id):
		user = users.get_current_user()
		if not user or not users.is_current_user_admin():
			return

		try:
			agency = Agency.get_by_id(int(agency_id))
			if agency is None:
				return self.send_404()
		except:
			return self.send_404()

		error_msg = None
		ok_msg = None

		# Process the upload
		upload = self.request.POST['upload']
		if upload.file:
			upload_csv = csv.reader(upload.file)
			header = upload_csv.next()
			
			name_f = header.index('name')
			stop_type_f = header.index('stop_type')
			try:
				stop_id_f = header.index('stop_id')
			except:
				stop_id_f = None
			try:
				stop_code_f = header.index('stop_code')
			except:
				stop_code_f = None
			try:
				y_f = header.index('y')
				x_f = header.index('x')
			except:
				y_f = x_f = None
			
			# Start reading rows
			stop_count = 0
			for row in upload_csv:
				name = row[name_f].strip()
				stop_type = row[stop_type_f].strip()
				if stop_type not in VEHICLE_TYPES.keys():
					error_msg = 'stop type invalid (%r)' % (stop_type,)
					break
				try:
					stop_code = row[stop_code_f].strip()
				except:
					stop_code = None
				try:
					stop_id = row[stop_id_f].strip()
				except:
					stop_id = None
				try:
					x = float(row[x_f].strip())
					y = float(row[y_f].strip())
				except:
					x_f = y_f = None
				if x_f is not None:
					point = ndb.GeoPt(y, x)
				else:
					point = None
				
				stop = Stop(
					name=name,
					stop_type=stop_type,
					agency=agency.key,
					gtfs_stop_id=stop_id,
					gtfs_stop_code=stop_code,
					gtfs_point=point,
				)
				stop.put()
				stop_count += 1
			ok_msg = 'imported %d stop(s)' % stop_count
		else:
			error_msg = 'cannot open uploaded file'

		context = dict(
			agency_name=agency.name,
			vehicle_types=VEHICLE_TYPES.keys(),
			error_msg=error_msg,
			ok_msg=ok_msg,
		)
		
		self.render_to_response('agency_stops_import.html', context)
Пример #9
0
	def post(self):
		profile = Profile.get_or_create()
		assert profile is not None

		agency_id = self.request.get('agency_id')
		vehicle = self.request.get('vehicle')
		card_id = self.request.get('card_id')
		known_stop = self.request.get('known_stop')
		stop_name = self.request.get('stop_name')
		comment = self.request.get('comment')

		# Start validation!
		# Check agency
		try:
			agency = Agency.get_by_id(int(agency_id))
			if agency is None or not agency.enabled:
				# Invalid agency
				return self.send_404()
		except:
			return self.send_404()

		# Check vehicle type
		if vehicle not in VEHICLE_TYPES or vehicle not in agency.vehicle_types:
			return self.send_404()

		# Check card id
		try:
			card_id = int(card_id)
		except:
			# invalid card id, not a number.
			return self._retry_form(agency, vehicle, card_id, known_stop, stop_name, comment, 'Card ID is not a number')

		# Check known_stop
		if known_stop != '':
			try:
				known_stop = Stop.get_by_id(int(known_stop))
				if known_stop.agency.id() != agency.key.id():
					# stop is not for agency
					known_stop = None
			except:
				
				known_stop = None
		
		if known_stop is None:
			# Invalid stop
			return self._retry_form(agency, vehicle, card_id, known_stop, stop_name, comment, 'Stop ID is not valid')

		if known_stop == '':
			known_stop = None

		# Check stop_name is present if known_stop is not
		if known_stop is None and stop_name == '':
			# Custom name not specified and no known stop selected.
			return self._retry_form(agency, vehicle, card_id, known_stop, stop_name, comment, 'No stop name was entered')

		# If the user is banned, then say we processed the report, but don't
		# actually store it anywhere.
		if not profile.banned:
			# Now get the extra metadata and make the report
			country = self.request.headers.get('x-appengine-country', 'XX')
			region = self.request.headers.get('x-appengine-region', '')
			city = self.request.headers.get('x-appengine-city', '')
			ip = self.request.remote_addr
		
			report = StopReport(
				agency=agency.key,
				stop=known_stop.key if known_stop else None,
				name=stop_name,
				stop_type=vehicle,
				card_id=str(card_id),
				comment=comment,
				gae_country=country,
				gae_region=region,
				gae_city=city,
				gae_ip=ip,
				user=profile.key
			)
			report.put()
		
		context = dict(
			agency_name=agency.name,
			agency_id=agency.key.id(),
		)
		self.render_to_response('report_sent.html', context)
Пример #10
0
engine = create_engine('sqlite:///app.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

# TABLE stops
# (1) stops.txt
stops_table = []
with open("../{0}/stops.txt".format(GTFS_FOLDER)) as stops_file:
    stops = csv.reader(stops_file)
    for stop in stops:
        # Disregard first row
        if stop[2] != "stop_name":
            stops_table.append(Stop(
                stop[2],
                float(stop[4]),
                float(stop[5]),
                stop[0]
            ))
session.add_all(stops_table)

# TABLE routes
# (1) trips.txt
routes_table = []
with open("../{0}/trips.txt".format(GTFS_FOLDER)) as trips_file:
    trips = csv.reader(trips_file)
    for trip in trips:
        # Disregard first row
        if trip[0] != "route_id":
            routes_table.append(Route(
                None,
                None,
Пример #11
0
 def stop(self, agency_tag, route_tag, direction_tag, stop_tag):
     return formatter.JSON(Stop.properties(agency_tag, route_tag, direction_tag, stop_tag))
Пример #12
0
 def stops(self, agency_tag, route_tag, direction_tag):
     return formatter.JSON(Stop.get_or_fetch(agency_tag, route_tag, direction_tag), prefetch = True, attr = "directions")
Пример #13
0
    def get(self):
        def _simple_error(message, code=400):
            self.error(code)
            self.response.out.write(simplejson.dumps({
                'status': 'error',
                'error': { 'message': message },
                'results': []
            }))
            return None

        self.response.headers['Content-Type'] = 'application/json'
        query_type = self.request.get('type')

        if not query_type in ['proximity', 'bounds']:
            return _simple_error('type parameter must be '
                                 'one of "proximity", "bounds".',
                                 code=400)

        if query_type == 'proximity':
            try:
                center = geotypes.Point(float(self.request.get('lat')),
                                        float(self.request.get('lon')))
            except ValueError:
                return _simple_error('lat and lon parameters must be valid latitude '
                                     'and longitude values.')
        elif query_type == 'bounds':
            try:
                bounds = geotypes.Box(float(self.request.get('north')),
                                    float(self.request.get('east')),
                                    float(self.request.get('south')),
                                    float(self.request.get('west')))
            except ValueError:
                return _simple_error('north, south, east, and west parameters must be '
                                 'valid latitude/longitude values.')

        max_results = 100
        if self.request.get('maxresults'):
            max_results = int(self.request.get('maxresults'))

        max_distance = 80000 # 80 km ~ 50 mi
        if self.request.get('maxdistance'):
            max_distance = float(self.request.get('maxdistance'))


        try:
            # Can't provide an ordering here in case inequality filters are used.
            base_query = Stop.all()

            # Perform proximity or bounds fetch.
            if query_type == 'proximity':
                results = Stop.proximity_fetch(
                    base_query,
                    center, max_results=max_results, max_distance=max_distance)
            elif query_type == 'bounds':
                results = Stop.bounding_box_fetch(
                    base_query,
                    bounds, max_results=max_results)

            results_obj = [
                {
                    'lat': result.location.lat,
                    'lng': result.location.lon,
                }
                for result in results
            ]

            self.response.out.write(simplejson.dumps({
                'status': 'success',
                'results': results_obj
            }))
        except:
            return _simple_error(str(sys.exc_info()[1]), code=500)
Пример #14
0
from models import Stop, Vehicle
from database import get_cur
import re
import json
import csv
from collections import defaultdict
from geopy.distance import geodesic
from shapely import wkb

cur = get_cur()

stops = []
stops_file = open("stops.csv", "r")
stops_table = csv.reader(stops_file, delimiter=',')
for stops_row in stops_table:
    stop = Stop(stops_row)
    stops.append(stop)


#This gets all the vehicles and puts them into an array of objects.
def populate_vehicles(cur):
    cur.execute("SELECT * FROM vehicles LIMIT 1000;")
    vehicles = []
    vehicles_table = cur.fetchall()
    for vehicles_row in vehicles_table:
        vehicle = Vehicle(vehicles_row)
        vehicles.append(vehicle)
    return vehicles


#This is confusing with get_stops_for_route, need to rename to be less confusing
Пример #15
0
    def post(self):
        if self.request.get('edit-route-entity-key'):
            route_key = ndb.Key(
                urlsafe=str(self.request.get('edit-route-entity-key')))
            new_route = route_key.get()
            routeStops = Stop.query(ancestor=route_key).order(
                Stop.order_number).fetch()

            dictionary = {}
            Stop1 = self.request.get('stop1')
            if self.request.get('stop1-checkbox') == 'on':
                Checkbox1 = True
            else:
                Checkbox1 = False
            dictionary["Stop1"] = Stop1
            dictionary["Checkbox-Stop1"] = Checkbox1
            lastStop = self.request.get('stop2')
            Stop2 = lastStop
            if self.request.get('stop2-checkbox') == 'on':
                Checkbox2 = True
            else:
                Checkbox2 = False
            dictionary["Stop2"] = Stop2
            dictionary["Checkbox-Stop2"] = Checkbox2
            Stop3 = ""
            Stop4 = ""
            Stop5 = ""

            if self.request.get('stop3'):
                lastStop = self.request.get('stop3')
                Stop3 = lastStop
                if self.request.get('stop3-checkbox') == 'on':
                    Checkbox3 = True
                else:
                    Checkbox3 = False
                dictionary["Stop3"] = Stop3
                dictionary["Checkbox-Stop3"] = Checkbox3

            if self.request.get('stop4'):
                lastStop = self.request.get('stop4')
                Stop4 = lastStop
                if self.request.get('stop4-checkbox') == 'on':
                    Checkbox4 = True
                else:
                    Checkbox4 = False
                dictionary["Stop4"] = Stop4
                dictionary["Checkbox-Stop4"] = Checkbox4

            if self.request.get('stop5'):
                lastStop = self.request.get('stop5')
                Stop5 = lastStop
                if self.request.get('stop5-checkbox') == 'on':
                    Checkbox5 = True
                else:
                    Checkbox5 = False
                dictionary["Stop5"] = Stop5
                dictionary["Checkbox-Stop5"] = Checkbox5

            new_route.name = Stop1 + " to " + lastStop

            for i in range(1, (len(dictionary.keys()) / 2) + 1):
                if i < len(routeStops) + 1:
                    routeStops[i - 1].ordered = dictionary["Checkbox-Stop" +
                                                           str(i)]
                    routeStops[i - 1].stop_name = dictionary["Stop" + str(i)]
                    routeStops[i - 1].put()
                else:
                    new_stop = Stop(parent=new_route.key,
                                    route_key=new_route.key,
                                    order_number=i,
                                    stop_name=dictionary["Stop" + str(i)],
                                    ordered=dictionary["Checkbox-Stop" +
                                                       str(i)])
                    new_stop.put()

            new_route.put()
        else:
            firstStop = self.request.get('stop1')
            if self.request.get('stop1-checkbox') == 'on':
                firstCheckbox = True
            else:
                firstCheckbox = False
            lastStop = self.request.get('stop2')
            secondStop = lastStop
            if self.request.get('stop2-checkbox') == 'on':
                secondCheckbox = True
            else:
                secondCheckbox = False
            thirdStop = ""
            fourthStop = ""
            fifthStop = ""

            if self.request.get('stop3'):
                lastStop = self.request.get('stop3')
                thirdStop = lastStop
                if self.request.get('stop3-checkbox') == 'on':
                    thirdCheckbox = True
                else:
                    thirdCheckbox = False
            if self.request.get('stop4'):
                lastStop = self.request.get('stop4')
                fourthStop = lastStop
                if self.request.get('stop4-checkbox') == 'on':
                    fourthCheckbox = True
                else:
                    fourthCheckbox = False

            if self.request.get('stop5'):
                lastStop = self.request.get('stop5')
                fifthStop = lastStop
                if self.request.get('stop5-checkbox') == 'on':
                    fifthCheckbox = True
                else:
                    fifthCheckbox = False

            user = users.get_current_user()
            email = user.email().lower()
            # TODO: Name will change when modal has dynamic number of routes
            # NOTE: Created routes start with type = 0 (not saved)
            # NOTE: Created routes start with daily = 0 (non-recurring)
            new_route = Route(parent=utils.get_parent_key_for_email(email),
                              created_by=email,
                              name=firstStop + " to " + lastStop,
                              type=0,
                              daily=0,
                              start_time=datetime.datetime.now())
            new_route.put()

            new_stop1 = Stop(parent=new_route.key,
                             route_key=new_route.key,
                             order_number=1,
                             stop_name=self.request.get('stop1'),
                             ordered=firstCheckbox)
            new_stop1.put()
            new_stop2 = Stop(parent=new_route.key,
                             route_key=new_route.key,
                             order_number=2,
                             stop_name=self.request.get('stop2'),
                             ordered=secondCheckbox)
            new_stop2.put()
            if thirdStop != "":
                new_stop3 = Stop(parent=new_route.key,
                                 route_key=new_route.key,
                                 order_number=3,
                                 stop_name=self.request.get('stop3'),
                                 ordered=thirdCheckbox)
                new_stop3.put()
            if fourthStop != "":
                new_stop4 = Stop(parent=new_route.key,
                                 route_key=new_route.key,
                                 order_number=4,
                                 stop_name=self.request.get('stop4'),
                                 ordered=fourthCheckbox)
                new_stop4.put()
            if fifthStop != "":
                new_stop5 = Stop(parent=new_route.key,
                                 route_key=new_route.key,
                                 order_number=5,
                                 stop_name=self.request.get('stop5'),
                                 ordered=fifthCheckbox)
                new_stop5.put()

        self.redirect('/'.join(self.request.referer.split("/")[:3]) +
                      "?route=" + str(new_route.key.urlsafe()))
Пример #16
0
#!/usr/bin/env python3
from networks import VRR
from models import Collection, Stop, Location, Trip, unserialize_typed
import json

collection = Collection('test')

vrr = VRR(collection)
bs = Stop(city='essen', name='fliegenbusch')
bo = Stop(city='essen', name='hbf')

trip = Trip.Request()
trip.origin = bs
trip.destination = bo

location = Location.Request()
location.name = 'Borbeck'

# result = vrr.search_trips(trip)

unserialize_typed

result, ids = vrr.query(bo, get_ids=True)

print(json.dumps(collection.get_by_ids_serialized(ids), indent=2))

stops = sorted(vrr.collection.known['Stop'], key=lambda s: s.name)

# for trip in result:
#    print(trip)
# result = vrr.get_stop_rides(bs)