def get_route_segment_sub_points(self, routing_table_id, reverse): c_list = DBControl().fetch_data("" \ "SELECT ST_Y(geom) AS lat, ST_X(geom) AS lon " \ "FROM (" \ "SELECT ST_PointN(geom_way, generate_series(1, ST_NPoints(geom_way))) AS geom " \ "FROM %s WHERE id = %d) AS points;" \ % (self.temp_routing_table_name, routing_table_id)) if reverse: c_list = c_list[::-1] point_list = [] last_accepted_bearing = geometry.bearing_between_two_points( c_list[0]['lat'], c_list[0]['lon'], c_list[1]['lat'], c_list[1]['lon']) for i in range(1, c_list.__len__()-1): new_bearing = geometry.bearing_between_two_points( c_list[i]['lat'], c_list[i]['lon'], c_list[i+1]['lat'], c_list[i+1]['lon']) turn = geometry.turn_between_two_segments( new_bearing, last_accepted_bearing) if turn > 22 and turn < 338: last_accepted_bearing = new_bearing point_list.append(self.poi.create_way_point(-1, c_list[i]['lat'], c_list[i]['lon'], {})) return point_list
def follow_this_way(self): # set gzip header cherrypy.response.headers['Content-Type'] = 'application/gzip' # create the return tuple return_tuple = {} return_tuple['route'] = [] return_tuple['warning'] = "" return_tuple['error'] = "" translator = Translator(Config().get_param("default_language")) # parse json encoded input input = helper.convert_dict_values_to_utf8( cherrypy.request.json ) # options if input.has_key("options") == False: return_tuple['error'] = translator.translate("message", "no_route_options") return helper.zip_data(return_tuple) elif type(input['options']) != type({}): return_tuple['error'] = translator.translate("message", "no_route_options") return helper.zip_data(return_tuple) options = input['options'] # user language language = "" if options.has_key("language") == True: language = options['language'] # if the user sends a language, which is not german, take the default language setting if language != "de": language = Config().get_param("default_language") # initialize the translator object with the user's choosen language translator = Translator(language) # start point if input.has_key("start_point") == False: return_tuple['error'] = translator.translate("message", "no_start_point") return helper.zip_data(return_tuple) start_point = input['start_point'] if start_point.has_key("name") == False: return_tuple['error'] = translator.translate("message", "start_point_no_name") return helper.zip_data(return_tuple) elif start_point.has_key("lat") == False: return_tuple['error'] = translator.translate("message", "start_point_no_latitude") return helper.zip_data(return_tuple) elif start_point.has_key("lon") == False: return_tuple['error'] = translator.translate("message", "start_point_no_longitude") return helper.zip_data(return_tuple) elif start_point.has_key("type") == False: return_tuple['error'] = translator.translate("message", "start_point_no_type") return helper.zip_data(return_tuple) # further options if options.has_key("way_id") == False: return_tuple['error'] = translator.translate("message", "no_way_id") return helper.zip_data(return_tuple) if options.has_key("bearing") == False: return_tuple['error'] = translator.translate("message", "no_bearing_value") return helper.zip_data(return_tuple) add_all_intersections = False if options.has_key("add_all_intersections") == True: if options['add_all_intersections'] == "yes": add_all_intersections = True way = DBControl().fetch_data("SELECT nodes from ways where id = %d" % options['way_id']) if way.__len__() == 0: return_tuple['error'] = translator.translate("message", "way_id_invalid") return helper.zip_data(return_tuple) # create session id if options.has_key("session_id") == False: return_tuple['error'] = translator.translate("message", "no_session_id_option") return helper.zip_data(return_tuple) session_id = options['session_id'] # try to cancel prior request if Config().clean_old_session(session_id) == False: return_tuple['error'] = translator.translate("message", "old_request_still_running") return helper.zip_data(return_tuple) if Config().number_of_session_ids() == Config().get_param("thread_pool") - 1: return_tuple['error'] = translator.translate("message", "server_busy") return helper.zip_data(return_tuple) Config().add_session_id(session_id) # get a route route_logger = RouteLogger("routes", "%s---way_id.%s" % (start_point['name'], options['way_id'])) rfc = RouteFootwayCreator(session_id, route_logger, translator, 1.0, ["big_streets", "small_streets", "paved_ways", "unpaved_ways", "unclassified_ways", "steps"], []) try: route = rfc.follow_this_way(start_point, options['way_id'], options['bearing'], add_all_intersections) except RouteFootwayCreator.FootwayRouteCreationError as e: route_logger.append_to_log("\n----- result -----\ncanceled") Config().confirm_removement_of_session_id(session_id) return_tuple['route'] = [] return_tuple['error'] = "%s" % e return helper.zip_data(return_tuple) # return calculated route return_tuple['route'] = route return_tuple['description'] = rfc.get_route_description( return_tuple['route'] ) route_logger.append_to_log("\n----- result -----\n") route_logger.append_to_log( json.dumps( return_tuple['route'], indent=4, encoding="utf-8") + "\n----- end of route -----\n") # convert return_tuple to json and zip it, before returning Config().confirm_removement_of_session_id(session_id) return helper.zip_data(return_tuple)
def follow_this_way(self): # set gzip header cherrypy.response.headers['Content-Type'] = 'application/gzip' # create the return tuple return_tuple = {} return_tuple['route'] = [] return_tuple['warning'] = "" return_tuple['error'] = "" translator = Translator(Config().get_param("default_language")) # parse json encoded input input = helper.convert_dict_values_to_utf8(cherrypy.request.json) # options if input.has_key("options") == False: return_tuple['error'] = translator.translate( "message", "no_route_options") return helper.zip_data(return_tuple) elif type(input['options']) != type({}): return_tuple['error'] = translator.translate( "message", "no_route_options") return helper.zip_data(return_tuple) options = input['options'] # user language language = "" if options.has_key("language") == True: language = options['language'] # if the user sends a language, which is not german, take the default language setting if language != "de": language = Config().get_param("default_language") # initialize the translator object with the user's choosen language translator = Translator(language) # start point if input.has_key("start_point") == False: return_tuple['error'] = translator.translate( "message", "no_start_point") return helper.zip_data(return_tuple) start_point = input['start_point'] if start_point.has_key("name") == False: return_tuple['error'] = translator.translate( "message", "start_point_no_name") return helper.zip_data(return_tuple) elif start_point.has_key("lat") == False: return_tuple['error'] = translator.translate( "message", "start_point_no_latitude") return helper.zip_data(return_tuple) elif start_point.has_key("lon") == False: return_tuple['error'] = translator.translate( "message", "start_point_no_longitude") return helper.zip_data(return_tuple) elif start_point.has_key("type") == False: return_tuple['error'] = translator.translate( "message", "start_point_no_type") return helper.zip_data(return_tuple) # further options if options.has_key("way_id") == False: return_tuple['error'] = translator.translate( "message", "no_way_id") return helper.zip_data(return_tuple) if options.has_key("bearing") == False: return_tuple['error'] = translator.translate( "message", "no_bearing_value") return helper.zip_data(return_tuple) add_all_intersections = False if options.has_key("add_all_intersections") == True: if options['add_all_intersections'] == "yes": add_all_intersections = True way = DBControl().fetch_data("SELECT nodes from ways where id = %d" % options['way_id']) if way.__len__() == 0: return_tuple['error'] = translator.translate( "message", "way_id_invalid") return helper.zip_data(return_tuple) # create session id if options.has_key("session_id") == False: return_tuple['error'] = translator.translate( "message", "no_session_id_option") return helper.zip_data(return_tuple) session_id = options['session_id'] # try to cancel prior request if Config().clean_old_session(session_id) == False: return_tuple['error'] = translator.translate( "message", "old_request_still_running") return helper.zip_data(return_tuple) if Config().number_of_session_ids( ) == Config().get_param("thread_pool") - 1: return_tuple['error'] = translator.translate( "message", "server_busy") return helper.zip_data(return_tuple) Config().add_session_id(session_id) # get a route route_logger = RouteLogger( "routes", "%s---way_id.%s" % (start_point['name'], options['way_id'])) rfc = RouteFootwayCreator(session_id, route_logger, translator, 1.0, [ "big_streets", "small_streets", "paved_ways", "unpaved_ways", "unclassified_ways", "steps" ], []) try: route = rfc.follow_this_way(start_point, options['way_id'], options['bearing'], add_all_intersections) except RouteFootwayCreator.FootwayRouteCreationError as e: route_logger.append_to_log("\n----- result -----\ncanceled") Config().confirm_removement_of_session_id(session_id) return_tuple['route'] = [] return_tuple['error'] = "%s" % e return helper.zip_data(return_tuple) # return calculated route return_tuple['route'] = route return_tuple['description'] = rfc.get_route_description( return_tuple['route']) route_logger.append_to_log("\n----- result -----\n") route_logger.append_to_log( json.dumps(return_tuple['route'], indent=4, encoding="utf-8") + "\n----- end of route -----\n") # convert return_tuple to json and zip it, before returning Config().confirm_removement_of_session_id(session_id) return helper.zip_data(return_tuple)
def follow_this_way(self, start_point, way_id, bearing, add_all_intersections): self.route = [] way = DBControl().fetch_data("SELECT nodes from ways where id = %d" % way_id)[0] # check for cancel command if Config().has_session_id_to_remove(self.session_id): raise RouteFootwayCreator.FootwayRouteCreationError( self.translator.translate("message", "process_canceled")) # find nearest way point min_dist = 1000000 id_index = 0 i = 0 for id in way['nodes']: wp = DBControl().fetch_data("SELECT ST_y(geom) as lat, ST_X(geom) as lon from nodes where id = %d" % id)[0] dist = geometry.distance_between_two_points(start_point['lat'], start_point['lon'], wp['lat'], wp['lon']) if dist < min_dist: min_dist = dist id_index = i i += 1 if id_index == 0: prev = DBControl().fetch_data("SELECT ST_y(geom) as lat, ST_X(geom) as lon from nodes where id = %d" % way['nodes'][id_index])[0] next = DBControl().fetch_data("SELECT ST_y(geom) as lat, ST_X(geom) as lon from nodes where id = %d" % way['nodes'][id_index+1])[0] else: prev = DBControl().fetch_data("SELECT ST_y(geom) as lat, ST_X(geom) as lon from nodes where id = %d" % way['nodes'][id_index-1])[0] next = DBControl().fetch_data("SELECT ST_y(geom) as lat, ST_X(geom) as lon from nodes where id = %d" % way['nodes'][id_index])[0] bearing_difference = geometry.bearing_between_two_points(prev['lat'], prev['lon'], next['lat'], next['lon']) - bearing if bearing_difference < 0: bearing_difference += 360 if bearing_difference < 90 or bearing_difference >= 270: for index in range( id_index, way['nodes'].__len__()): next_point = self.poi.create_intersection_by_id(way['nodes'][index]) if next_point == {}: next_point = self.poi.create_way_point_by_id(way['nodes'][index]) next_segment = self.poi.create_way_segment_by_id(way_id) self.add_point_to_route(next_point, next_segment, add_all_intersections) last_node_id = way['nodes'][-1] else: for index in range( id_index, -1, -1): next_point = self.poi.create_intersection_by_id(way['nodes'][index]) if next_point == {}: next_point = self.poi.create_way_point_by_id(way['nodes'][index]) next_segment = self.poi.create_way_segment_by_id(way_id, True) self.add_point_to_route(next_point, next_segment, add_all_intersections) last_node_id = way['nodes'][0] # check for cancel command if Config().has_session_id_to_remove(self.session_id): raise RouteFootwayCreator.FootwayRouteCreationError( self.translator.translate("message", "process_canceled")) last_way_properties = self.poi.create_way_segment_by_id(way_id) while True: found_next_part = False result = DBControl().fetch_data("SELECT w.id, w.nodes \ from ways w join way_nodes wn on w.id = wn.way_id \ where wn.node_id = %d and wn.way_id != %d" % (last_node_id, last_way_properties['way_id'])) if result.__len__() == 0: break for way in result: next_way_properties = self.poi.create_way_segment_by_id(way['id']) if last_way_properties['name'] != next_way_properties['name']: continue if last_node_id == way['nodes'][0]: for index in range(1, way['nodes'].__len__()): next_point = self.poi.create_intersection_by_id(way['nodes'][index]) if next_point == {}: next_point = self.poi.create_way_point_by_id(way['nodes'][index]) next_segment = self.poi.create_way_segment_by_id( last_way_properties['way_id']) self.add_point_to_route(next_point, next_segment, add_all_intersections) last_node_id = way['nodes'][-1] last_way_properties = next_way_properties found_next_part = True break if last_node_id == way['nodes'][-1]: for index in range( way['nodes'].__len__()-2, -1, -1): next_point = self.poi.create_intersection_by_id(way['nodes'][index]) if next_point == {}: next_point = self.poi.create_way_point_by_id(way['nodes'][index]) next_segment = self.poi.create_way_segment_by_id( last_way_properties['way_id'], True) self.add_point_to_route(next_point, next_segment, add_all_intersections) last_node_id = way['nodes'][0] last_way_properties = next_way_properties found_next_part = True break if found_next_part == False: break # check for cancel command if Config().has_session_id_to_remove(self.session_id): raise RouteFootwayCreator.FootwayRouteCreationError( self.translator.translate("message", "process_canceled")) return self.route