def compute_geometry(self): from_n = osrm.Point(latitude=self.from_node.lat, longitude=self.from_node.lon) to_n = osrm.Point(latitude=self.to_node.lat, longitude=self.to_node.lon) result = osrm.simple_route( from_n, to_n) #, output='route', overview="full", geometry='wkt') # https://github.com/ustroetz/python-osrm self.geometry = result['routes'][0]['geometry']
def assign_distance_between_neighbors(node, neighbor_name_coords): # get node coords node_point = osrm.Point(latitude=node.lat, longitude=node.lon) # get neighbor coords neighbor_name, neighbor_lon, neighbor_lat = neighbor_name_coords neighbor_point = osrm.Point(latitude=neighbor_lat, longitude=neighbor_lon) # find distance between the two full_route = osrm.simple_route(node_point, neighbor_point) dist = full_route['routes'][0]['distance'] # populate node with the neighbor and the distance between the two. node.set_new_neighbor_dist(neighbor_name, dist) # populate node with the neighbor and the duration between the two. dur = full_route['routes'][0]['duration'] node.set_new_neighbor_dur(neighbor_name, dur)
def test_helpers(self): _list = [i for i in osrm._chain([789, 45], [78, 96], [7878, 789, 36])] self.assertEqual(_list, [789, 45, 78, 96, 7878, 789, 36]) p1 = osrm.Point(latitude=10.00, longitude=53.55) self.assertEqual(p1.longitude, p1[0]) self.assertEqual(p1.latitude, p1[1])
def test_accessibility(self, mock_urlopen): mock_urlopen.return_value = MockReadable( u'''{"code":"Ok","durations":[[6924.8,6389.6,7313.6,8356.7,9011,6253.4,6054.6,4462.2,6133.3,6779.8,6138.5,2289.3,2414.2,2472.8,3920.2,4375.4,5580.1,6506.5,5472.3,1657.7,1648.1,2197.3,2938.4,4451.1,4721.7,4198.7,5044.5,1034.4,729.7,2290.2,4911.3,4353.3,4244.2,3712.6,1610.5,676,704.2,3083.6,6121.6,6132.9,3625.2,3131.4,2042.5,1637.5,1635.3,5396.5,7891.1,5753.5,4248.4,4272.6,3064.2,2459.2,2090.2,4464.3,9555.2,6675.5,4861.1,4776.1,3054.2,2336.8,3072.6,4258.5,6444.1,9452.4]],"destinations":[{"hint":"K9UHiUTVB4nG6PIAAAAAAA4AAADtAAAAHAAAAMD7xAZl_MQGE_sAADbpOwF-RIYCOPU7AahFhgIJAAEB9TrYrw==","name":"Retij\xc3\xab - Opterush\xc3\xab","location":[20.703542,42.35379]},{"hint":"9PGug7rB-4gAAAAAFQAAAAoAAABXAwAAewAAAJ9sigdmVb8GE_sAAArvOwFOt4QCOPU7AQi_hAIZAAEB9TrYrw==","name":"","location":[20.705034,42.25211]},{"hint":"S4wXiVaMF4kAAAAAAAAAAA8AAAAAAAAAEAAAAPLlzQbz5c0GE_sAAHbtOwH_TIMCOPU7AWg4gwIAAAEB9TrYrw==","name":"","location":[20.70463,42.159359]},{"hint":"eFX9iO3_lIsAAAAAAAAAABIAAAAAAAAAHwAAAAwNwAb7DMAGE_sAADffOwHzsYECOPU7AcixgQIAAAEB9TrYrw==","name":"","location":[20.700983,42.054131]},{"hint":"m4QXiaD1OY4AAAAAFgAAAAAAAAALAQAAAAAAABbizQaW4c0GE_sAAO8HPAGEVIACOPU7ASgrgAIHAAEB9TrYrw==","name":"","location":[20.711407,41.964676]},{"hint":"f9wYi4HcGIsAAAAAMQAAAAAAAABZCAAAAAAAAES9tAc8vbQHE_sAAAP6OgHErH4COPU7AYikfgJlAAEB9TrYrw==","name":"","location":[20.642307,41.856196]},{"hint":"CmwBig1sAYoAAAAAEQAAAAoAAAB7NgAAZgAAAAE2BQTo6T4HE_sAAO_iOwFmLH0COPU7AegdfQJNAQEB9TrYrw==","name":"","location":[20.701935,41.757798]},{"hint":"REauhG7WqIlUUp4AEAAAAAAAAAAQCQAABxsAAKeNMgEdx-4DE_sAAG4IPAGil3sCOPU7AUiXewIAAQEB9TrYrw==","name":"R2238","location":[20.711534,41.654178]},{"hint":"63_iie1_4okAAAAAEQAAABAAAAAfAAAAQwAAAFwwMQc_MDEHE_sAAOh9PQGGP4YC2Hs9AahFhgIBAAEB9TrYrw==","name":"","location":[20.807144,42.352518]},{"hint":"6bLaiQiz2okAAAAAAAAAAAkAAAAAAAAAjQAAADq3LQexty0HE_sAAP1dPQH_yYQC2Hs9AQi_hAIAAAEB9TrYrw==","name":"","location":[20.798973,42.256895]},{"hint":"FD8NikM_DYoAAAAAAAAAABUAAACVCgAArwkAALmjRAeao0QHE_sAAPOAPQGYNoMC2Hs9AWg4gwKwAAEB9TrYrw==","name":"","location":[20.807923,42.153624]},{"hint":"akb8iYWPAYoAAAAAAAAAABUAAACVCQAAAAAAADYNQgM9IHkBE_sAAPTNPQFetIEC2Hs9AcixgQJQAAEB9TrYrw==","name":"","location":[20.827636,42.05475]},{"hint":"_9D0iS6HAYoAAAAAFQAAAAAAAACGBAAAAAAAAOZaOQeu-D4HE_sAALNJPgEHB4AC2Hs9ASgrgAJLAAEB9TrYrw==","name":"","location":[20.859315,41.944839]},{"hint":"E4cBihiHAYoAAAAAAAAAACoAAAAAAAAAvQEAAMX4PgfE-D4HE_sAAM9ePgHBk34C2Hs9AYikfgIAAAEB9TrYrw==","name":"","location":[20.864719,41.849793]},{"hint":"B3QBinegG48AAAAAAAAAABEAAADJBQAAfQAAADv1xAg89cQIE_sAANJ3PQEBNX0C2Hs9AegdfQI2AAEB9TrYrw==","name":"","location":[20.805586,41.760001]},{"hint":"4ThEjuI4RI4AAAAAAAAAAAQAAAAWAAAAfAAAAK-srgKurK4CE_sAAGYHPQEA6HsC2Hs9AUiXewIBAAEB9TrYrw==","name":"","location":[20.776806,41.674752]},{"hint":"Jc3uiV3N7okAAAAACgAAAAAAAADOAAAA6gAAAISXNgf8ljYHE_sAAFj1PgEqVIYCeAI_AahFhgINAAEB9TrYrw==","name":"","location":[20.903256,42.357802]},{"hint":"OYTiidYe54kAAAAALwAAAAAAAACpAQAAAAAAAK4yMQf3KAMGE_sAAHk0PwHHJ4UCeAI_AQi_hAIXAAEB9TrYrw==","name":"","location":[20.919417,42.280903]},{"hint":"6hg2iiE1lo8AAAAAFwAAAAAAAACVAgAAAAAAAGd5KgM8oc0BE_sAAMrTPgFGdIMCeAI_AWg4gwIhAAEB9TrYrw==","name":"","location":[20.894666,42.169414]},{"hint":"AUD8iYKPAYp7gf8AAAAAAAwAAABuAwAA_AUAAOt5PAd8eTwHE_sAAO_wPgGaq4ECeAI_AcixgQJKAAEB9TrYrw==","name":"R29275","location":[20.902127,42.052506]},{"hint":"6cb0iYLH9IkAAAAAAAAAAAkAAACVDQAAdQUAAIxWOQfmVTkHE_sAAE0DPwG4KoACeAI_ASgrgAJsAQEB9TrYrw==","name":"","location":[20.906829,41.953976]},{"hint":"ZlPliQxCMIwAAAAAJQAAAAAAAACjBAAATAMAAP-OFwj6jhcIE_sAAC0BPwE1oH4CeAI_AYikfgIiAAEB9TrYrw==","name":"","location":[20.906285,41.852981]},{"hint":"5zwQisOwNYwAAAAAGAAAAAAAAADcCQAAaAYAAB_9RQfr_UUHE_sAAJbNPgF7MH0CeAI_AegdfQJQAAEB9TrYrw==","name":"","location":[20.893078,41.758843]},{"hint":"Q696i-NmOY4AAAAAHAAAAAAAAADLBwAAAAAAAJyzmQiis5kIE_sAANAMPwEYXXsCeAI_AUiXewJAAAEB9TrYrw==","name":"","location":[20.909264,41.639192]},{"hint":"IFKwh0PdGougcQsBCgAAAAAAAAC3HgAAiAUAAETAWwQ6kbUHE_sAANKTQAHHVoYCGIlAAahFhgKpAQEB9TrYrw==","name":"R-206 (206)","location":[21.009362,42.358471]},{"hint":"RYkii5OJIosAAAAACAAAAAAAAABnAAAAAAAAADaXuAdOlrgHE_sAAAO-QAFjn4QCGIlAAQi_hAILAAEB9TrYrw==","name":"","location":[21.020163,42.245987]},{"hint":"y6LrirscNYsAAAAAPAAAAAAAAABTBAAAAAAAANxQiAJidCUDE_sAAK4AQAHASYMCGIlAAWg4gwIJAAEB9TrYrw==","name":"","location":[20.971694,42.158528]},{"hint":"FbJ_i9TrgosAAAAAAAAAABIAAACwBQAA6AkAAH87LQMXOdsHE_sAAOuFQAGesoECGIlAAcixgQKHAAEB9TrYrw==","name":"","location":[21.005803,42.054302]},{"hint":"idevhZobsIUAAAAAAAAAACQAAAB2CAAA9gUAAMqxwARzTsUEE_sAAOegQAGrLYACGIlAASgrgAJGAAEB9TrYrw==","name":"","location":[21.012711,41.954731]},{"hint":"npeRjFCYkYwAAAAAAAAAABIAAAB4AwAAkwEAABWUdQfbXDIIE_sAACJ2QAFagH4CGIlAAYikfgIMAAEB9TrYrw==","name":"","location":[21.001762,41.844826]},{"hint":"ajW4i7KPHo8AAAAAAAAAADYAAADRNgAAmyYAAN_27wem-O8HE_sAAMesQAGyOn0CGIlAAegdfQK8AgEB9TrYrw==","name":"","location":[21.015751,41.761458]},{"hint":"Ls3fid5Z5YkAAAAAHAAAAAAAAAA-FAAAjwUAAIp6MgeDejIHE_sAAAmTQAFap3sCGIlAAUiXewLZAAEB9TrYrw==","name":"","location":[21.009161,41.658202]},{"hint":"OrLrijHyKY4AAAAANQAAAAAAAABhAAAAlQIAAA79ogcX_aIHE_sAADEOQgHnJ4YCuA9CAahFhgICAAEB9TrYrw==","name":"","location":[21.106225,42.346471]},{"hint":"eMOhj4vDoY8AAAAAMgAAAAAAAAAbAQAAkwEAAFq3qgQ9k5kDE_sAAI8RQgFCyoQCuA9CAQi_hAIHAAEB9TrYrw==","name":"","location":[21.107087,42.256962]},{"hint":"eqQrhKqkK4QAAAAAAAAAACgAAAASAwAAhwAAALB_ggOyv4EDE_sAAC_0QQEUFoMCuA9CAWg4gwILAAEB9TrYrw==","name":"","location":[21.099567,42.1453]},{"hint":"upohhIiZ54wAAAAAHAAAAAAAAACLAgAAfgAAAFRvSQhJvfoAE_sAAD4cQgFyr4ECuA9CAcixgQILAAEB9TrYrw==","name":"","location":[21.109822,42.05349]},{"hint":"FoLljEuC5YwAAAAAAAAAADQAAADRBQAAKwQAAHdKyQUplwQDE_sAAP_8QQE_PYACuA9CASgrgAIcAAEB9TrYrw==","name":"","location":[21.101823,41.958719]},{"hint":"xYzljMiM5YwAAAAAAAAAADQAAAC9AgAAAQQAANHoSAjpCI4DE_sAAC0EQgH_n34CuA9CAYikfgIUAAEB9TrYrw==","name":"","location":[21.103661,41.852927]},{"hint":"azm4i9GJHY8AAAAAAAAAAA4AAACyBwAAxQAAAK3tSAhv-O8HE_sAAHT0QQHsDn0CuA9CAegdfQI_AAEB9TrYrw==","name":"","location":[21.099636,41.750252]},{"hint":"HojmjC6I5owAAAAAFwAAAAAAAAC5GAAAcgAAAE41SQhMNEkIE_sAAM1uQgGUqHsCuA9CAUiXewLQAAEB9TrYrw==","name":"","location":[21.130957,41.658516]},{"hint":"opfEjovLz46xMpEAPgAAADwAAAB5AgAAHQMAAFFVGgN0aI8DE_sAAMacQwHoOoYCWJZDAahFhgIHAAEB9TrYrw==","name":"KFORIT","location":[21.208262,42.351336]},{"hint":"VAmgj1cJoI8AAAAAMgAAAAAAAAAAAwAAAAAAAJ9tmANsqbIHE_sAABiJQwF_vIQCWJZDAQi_hAIOAAEB9TrYrw==","name":"","location":[21.203224,42.253439]},{"hint":"EmmsjxRprI8AAAAADAAAAAAAAADPAAAAAAAAAOpqWQPpalkDE_sAAHZfQwEjXIMCWJZDAWg4gwIHAAEB9TrYrw==","name":"","location":[21.192566,42.163235]},{"hint":"oVIUi6VSFIsAAAAAAAAAADYAAAAAAAAA6QAAAELtsgeu67IHE_sAAMfOQwGWFYICWJZDAcixgQIAAAEB9TrYrw==","name":"","location":[21.221063,42.079638]},{"hint":"ZRspi3QbKYsAAAAAAAAAAAkAAABrAAAA8Q4AAD39ugc3_LoHE_sAAJemQwG8KYACWJZDASgrgAIHAAEB9TrYrw==","name":"","location":[21.210775,41.953724]},{"hint":"5thjiSwHnIkAAAAAAAAAAAoAAADwBAAA0TEAADTH9gZrSEQGE_sAAF6JQwH3-H4CWJZDAYikfgI-AAEB9TrYrw==","name":"","location":[21.203294,41.875703]},{"hint":"mHpYi78puIsAAAAAAAAAAAYAAABxHwAAawcAAI2jzAeOpcwHE_sAAHfAQwEEAX0CWJZDAegdfQInAgEB9TrYrw==","name":"","location":[21.217399,41.746692]},{"hint":"hYN1itSEdYr0FpQABgAAAAAAAAA7AAAACAQAAC10cQc8dHEHE_sAAP6SQwFgonsCWJZDAUiXewIFAAEB9TrYrw==","name":"R2132","location":[21.205758,41.656928]},{"hint":"QYU4i4aO440AAAAAAQAAAA4AAABCAAAAKgAAADnCwAdCwsAHE_sAAKkcRQGuRoYC-BxFAahFhgIEAAEB9TrYrw==","name":"","location":[21.306537,42.35435]},{"hint":"gh8Dj0EkA49J9asAGgAAAAAAAAClAAAAAAAAAN_mEwBchOACE_sAAHQlRQE-tYQC-BxFAQi_hAIJAAEB9TrYrw==","name":"Kulla","location":[21.308788,42.251582]},{"hint":"iV43i51eN4snqgwBDAAAAAQAAABbAgAAAgEAAFw6wAdNOsAHE_sAAHEcRQGeOIMC-BxFAWg4gwIoAAEB9TrYrw==","name":"Isen Suma","location":[21.306481,42.154142]},{"hint":"k4vJhDEcLosAAAAALQAAAEYAAAAcBAAA7gAAAIhyXwSBxcgGE_sAAA4eRQG_s4EC-BxFAcixgQIqAAEB9TrYrw==","name":"","location":[21.306894,42.054591]},{"hint":"y8UIic7FCIkAAAAADAAAAAAAAADQAQAABQAAADOKxQYvisUGE_sAABr9RAFAJIAC-BxFASgrgAIYAAEB9TrYrw==","name":"","location":[21.298458,41.95232]},{"hint":"fyrkiYUq5IkAAAAAFAAAAAAAAADJEQAA6woAALn2SgOy9koDE_sAAHtbRQHclX4C-BxFAYikfgIsAQEB9TrYrw==","name":"","location":[21.322619,41.850332]},{"hint":"PwKciRnJnYkAAAAALwAAAAAAAAA8LgAAAAAAAKegRgAMcxIHE_sAAH9rRAGaJ30C-BxFAegdfQJnAgEB9TrYrw==","name":"","location":[21.261183,41.75657]},{"hint":"ANDaiGlECI4AAAAAAAAAADYAAAAaEwAAnwwAAJjTrgb-Yo4IE_sAAGfNRAF2cHsC-BxFAUiXewJDAAEB9TrYrw==","name":"","location":[21.286247,41.64415]},{"hint":"4ok4i-OJOIsAAAAAAAAAAAoAAAD8BQAA_AAAAL7EwAdQxMAHE_sAAHO1RgHWUoYCmKNGAahFhgIxAAEB9TrYrw==","name":"","location":[21.411187,42.357462]},{"hint":"9Csai5y5RosAAAAACwAAAAAAAAATAwAAZwUAAKfTfADibssCE_sAAMujRgGHv4QCmKNGAQi_hAI6AAEB9TrYrw==","name":"","location":[21.406667,42.254215]},{"hint":"NHRBiT8AwIkAAAAAFgAAAAAAAAAzAQAAAAAAAF-aIQdpmiEHE_sAAKTDRgH744ICmKNGAWg4gwIQAAEB9TrYrw==","name":"","location":[21.41482,42.132475]},{"hint":"pmk-hP___3_Vq5AAxQAAAH4BAADuAAAAtAEAAMVHkAPAR5ADE_sAAKOjRgEhroECmKNGAcixgQIHAAEB9TrYrw==","name":"\xd0\x9c\xd0\xb0\xd1\x98\xd0\xba\xd0\xb0 \xd0\xa2\xd0\xb5\xd1\x80\xd0\xb5\xd0\xb7\xd0\xb0 (A2;A4)","location":[21.406627,42.053153]},{"hint":"uGU8hLxlPIQlqJUAAAAAAAUAAAAQAgAAZgEAAP1AkAPYQJADE_sAAFOqRgEqGYACmKNGASgrgAIrAAEB9TrYrw==","name":"R2134","location":[21.408339,41.949482]},{"hint":"QTXyjEw18owAAAAAAAAAAAwAAABsAAAAbgAAAKpxTAjWcEwIE_sAAJOpRgEKs34CmKNGAYikfgIGAAEB9TrYrw==","name":"","location":[21.408147,41.857802]},{"hint":"HTTaiCA02ogAAAAAAAAAADEAAAAAAAAAGA8AAJGHrgZkiK4GE_sAAHjtRgFxsX0CmKNGAegdfQIAAAEB9TrYrw==","name":"","location":[21.425528,41.791857]},{"hint":"h1jaiIhY2ogAAAAADgAAAAAAAAABEAAAWwAAAD-crgZCnK4GE_sAAASRRgFwTnsCmKNGAUiXewJyAAEB9TrYrw==","name":"","location":[21.40186,41.63544]}],"sources":[{"hint":"ZVKXhLuOHY8AAAAAAQAAAAoAAAAAAAAANwUAAE4HKQNNBykDE_sAAGdMQQF37oACaExBAXjugAIAAAEB9TrYrw==","name":"","location":[21.056615,42.004087]}]}''' ) center_pt = osrm.Point(latitude=21.0566163, longitude=42.0040885) n_class = 8 Accessibility = osrm.AccessIsochrone(center_pt, points_grid=100) snapped_center_point = Accessibility.center_point gdf = Accessibility.render_contour(n_class=n_class) self.assertIsInstance(snapped_center_point, osrm.Point) self.assertIsInstance(gdf, GeoDataFrame) self.assertEqual(n_class, len(gdf)) n_class = 5 gdf = Accessibility.render_contour(n_class=n_class) self.assertEqual(n_class, len(gdf)) n_class = 10 gdf = Accessibility.render_contour(n_class=n_class) self.assertEqual(n_class, len(gdf))
def __init__(self, **kwargs): super(Routing_machine, self).__init__(**kwargs) # Configuration self.host = kwargs.get('host', 'router.project-osrm.org') self.profile = kwargs.get('profile', 'driving') # Parameters self.latitude1 = kwargs.get('latitude1', None) self.longitude1 = kwargs.get('longitude1', None) self.latitude2 = kwargs.get('latitude2', None) self.longitude2 = kwargs.get('longitude2', None) self.alternatives = kwargs.get('alternatives', False) self.distance = kwargs.get('distance', False) self.duration = kwargs.get('duration', False) self.summary = kwargs.get('summary', False) self.route = kwargs.get('route', False) self.raw = kwargs.get('raw', False) if self._is_parameters_ok(): osrm.RequestConfig.host = self.host osrm.RequestConfig.profile = self.profile start = osrm.Point(latitude=float(self.latitude1), longitude=float(self.longitude1)) end = osrm.Point(latitude=float(self.latitude2), longitude=float(self.longitude2)) try: self.api_result = osrm.simple_route( start, end, steps=True, alternatives=self.alternatives) logging.debug("[OSRM] get API result") message = { "returncode": "OK", "start": str(start), "end": str(end), "profile": self.profile } if self.raw: message['raw'] = self.api_result logging.debug("[OSRM] add raw JSON to message") # create items (result, route) iterator and append mappers items_iterator = map( lambda route: { 'result': {}, 'route': route }, self.api_result['routes']) if self.distance: logging.debug("[OSRM] add distance to execution scheme") items_iterator = map(self.distance_mapper, items_iterator) if self.duration: logging.debug("[OSRM] add duration to execution scheme") items_iterator = map(self.duration_mapper, items_iterator) if self.summary: logging.debug("[OSRM] add summary to execution scheme") items_iterator = map(self.route_summary_mapper, items_iterator) if self.route: logging.debug("[OSRM] add route to execution scheme") items_iterator = map(self.route_mapper, items_iterator) # extract results from items items_iterator = map(lambda item: item['result'], items_iterator) message['routes'] = list(items_iterator) except HTTPError as e: message = {"returncode": str(e.code)} logging.debug("[OSRM] neuron return dict %s" % message) self.say(message)