def test_calculate_time_and_distance(self): for test_case in self.test_data: logging.info("Testing route from %s (p0) to %s (p1)" % (test_case["p0"], test_case["p1"])) estimation = calculate_time_and_distance(test_case["p0_lon"], test_case["p0_lat"], test_case["p1_lon"], test_case["p1_lat"]) t, d = float(estimation["estimated_duration"]), float(estimation["estimated_distance"]) expected_t, expected_d = float(test_case["expected_t"]), float(test_case["expected_d"]) logging.info("Time received: %d (expected %d)" % (t, expected_t)) self.assertTrue(1 - self.ERR_RATIO < t/expected_t < 1 + self.ERR_RATIO) logging.info("Distance received: %d (expected %d)" % (d, expected_d)) self.assertTrue(1 - self.ERR_RATIO < d/expected_d < 1 + self.ERR_RATIO)
def estimate_ride_cost(request): from_lon = request.GET["from_lon"] from_lat = request.GET["from_lat"] to_lon = request.GET["to_lon"] to_lat = request.GET["to_lat"] from_city = int(request.GET["from_city"]) to_city = int(request.GET["to_city"]) result = calculate_time_and_distance(from_lon, from_lat, to_lon, to_lat) estimated_duration, estimated_distance = result[ "estimated_duration"], result["estimated_distance"] cities = [from_city, to_city] if not (estimated_distance and estimated_duration): return JSONResponse({ "estimated_cost": "", "estimated_duration": "", "currency": "", "label": _("Ride estimation not available") }) ride_cost, ride_type = estimate_cost(estimated_duration, estimated_distance, cities=cities) if ride_type == CostType.METER: label = _('Estimated ride cost') cost = "%d-%d" % (ride_cost, ride_cost * (1 + ESTIMATION_FUZZINESS_FACTOR)) elif ride_type == CostType.FLAT: label = _('Flat rate') cost = "%d" % ride_cost result = { "estimated_cost": cost, "estimated_duration": "%d-%d %s" % (estimated_duration / 60, estimated_duration * (1 + ESTIMATION_FUZZINESS_FACTOR) / 60, _('min')), "currency": u'₪', # TODO_WB: change to non hardcoded version "label": label } return JSONResponse(result)
def test_calculate_time_and_distance(self): for test_case in self.test_data: logging.info("Testing route from %s (p0) to %s (p1)" % (test_case["p0"], test_case["p1"])) estimation = calculate_time_and_distance(test_case["p0_lon"], test_case["p0_lat"], test_case["p1_lon"], test_case["p1_lat"]) t, d = float(estimation["estimated_duration"]), float( estimation["estimated_distance"]) expected_t, expected_d = float(test_case["expected_t"]), float( test_case["expected_d"]) logging.info("Time received: %d (expected %d)" % (t, expected_t)) self.assertTrue( 1 - self.ERR_RATIO < t / expected_t < 1 + self.ERR_RATIO) logging.info("Distance received: %d (expected %d)" % (d, expected_d)) self.assertTrue( 1 - self.ERR_RATIO < d / expected_d < 1 + self.ERR_RATIO)
def test_routing_service_task(request): failures_counter = "routing_test_failures" num_strikes = 3 l = len(TEL_AVIV_POINTS) success = True err_msg = "" try: for i, p in enumerate(TEL_AVIV_POINTS): q = TEL_AVIV_POINTS[(i + 1) % l] result = calculate_time_and_distance(p["lon"], p["lat"], q["lon"], q["lat"]) if not (result["estimated_distance"] and result["estimated_duration"]): err_msg += "calculate_time_and_distance failed for: p=(%s, %s), q=(%s, %s)\n" % ( p["lon"], p["lat"], q["lon"], q["lat"]) success = False except Exception, e: success = False err_msg = "%s\n There was an exception: %s\nTraceback:%s" % ( err_msg, e, traceback.format_exc())
def create(self, request, *args, **kwargs): request_data = request.data.get("request") for address_type in ('from', 'to'): for att, val in request_data[address_type].items(): request_data[address_type + "_" + att] = val or "" if not is_valid_address(request_data, address_type): res = rc.BAD_REQUEST res.write(" %s\n" % ErrorCodes.INVALID_ADDRESS) return res try: from_lon = request_data["from_lon"] from_lat = request_data["from_lat"] to_lon = request_data["to_lon"] to_lat = request_data["to_lat"] except KeyError: return rc.BAD_REQUEST result = calculate_time_and_distance(from_lon, from_lat, to_lon, to_lat) return { "ride_estimate_result": result }
def estimate_ride_cost(request): from_lon = request.GET["from_lon"] from_lat = request.GET["from_lat"] to_lon = request.GET["to_lon"] to_lat = request.GET["to_lat"] from_city = int(request.GET["from_city"]) to_city = int(request.GET["to_city"]) result = calculate_time_and_distance(from_lon, from_lat, to_lon, to_lat) estimated_duration, estimated_distance = result["estimated_duration"], result["estimated_distance"] cities = [from_city, to_city] if not (estimated_distance and estimated_duration): return JSONResponse( { "estimated_cost": "", "estimated_duration": "", "currency": "", "label": _("Ride estimation not available"), } ) ride_cost, ride_type = estimate_cost(estimated_duration, estimated_distance, cities=cities) if ride_type == CostType.METER: label = _("Estimated ride cost") cost = "%d-%d" % (ride_cost, ride_cost * (1 + ESTIMATION_FUZZINESS_FACTOR)) elif ride_type == CostType.FLAT: label = _("Flat rate") cost = "%d" % ride_cost result = { "estimated_cost": cost, "estimated_duration": "%d-%d %s" % (estimated_duration / 60, estimated_duration * (1 + ESTIMATION_FUZZINESS_FACTOR) / 60, _("min")), "currency": u"₪", # TODO_WB: change to non hardcoded version "label": label, } return JSONResponse(result)