def do_get_park_and_rides(): pr_json = RdwApi.get_park_and_ride_json() area_json = RdwApi.get_area_json() RdwApi.parkings = [] # print('found ' + str(len(pr_json)) + ' parkings') # print('found ' + str(len(area_json)) + ' areas') for item in pr_json: try: areaid = item['areaid'] area = next(a for a in area_json if a['areaid'] == areaid) # area['areageometryastext'] = 'POINT (4.382199252 51.884720263)' geo_text = area['areageometryastext'].translate( str.maketrans('', '', '()')).split() if geo_text[0] == 'POINT': loc_name = item['areadesc'].replace( '&', '&' ) # unfortunately there are xml-escapes in the json... parking = Location( loc_name, (float(geo_text[2]), float(geo_text[1]))) # reversed! parking.has_parking = True RdwApi.parkings.append(parking) else: print('no point in ' < str(geo_text[0])) except (StopIteration, KeyError, IndexError): pass # for p in parkings: # print(str(p) + ' at ' + str(p.gps)) # print(str(len(parkings)) + ' parkings have a location') RdwApi.last_update = datetime.datetime.now()
def test(self): noon = noon_today() start = Point(Location('Madurodam'), noon) end = Point(Location('Martinitoren'), noon) segments = [] self.generator.create_edges(start, end, FixTime.START, segments) self.assertEqual(len(segments), 4)
def get_context_data(self, start, end, timestamp, **kwargs): user, user_preferences = PlanView.get_user_preferences(self.request) PlanView.register_request(start, end, user) request_start = time.time() fix_time = FixTime.START if timestamp[-1].isalpha: if timestamp[-1] == 'a': fix_time = FixTime.END timestamp = timestamp[:-1] if not timestamp or timestamp == '0': req_time = datetime.datetime.now() else: req_time = datetime.datetime.fromtimestamp(60 * float(timestamp)) p = Planner() start_loc = Location(start) end_loc = Location(end) plans = p.solve(start_loc, end_loc, req_time, fix_time, user_preferences) results = PlanView.get_results(plans) request_end = time.time() Request.objects.create(user=user, start=start, end=end, timedelta=request_end - request_start) context = super().get_context_data() context['start'] = start context['end'] = end context['arrive_by'] = fix_time == FixTime.END context['results'] = results return context
def travel_time_and_map_url(start, end, mode): # because openrouteservice has trouble finding bike-routes, e.g. from Utrecht Centraal, we try to find other locations # where we can bike from jump_closer_distance = 100 bicycle_speed = 18 / 3.6 local_start = deepcopy(start) local_end = deepcopy(end) for i in range(0, 3): try: if i != 0: logger.info('Trying again with locations ' + local_start.full_str() + ', ' + local_end.full_str()) extra_time = i * jump_closer_distance / bicycle_speed time_sec = try_travel_time(local_start, local_end, mode) + extra_time return time_sec, map_url(local_start, local_end, mode) except ConnectionNotFoundError as error: distance = vincenty(local_start.gps, local_end.gps).meters new_start = Location.midpoint(local_start, local_end, jump_closer_distance / distance) new_end = Location.midpoint(local_start, local_end, (distance - jump_closer_distance) / distance) local_start = new_start local_end = new_end logger.error('Giving up on finding a route') return 99999, map_url(local_start, local_end, mode) # no bike route found, please ignore
def testAvoidHighway(self): start = Location('Madurodam') end = Location('Martinitoren') travel1 = self.tomtom.travel_time(TomTomApi.RouteParams(start, end, avoid_highways=True))[0] self.assertGreater(travel1, 8442) self.assertGreater(travel1, 4 * 3600) self.assertLess(travel1, 6 * 3600)
def test_map_url(self): start = Location('Madurodam') end = Location('Martinitoren') travel1, url = openrouteserviceapi.travel_time_and_map_url(start, end, TransportType.CAR) # note that yournavigation.org DOES care about order: lat must come before lon self.assertEqual(url, 'https://maps.openrouteservice.org/directions?n3=9&a=52.0993%2C4.2986%2C53.21934%2C6.56817&b=1a&c=0&g1=-1&g2=0&h2=3&k1=en-US&k2=km') travel1, url = openrouteserviceapi.travel_time_and_map_url(start, end, TransportType.BIKE) self.assertEqual(url, 'https://maps.openrouteservice.org/directions?n3=9&a=52.0993%2C4.2986%2C53.21934%2C6.56817&b=1a&c=0&g1=-1&g2=0&h2=3&k1=en-US&k2=km') travel1, url = openrouteserviceapi.travel_time_and_map_url(start, end, TransportType.WALK) self.assertEqual(url, 'https://maps.openrouteservice.org/directions?n3=9&a=52.0993%2C4.2986%2C53.21934%2C6.56817&b=1a&c=0&g1=-1&g2=0&h2=3&k1=en-US&k2=km')
def test_routing(self): start = Location('Madurodam') end = Location('Martinitoren') travel1, map_url = openrouteserviceapi.travel_time_and_map_url(start, end, TransportType.CAR) # print(travel1) self.assertGreater(travel1, 2 * 3600) self.assertLess(travel1, 3 * 3600) travel2, map_url = openrouteserviceapi.travel_time_and_map_url(start, end, TransportType.BIKE) # print(travel2) self.assertGreater(travel2, 10 * 3600) self.assertLess(travel2, 15 * 3600)
def test_mid(self): loc1 = Location('a', (0.5, 1.5)) loc2 = Location('b', (1.5, 3.5)) mid1 = Location.midpoint(loc1, loc2) self.assertEqual(mid1.loc_str, 'mid_a_b') self.assertEqual(mid1.gps, (1.0, 2.5)) mid2 = Location.midpoint(loc1, loc2, 0.75) self.assertEqual(mid2.loc_str, 'mid_a_b') self.assertEqual(mid2.gps, (1.25, 3.0))
def test_no_short_bike_rides(self): noon = noon_today() plans = self.planner.solve(Location('Centraal Museum'), Location('Oldambt utrecht'), noon, FixTime.START) for p in plans: for s in p.route: if s.transport_type in (TransportType.BIKE, TransportType.OVFIETS): self.assertGreater(s.to_vertex.time - s.from_vertex.time, timedelta(minutes=1, seconds=55))
def test_map_url(self): start = Location('Madurodam') end = Location('Martinitoren') travel1, url = yoursapi.travel_time_and_map_url(start, end, TransportType.CAR) # note that yournavigation.org DOES care about order: lat must come before lon self.assertEqual(url,'http://yournavigation.org/?flat=52.0993&flon=4.2986&tlat=53.21934&tlon=6.56817&v=motorcar&fast=1&layer=mapnik') url = url.replace('&', '?') words = sorted(url.split('?')) self.assertEqual(words, ['fast=1','flat=52.0993','flon=4.2986','http://yournavigation.org/','layer=mapnik','tlat=53.21934','tlon=6.56817','v=motorcar']) travel1, url = yoursapi.travel_time_and_map_url(start, end, TransportType.BIKE) self.assertEqual(url,'http://yournavigation.org/?flat=52.0993&flon=4.2986&tlat=53.21934&tlon=6.56817&v=bicycle&fast=1&layer=mapnik') url = url.replace('&', '?') words = sorted(url.split('?')) self.assertEqual(words, ['fast=1','flat=52.0993','flon=4.2986','http://yournavigation.org/','layer=mapnik','tlat=53.21934','tlon=6.56817','v=bicycle'])
def test_location_holder(self): a = Location('location a', (0, 0)) a1 = Location('location a', (0, 0)) a2 = Location('location a', (0.0002, 0.0001)) a3 = Location('location a', (0.002, 0.001)) b = Location('location b', (0, 0)) b1 = Location('loca tion b', (0, 0)) b2 = Location('loca tion b', (0.0002, 0.0001)) b3 = Location('loca tion b', (0.002, 0.001)) lh = LocationHolder() test_a = lh.process(a) test_a1 = lh.process(a1) test_a2 = lh.process(a2) test_a3 = lh.process(a3) self.assertTrue(test_a is a) self.assertFalse(test_a1 is a1) self.assertFalse(test_a2 is a2) self.assertTrue(test_a3 is a3) test_b = lh.process(b) test_b1 = lh.process(b1) test_b2 = lh.process(b2) test_b3 = lh.process(b3) self.assertTrue(test_b is b) self.assertFalse(test_b1 is b1) self.assertFalse(test_b2 is b2) self.assertTrue(test_b3 is b3)
def test(self): noon = noon_today() plans = self.planner.solve(Location('Den Haag'), Location('Nieuwegein'), noon, FixTime.START) results = PlanView.get_results(plans) # for p in plans: # print (list(map(str,p))) self.assertGreater(len(plans), 2) self.assertGreater(results[0]['travel_time_min'], 20) self.assertLess(results[0]['travel_time_min'], 120) for p in plans: for s in p.route: if s.transport_type in (TransportType.BIKE, TransportType.CAR): self.assertNotEqual(s.map_url, '')
def test_doubles(self): noon = noon_today() plans = self.planner.solve(Location('Den haag centraal'), Location('Utrecht centraal'), noon, FixTime.START) segments = set() for p in plans: segments.update(p.route) for s1 in segments: for s2 in segments: if not s1 is s2: self.assertFalse( s1.from_vertex == s2.from_vertex and s1.to_vertex == s2.to_vertex and s1.transport_type == s2.transport_type)
def validate_location(value): location = Location(value) if not location.gps or location.gps == (0, 0): raise ValidationError( '{0} is not a valid location'.format(value), params={'value': value}, )
def finish(self, edges): loc_1 = Location.midpoint(self.start.location, self.end.location, 0.01) loc_2 = Location.midpoint(self.start.location, self.end.location, 0.5) loc_3 = Location.midpoint(self.start.location, self.end.location, 0.99) loc_4 = self.end.location if self.fix_time == FixTime.START: start_time = self.start.time else: start_time = self.end.time - timedelta(hours=1) time_1 = start_time + 0.25 * timedelta(hours=1) time_2 = start_time + 0.5 * timedelta(hours=1) time_3 = start_time + 0.75 * timedelta(hours=1) time_4 = start_time + 0.99 * timedelta(hours=1) p1 = Point(loc_1, time_1) p2 = Point(loc_2, time_2) p3 = Point(loc_3, time_3) p4 = Point(loc_4, time_4) edges += [ Segment(TransportType.BUS, p1, p2), Segment(TransportType.TRAIN, p2, p3), Segment(TransportType.WALK, p3, p4), ]
def test(self): loc_utr = Location('Utrecht, Varkenmarkt') loc_ams = Location('Amsterdam, Damrak') noon = datetime(year=2017, month=11, day=17, hour=12) start1 = Point(location=loc_utr, time=noon) end1 = Point(location=loc_ams, time=noon) start2 = Point(location=loc_utr, time=noon) end2 = Point(location=loc_ams, time=noon) generator = WalkGenerator() segment, new_point = generator.create_segment(start1, end1, FixTime.START, TransportType.WALK) segment2, new_point2 = generator.create_segment(start2, end2, FixTime.END, TransportType.WALK) self.assertEqual(segment.transport_type, TransportType.WALK) self.assertEqual(segment.from_vertex.location, loc_utr) self.assertEqual(segment.to_vertex.location, loc_ams) self.assertEqual(segment.from_vertex.time, noon) self.assertEqual(new_point.time, datetime(2017, 11, 17, 21, 53, 14, 79125)) self.assertEqual(new_point2.time, datetime(2017, 11, 17, 2, 6, 45, 920875)) self.assertEqual(segment2.to_vertex.time, noon) segment, new_point = generator.create_segment(start1, end1, FixTime.START, TransportType.BIKE) segment2, new_point2 = generator.create_segment(start2, end2, FixTime.END, TransportType.BIKE) self.assertEqual(segment.transport_type, TransportType.BIKE) self.assertEqual(segment.from_vertex.location, loc_utr) self.assertEqual(segment.to_vertex.location, loc_ams) self.assertEqual(segment.from_vertex.time, noon) self.assertAlmostEqual(new_point.time, datetime(2017, 11, 17, 14, 16, 48), delta=timedelta(seconds=1200)) self.assertAlmostEqual(new_point2.time, datetime(2017, 11, 17, 9, 43, 12), delta=timedelta(seconds=1200)) self.assertEqual(segment2.to_vertex.time, noon) loc_bhd = Location('sloterweg 183, badhoevedorp', (52.33668,4.77682)) loc_mee = Location('Station Meerssen', (51.1923957995,5.99421143532)) start = Point(location=loc_bhd, time=noon) end = Point(location=loc_mee, time=noon) segment, new_point = generator.create_segment(start, end, FixTime.START, TransportType.BIKE) self.assertEqual(segment.transport_type, TransportType.BIKE) self.assertEqual(segment.from_vertex.location, loc_bhd) self.assertEqual(segment.to_vertex.location, loc_mee) self.assertGreater(segment.to_vertex.time - segment.from_vertex.time, timedelta(hours=4)) self.assertLess(segment.to_vertex.time - segment.from_vertex.time, timedelta(hours=14)) loc_bhd = Location('parkzichtlaan', (52.10073, 5.03322)) loc_mee = Location('Utrecht Centraal', (52.089820488, 5.10957062244)) start = Point(location=loc_bhd, time=noon) end = Point(location=loc_mee, time=noon) segment, new_point = generator.create_segment(start, end, FixTime.START, TransportType.BIKE) self.assertEqual(segment.transport_type, TransportType.BIKE) self.assertEqual(segment.from_vertex.location, loc_bhd) self.assertEqual(segment.to_vertex.location, loc_mee) self.assertGreater(segment.to_vertex.time - segment.from_vertex.time, timedelta(minutes=10)) self.assertLess(segment.to_vertex.time - segment.from_vertex.time, timedelta(minutes=30))
def test_points(self): # location and point are value classes l1 = Location('a', (2, 3)) l2 = Location('a', (2, 3)) l3 = Location('b', (2, 3)) l4 = Location('a', (3, 3)) t0 = datetime(year=2000, month=1, day=1) t1 = t0 + timedelta(seconds=60) p1 = Point(l1, t0) p2 = Point(l2, t0) p3 = Point(l3, t0) p4 = Point(l4, t0) p5 = Point(l1, t1) p6 = Point(l2, t1) self.assertFalse(l1 is l2) self.assertTrue(l1 == l2) self.assertFalse(p1 is p2) self.assertTrue(p1 == p2) self.assertFalse(l1 == l3) self.assertFalse(l1 == l4) self.assertFalse(p1 == p3) self.assertFalse(p1 == p4) self.assertTrue(p1.location == p5.location) self.assertTrue(p2.location == p6.location) self.assertFalse(p1 == p5) self.assertFalse(p2 == p6) ll = [l1, l3] lp = [p1, p3] self.assertTrue(l1 in ll) self.assertTrue(l2 in ll) self.assertTrue(l3 in ll) self.assertFalse(l4 in ll) self.assertTrue(p1 in lp) self.assertTrue(p2 in lp) self.assertTrue(p3 in lp) self.assertFalse(p4 in lp) sl = set([l1, l2, l3, l4]) sp = set([p1, p2, p3, p4, p5, p6]) self.assertEqual(len(sl), 3) self.assertEqual(len(sp), 4) self.assertTrue(Location('a', (2, 3)) in sl) self.assertTrue(Point(l1, t0) in sp) self.assertFalse(Location('b', (3, 3)) in sl) self.assertFalse(Point(l3, t1) in sp)
def testStations(self): begin = Location('Madurodam') end = Location('Martinitoren') start_time = datetime.datetime.combine(datetime.date.today(), datetime.time(12)) self.monotch.add_search_request(begin, end, start_time, FixTime.START)
def create_location(self, loc_str, gps=None): return self.process(Location(loc_str, gps))
def solve(self, start_loc_str, start_hike_time, user_preferences=UserTravelPreferences()): logger.info('BEGIN') log_start = time.time() start = Point(Location(start_loc_str), start_hike_time - timedelta(hours=12)) trail = get_trail(start.location) hike_start_loc = Location('wandeling vertrek', (trail.begin_lat, trail.begin_lon)) hike_end_loc = Location('wandeling aankomst', (trail.end_lat, trail.end_lon)) if hike_start_loc.distance_to(hike_end_loc).meters < 100: hike_end_loc = hike_start_loc hike_start_loc.loc_str = 'wandeling vertrek/aankomst' hike_start = Point(hike_start_loc, start_hike_time) hike_end = Point( hike_end_loc, start_hike_time + timedelta(seconds=trail.distance / WALKING_SPEED)) end = Point(Location(start_loc_str), hike_end.time + timedelta(hours=12)) logger.info('start=' + str(start)) logger.info('hike_start=' + str(hike_start)) logger.info('hike_end=' + str(hike_end)) logger.info('end=' + str(end)) segments = self.generator.create_edges(start, hike_start, FixTime.END) segments += self.generator.create_edges(hike_end, end, FixTime.START) hiking_segment = Segment(TransportType.WALK, hike_start, hike_end) hiking_segment.route_name = trail.title hiking_segment.map_url = trail.wandelpagina_url segments.append(hiking_segment) # for e in segments: # logger.info('Edge: ' + str(e)) vehicle_positions = VehiclePositions() # put a bicycle and car at home... vehicle_positions.add_vehicle( start.location, Vehicle(TransportType.BIKE, VehicleType.BIKE)) vehicle_positions.add_vehicle( start.location, Vehicle(TransportType.CAR, VehicleType.CAR)) # add OV fietsen ov_fiets_stations = OvFietsStations() vehicle_positions = ov_fiets_stations.add_default_ovfiets_positions( vehicle_positions, segments) plans = self.router.make_plans(start, end, segments, vehicle_positions) plans = list(filter(Planner.has_no_double_biking, plans)) order_and_select(plans, user_preferences, None) Planner.remove_unnecessary_waiting(plans, FixTime.START) log_end = time.time() logger.info('END - time: ' + str(log_end - log_start)) return plans, trail
def test_closest_stations(self): location = Location('Madurodam') self.assertIsNotNone(location.gps) closest = [station.naam for station in self.generator.closest_stations(location, 3)] expected = ['Den Haag Centraal', 'Den Haag HS', 'Den Haag Laan v NOI'] self.assertEqual(closest, expected)
def gen_points(req_time, fix_time): start_time = req_time if fix_time == FixTime.START else req_time-timedelta(hours=10) end_time = req_time if fix_time == FixTime.END else req_time+timedelta(hours=10) start = Point(Location('Den Haag'), start_time) end = Point(Location('Nieuwegein'), end_time) return start, end
def test_random(self): for i in range(0, 50): t = planner.get_trail(Location('home', (0, 0)))
def test_map_url(self): start = Location('Utrecht') end = Location('Amsterdam') url = self.tomtom.map_url(TomTomApi.RouteParams(start=start, end=end)) self.assertEqual(url,'https://mydrive.tomtom.com/nl_nl/#mode=routes+routes={"departure":true,"traffic":true,"routeType":"FASTEST","travelMode":"CAR","points":["hw~52.09126,5.12275","hw~52.37317,4.89066"],"avoidCriteria":[]}+ver=3')
def test_basic(self): loc = Location('Utrecht') self.assertEqual(loc.gps, (52.09126, 5.12275)) loc2 = Location('ddd-unknown-location-ddsfaasdfasdf') self.assertEqual(loc2.gps, None)
def testRouting(self): start = Location('Madurodam') end = Location('Martinitoren') travel1 = self.tomtom.travel_time(TomTomApi.RouteParams(start, end))[0] self.assertGreater(travel1, 2 * 3600) self.assertLess(travel1, 4 * 3600)