def _get_names_for_unnamed_stops(self): """Intelligently guess stop names for unnamed stops by sourrounding street names and amenities. Caches stops with newly guessed names. """ # Loop through all stops for stop in self.stops['regular'].values(): # If there is no name, query one intelligently from OSM if stop.name == "[" + self.stop_no_name + "]": self._find_best_name_for_unnamed_stop(stop) logging.info("* Found alternative stop name: " + stop.name + " - " + stop.osm_url) # Cache stops with newly created stop names Cache.write_data(self.selector + '-stops', self.stops)
def get_routes(self, refresh=False): """The get_routes function returns the data of routes from OpenStreetMap converted into usable objects. Data about routes is getting obtained from OpenStreetMap through the Overpass API, based on the configuration from the config file. Then this data gets prepared by building up objects of Line and Itinerary objects that are related to each other. It uses caching to leverage fast performance and spare the Overpass API. Special commands are used to refresh cached data. :param self: the own object including it's functions and variables :param refresh: A simple boolean indicating a data refresh or use of caching if possible. :return routes: A dictionary of Line objects with related Itinerary objects constituting the tree of data. """ # Preferably return cached data about routes if refresh is False: # Check if routes data is already built in this object if not self.routes: # If not, try to get routes data from file cache self.routes = Cache.read_data(self.selector + '-routes') # Return cached data if found if bool(self.routes): return self.routes # No cached data was found or refresh was forced logging.info("Query and build fresh data for routes") # Obtain raw data about routes from OpenStreetMap result = self._query_routes() # Pre-sort relations by type route_masters = {} route_variants = {} for relation in result.relations: if relation.tags["type"] == "route_master": route_masters[relation.id] = relation else: route_variants[relation.id] = relation # Build routes from master relations for rmid, route_master in route_masters.iteritems(): itineraries = OrderedDict() # Build route variant members for member in route_master.members: # Create Itinerary objects from member route variants if member.ref in route_variants: rv = route_variants.pop(member.ref) itinerary = self._build_itinerary(rv, result, route_master) if itinerary is not None: itineraries[rv.id] = itinerary # Route variant was already used or is not valid else: rv = result.get_relations(member.ref) if bool(rv): rv = rv.pop() logging.warning("Itinerary was assigned again:") logging.warning("https://osm.org/relation/%s", rv.id) itinerary = self._build_itinerary( rv, result, route_master) if itinerary is not None: itineraries[rv.id] = itinerary else: logging.warning("Warning: This relation route master:") logging.warning(" https://osm.org/relation/%s", route_master.id) logging.warning( " has a member which is not a valid itinerary:") logging.warning(" https://osm.org/relation/%s", member.ref) # Create Line object from route master line = self._build_line(route_master, itineraries) if line is None: continue # Make sure route_id (ref) number is not already taken if line.route_id and line.route_id in [ elem.route_id for elem in self.routes.values() ]: logging.warning("'Ref' of route_master already taken") logging.warning(" https://osm.org/relation/%s", route_master.id) logging.warning(" Skipped. Please fix in OpenStreetMap") continue self.routes[str(line.osm_id)] = line # Build routes from variants (missing master relation) for rvid, route_variant in route_variants.iteritems(): logging.warning("Route (variant) without route_master") logging.warning(" https://osm.org/relation/%s", route_variant.id) logging.warning(" Please fix in OpenStreetMap") itinerary = self._build_itinerary(route_variant, result, False) # Make sure route_id (ref) number is not already taken if itinerary is not None: if itinerary.route_id in self.routes: logging.warning("Route with existing route_id (ref)") logging.warning(" https://osm.org/relation/%s", route_variant.id) logging.warning(" Skipped. Please fix in OpenStreetMap") else: # Create Line from route variant itineraries = OrderedDict() itineraries[itinerary.osm_id] = itinerary line = self._build_line(route_variant, itineraries) if line is not None: self.routes[line.route_id] = line # Cache data Cache.write_data(self.selector + '-routes', self.routes) return self.routes
def get_stops(self, refresh=False): """The get_stops function returns the data of stops and stop areas from OpenStreetMap converted into usable objects. Data about stops and stop_areas is getting obtained from OpenStreetMap through the Overpass API, based on the configuration from the config file. Then this data gets prepared by building up objects of the class Stops and Station (when the Stops are members of a stop_area) It uses caching to leverage fast performance and spare the Overpass API. Special commands are used to refresh cached data. :param self: the own object including it's functions and variables :param refresh: A simple boolean indicating a data refresh or use of caching if possible. :return stops: A dictionary of Stops and Stations constituting the obtained data. """ # Preferably return cached data about stops if refresh is False: # Check if stops data is already built in this object if not self.stops: # If not, try to get stops data from file cache self.stops = Cache.read_data(self.selector + '-stops') if bool(self.stops): # Maybe check for unnamed stop names if self.auto_stop_names: self._get_names_for_unnamed_stops() # Return cached data if found return self.stops # No cached data was found or refresh was forced logging.info("Query and build fresh data for stops") # Obtain raw data about routes from OpenStreetMap result = self._query_stops() self.stops['regular'] = {} self.stops['stations'] = {} # Build stops from ways (polygons) for stop in result.ways: osm_type = "way" stop_object = self._build_stop(stop, osm_type) if stop_object: self.stops['regular'][osm_type + "/" + str(stop_object.osm_id)] = stop_object # Build stops from nodes for stop in result.nodes: osm_type = "node" stop_object = self._build_stop(stop, osm_type) if stop_object: self.stops['regular'][osm_type + "/" + str(stop_object.osm_id)] = stop_object # Build stations from stop_area relations for stop in result.relations: osm_type = "relation" stop_object = self._build_station(stop, osm_type) if stop_object: self.stops['stations'][osm_type + "/" + str(stop.id)] = stop_object # Cache data Cache.write_data(self.selector + '-stops', self.stops) # Maybe check for unnamed stop names if self.auto_stop_names: self._get_names_for_unnamed_stops() return self.stops