def additional_streets_view_txt(ctx: context.Context, relations: areas.Relations, request_uri: str, chkl: bool) -> Tuple[str, str]: """Expected request_uri: e.g. /osm/additional-streets/ujbuda/view-result.txt.""" tokens = request_uri.split("/") relation_name = tokens[-2] relation = relations.get_relation(relation_name) output = "" if not ctx.get_file_system().path_exists( relation.get_files().get_osm_streets_path()): output += tr("No existing streets") elif not ctx.get_file_system().path_exists( relation.get_files().get_ref_streets_path()): output += tr("No reference streets") else: streets = relation.get_additional_streets() lexical_sort_key = util.get_lexical_sort_key() streets.sort( key=lambda street: lexical_sort_key(street.get_osm_name())) for street in streets: if chkl: output += "[ ] {}\n".format(street.get_osm_name()) else: output += "{}\n".format(street.get_osm_name()) return output, relation_name
def get_missing_housenumbers_txt(ctx: context.Context, relation: areas.Relation) -> str: """Gets the cached plain text of the missing housenumbers for a relation.""" output = "" if is_missing_housenumbers_txt_cached(ctx, relation): with relation.get_files().get_housenumbers_txtcache_stream( "rb") as stream: output = util.from_bytes(stream.read()) return output ongoing_streets, _ignore = relation.get_missing_housenumbers() table = [] for result in ongoing_streets: range_list = util.get_housenumber_ranges(result[1]) range_strings = [i.get_number() for i in range_list] # Street name, only_in_reference items. if not relation.get_config().get_street_is_even_odd( result[0].get_osm_name()): result_sorted = sorted(range_strings, key=util.split_house_number) row = result[0].get_osm_name() + "\t[" + ", ".join( result_sorted) + "]" else: elements = util.format_even_odd(range_list, doc=None) row = result[0].get_osm_name() + "\t[" + "], [".join( elements) + "]" table.append(row) table.sort(key=util.get_lexical_sort_key()) output += "\n".join(table) with relation.get_files().get_housenumbers_txtcache_stream("wb") as stream: stream.write(util.to_bytes(output)) return output
def write_city_count_path(ctx: context.Context, city_count_path: str, cities: Dict[str, Set[str]]) -> None: """Writes a daily .citycount file.""" with ctx.get_file_system().open(city_count_path, "wb") as stream: # Locale-aware sort, by key. lexical_sort_key = util.get_lexical_sort_key() for key, value in sorted(cities.items(), key=lambda item: lexical_sort_key(item[0])): stream.write(util.to_bytes(key + "\t" + str(len(value)) + "\n"))
def missing_housenumbers_view_chkl(ctx: context.Context, relations: areas.Relations, request_uri: str) -> Tuple[str, str]: """Expected request_uri: e.g. /osm/missing-housenumbers/ormezo/view-result.chkl.""" tokens = request_uri.split("/") relation_name = tokens[-2] relation = relations.get_relation(relation_name) relation.get_config().set_letter_suffix_style(util.LetterSuffixStyle.LOWER) output = "" if not ctx.get_file_system().path_exists( relation.get_files().get_osm_streets_path()): output += tr("No existing streets") elif not ctx.get_file_system().path_exists( relation.get_files().get_osm_housenumbers_path()): output += tr("No existing house numbers") elif not ctx.get_file_system().path_exists( relation.get_files().get_ref_housenumbers_path()): output += tr("No reference house numbers") else: ongoing_streets, _ignore = relation.get_missing_housenumbers() table = [] for result in ongoing_streets: range_list = util.get_housenumber_ranges(result[1]) # Street name, only_in_reference items. row = "[ ] " if not relation.get_config().get_street_is_even_odd( result[0].get_osm_name()): result_sorted = sorted([i.get_number() for i in range_list], key=util.split_house_number) row += result[0].get_osm_name() + " [" + ", ".join( result_sorted) + "]" table.append(row) else: elements = util.format_even_odd(range_list, doc=None) if len(elements) > 1 and len( range_list) > get_chkl_split_limit(): for element in elements: row = "[ ] " + result[0].get_osm_name( ) + " [" + element + "]" table.append(row) else: row += result[0].get_osm_name() + " [" + "], [".join( elements) + "]" table.append(row) table.sort(key=util.get_lexical_sort_key()) output += "\n".join(table) return output, relation_name
def additional_streets_view_result(ctx: context.Context, relations: areas.Relations, request_uri: str) -> yattag.doc.Doc: """Expected request_uri: e.g. /osm/additional-streets/budapest_11/view-result.""" tokens = request_uri.split("/") relation_name = tokens[-2] relation = relations.get_relation(relation_name) doc = yattag.doc.Doc() prefix = ctx.get_ini().get_uri_prefix() if not ctx.get_file_system().path_exists( relation.get_files().get_osm_streets_path()): doc.asis( webframe.handle_no_osm_streets(prefix, relation_name).getvalue()) elif not ctx.get_file_system().path_exists( relation.get_files().get_ref_streets_path()): doc.asis( webframe.handle_no_ref_streets(prefix, relation_name).getvalue()) else: # Get "only in OSM" streets. streets = relation.write_additional_streets() count = len(streets) lexical_sort_key = util.get_lexical_sort_key() streets.sort( key=lambda street: lexical_sort_key(street.get_osm_name())) table = [[ util.html_escape(tr("Identifier")), util.html_escape(tr("Type")), util.html_escape(tr("Source")), util.html_escape(tr("Street name")) ]] for street in streets: cell = yattag.doc.Doc() href = "https://www.openstreetmap.org/{}/{}".format( street.get_osm_type(), street.get_osm_id()) with cell.tag("a", href=href, target="_blank"): cell.text(str(street.get_osm_id())) cells = [ cell, util.html_escape(street.get_osm_type()), util.html_escape(street.get_source()), util.html_escape(street.get_osm_name()), ] table.append(cells) with doc.tag("p"): doc.text( tr("OpenStreetMap additionally has the below {0} streets."). format(str(count))) doc.stag("br") with doc.tag("a", href=prefix + "/additional-streets/" + relation_name + "/view-result.txt"): doc.text(tr("Plain text format")) doc.stag("br") with doc.tag("a", href=prefix + "/additional-streets/" + relation_name + "/view-result.chkl"): doc.text(tr("Checklist format")) doc.stag("br") with doc.tag( "a", href=prefix + "/additional-streets/{}/view-turbo".format(relation_name)): doc.text(tr("Overpass turbo query for the below streets")) doc.asis(util.html_table_from_list(table).getvalue()) doc.asis( util.invalid_refstreets_to_html( relation.get_invalid_refstreets()).getvalue()) return doc
def missing_streets_view_result(ctx: context.Context, relations: areas.Relations, request_uri: str) -> yattag.doc.Doc: """Expected request_uri: e.g. /osm/missing-streets/budapest_11/view-result.""" tokens = request_uri.split("/") relation_name = tokens[-2] relation = relations.get_relation(relation_name) doc = yattag.doc.Doc() prefix = ctx.get_ini().get_uri_prefix() if not ctx.get_file_system().path_exists( relation.get_files().get_osm_streets_path()): doc.asis( webframe.handle_no_osm_streets(prefix, relation_name).getvalue()) return doc if not ctx.get_file_system().path_exists( relation.get_files().get_ref_streets_path()): doc.asis( webframe.handle_no_ref_streets(prefix, relation_name).getvalue()) return doc ret = relation.write_missing_streets() todo_count, done_count, percent, streets = ret streets.sort(key=util.get_lexical_sort_key()) table = [[util.html_escape(tr("Street name"))]] for street in streets: table.append([util.html_escape(street)]) with doc.tag("p"): doc.text( tr("OpenStreetMap is possibly missing the below {0} streets."). format(str(todo_count))) doc.text( tr(" (existing: {0}, ready: {1}).").format( str(done_count), util.format_percent(str(percent)))) doc.stag("br") with doc.tag("a", href=prefix + "/missing-streets/{}/view-turbo".format(relation_name)): doc.text( tr("Overpass turbo query for streets with questionable names")) doc.stag("br") with doc.tag("a", href=prefix + "/missing-streets/" + relation_name + "/view-result.txt"): doc.text(tr("Plain text format")) doc.stag("br") with doc.tag("a", href=prefix + "/missing-streets/" + relation_name + "/view-result.chkl"): doc.text(tr("Checklist format")) doc.asis(util.html_table_from_list(table).getvalue()) doc.asis( util.invalid_refstreets_to_html( relation.get_invalid_refstreets()).getvalue()) doc.asis( util.invalid_filter_keys_to_html( relation.get_invalid_filter_keys()).getvalue()) return doc
def handle_stats_cityprogress(ctx: context.Context, relations: areas.Relations) -> yattag.doc.Doc: """Expected request_uri: e.g. /osm/housenumber-stats/hungary/cityprogress.""" doc = yattag.doc.Doc() doc.asis(get_toolbar(ctx, relations).getvalue()) ref_citycounts: Dict[str, int] = {} with open(ctx.get_ini().get_reference_citycounts_path(), "r") as stream: first = True for line in stream.readlines(): if first: first = False continue cells = line.strip().split('\t') if len(cells) < 2: continue city = cells[0] count = int(cells[1]) ref_citycounts[city] = count today = datetime.date.fromtimestamp( ctx.get_time().now()).strftime("%Y-%m-%d") osm_citycounts: Dict[str, int] = {} with open(ctx.get_ini().get_workdir() + "/stats/" + today + ".citycount", "r") as stream: for line in stream.readlines(): cells = line.strip().split('\t') if len(cells) < 2: continue city = cells[0] count = int(cells[1]) osm_citycounts[city] = count cities = util.get_in_both(list(ref_citycounts.keys()), list(osm_citycounts.keys())) cities.sort(key=util.get_lexical_sort_key()) table = [] table.append([ util.html_escape(tr("City name")), util.html_escape(tr("House number coverage")), util.html_escape(tr("OSM count")), util.html_escape(tr("Reference count")) ]) for city in cities: percent = "100.00" if ref_citycounts[city] > 0 and osm_citycounts[city] < ref_citycounts[ city]: percent = "%.2f" % (osm_citycounts[city] / ref_citycounts[city] * 100) table.append([ util.html_escape(city), util.html_escape(util.format_percent(percent)), util.html_escape(str(osm_citycounts[city])), util.html_escape(str(ref_citycounts[city])) ]) doc.asis(util.html_table_from_list(table).getvalue()) with doc.tag("h2"): doc.text(tr("Note")) with doc.tag("div"): doc.text( tr("""These statistics are estimates, not taking house number filters into account. Only cities with house numbers in OSM are considered.""")) doc.asis(get_footer().getvalue()) return doc