Repository navigation
Tag usage script #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nakaner
wants to merge
9
commits into
main
Choose a base branch
from
tag_usage_script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Tag usage script #188
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e5f5fac
Add tag usage stats script in almost unmodified version
e65337c
Allow users to change regions, key and number
Nakaner 7dcfc92
Add quoting and escaping
Nakaner 9e86b8f
Log an error message and fail if HTTP status code != 200
Nakaner 3d2462c
Set user-agent
Nakaner 1f106cd
Allow to query frequently used keys matching a certain string
Nakaner f60f1ac
Print summed counts
Nakaner 4ead496
Explain script
Nakaner ecb5884
Improve documentation, remove unnecessary import, add license
Nakaner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #! /usr/bin/env python3 | ||
| # SPDX-License-Identifier: CC0-1.0 | ||
| # Author: Frederik Ramm | ||
| # Extended by Michael Reichert | ||
| # originally published in https://github.com/shortbread-tiles/shortbread-docs/issues/153#issuecomment-5476984543 | ||
| # | ||
| # Program to determine a list of values for a given key | ||
| # or keys matching a given search string | ||
| # where each value/key is in the list of N most frequently used values/keys | ||
| # across a list of regions. | ||
| # | ||
| # Example: | ||
| # - `python tag_usage_stats.py -t value -k shop -n 75` | ||
| # will return all values of shop=* which are in the list of 75 most frequently used values of shop=* in all of the following regions: Africa, Asia, Europe, North America, South America. | ||
| # For example, if shop=outdoor is frequently used in Europe an North America but rarely used in Africa, it will be not be returned. | ||
| # However shop=convenience will be returned because it is among the top 75 shop values in all regions. | ||
| # - `python tag_usage_stats.py -t key -k name -n 75` | ||
| # will return all keys which appear among the 75 most frequently used keys containing | ||
| # the substring "name" in all of the following regions: Africa, Asia, Europe, North America, South America. | ||
| # - `python tag_usage_stats.py -t value -k office -n 100 -r europe/germany,europe/france` | ||
| # will return the values of office=* which are in the list of 100 most freuqently used values in Germany and France. | ||
|
|
||
| import argparse | ||
| import enum | ||
| import math | ||
| import requests | ||
| import sys | ||
| import urllib.parse | ||
|
|
||
| N = 100 | ||
| key = "shop" | ||
| default_regions = ",".join(["europe", "north-america", "asia", "south-america", "africa"]) | ||
| headers = {"User-Agent": "shortbread/tag_usage_stats"} | ||
|
|
||
|
Nakaner marked this conversation as resolved.
|
||
| class QueryType(enum.Enum): | ||
| values = "value" | ||
| keys = "key" | ||
|
|
||
| def __str__(self): | ||
| return self.value | ||
|
|
||
|
|
||
| parser = argparse.ArgumentParser(description="""Program to determine a list of values for a given key | ||
| where each value is in the list of N most frequently used values | ||
| across a list of regions. | ||
| """) | ||
| parser.add_argument("-k", "--key", type=str, default=key, help="Set OSM key (defaults to 'shop')") | ||
| parser.add_argument("-n", "--number", type=int, default=N, help="Change N (defaults to 100)") | ||
| parser.add_argument("-t", "--type", type=QueryType, choices=list(QueryType), default=QueryType.values, help="Whether to query the N most frequently used values of a given key or the N most frequently used keys matching a given search string.") | ||
| parser.add_argument("-r", "--regions", type=str, default=default_regions, help="Regions to compare. Specify as one string separated by comma. This must match the paths used at taginfo.geofabrik.de") | ||
| args = parser.parse_args() | ||
| regions = args.regions.split(",") | ||
|
Nakaner marked this conversation as resolved.
|
||
| if len(regions) == 0: | ||
| sys.stderr.write("ERROR: No regions provided.\n") | ||
|
|
||
| globalitem = {} | ||
|
|
||
| for i, region in enumerate(regions): | ||
| url = "https://taginfo.geofabrik.de/{}/api/4/key/values".format(urllib.parse.quote(region)) | ||
| params = {"sortname": "count_all", "sortorder": "desc", "rp": str(args.number), "page": "1"} | ||
| if args.type == QueryType.keys: | ||
| url = "https://taginfo.geofabrik.de/{}/api/4/keys/all".format(urllib.parse.quote(region)) | ||
| params["query"] = args.key | ||
| elif args.type == QueryType.values: | ||
| params["key"] = args.key | ||
| r = requests.get(url, headers=headers, params=params) | ||
| if r.status_code != 200: | ||
| sys.stderr.write("ERROR: {url} HTTP {status_code}\n".format(**r.__dict__)) | ||
| sys.exit(1) | ||
| for item in r.json()["data"]: | ||
| entry_key = item[args.type.value] | ||
| entry = globalitem.get(entry_key, [0 for j in range(len(regions))]) | ||
| attr = "count" | ||
| if args.type == QueryType.keys: | ||
| attr = "count_all" | ||
| entry[i] = item[attr] | ||
| globalitem[entry_key] = entry | ||
|
|
||
| top_entries = [] | ||
| for item, counts in globalitem.items(): | ||
| if 0 not in counts: | ||
| top_entries.append((item, sum(counts))) | ||
| top_entries.sort(key=lambda k: k[1], reverse=True) | ||
| max_keylength = max([len(k[0]) for k in top_entries]) | ||
| max_valuelength = int(max([math.log(k[1], 10) for k in top_entries])) + 1 | ||
| for t in top_entries: | ||
| print("{} {}".format(t[0].ljust(max_keylength, " "), str(t[1]).rjust(max_valuelength, " "))) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have a proper License mentioned or a public domain release.