This repository was archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
executable file
·69 lines (52 loc) · 1.92 KB
/
filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
"""
Entry point for running vaccine feed runners
"""
import pathlib
import json
import click
@click.group()
def cli():
"""Run commands"""
@cli.command()
@click.option("--feed-locations", "feed_locations", type=str, default="feed-locations.ndjson")
@click.option("--api-locations", "api_locations", type=str, default="api-locations.geojson")
@click.option("--output-locations", "output_locations", type=str, default="output-locations.geojson")
def filter_api(
feed_locations: str,
api_locations: str,
output_locations: str,
) -> None:
"""Filter API locations to locations that exist in feed locations"""
feed_locations_path = pathlib.Path(feed_locations)
api_locations_path = pathlib.Path(api_locations)
output_locations_path = pathlib.Path(output_locations)
source_ids = set()
with feed_locations_path.open() as feed_locations_file:
for line in feed_locations_file:
line = line.strip()
if not line:
continue
try:
feed_location = json.loads(line)
except json.JSONDecodeError as err:
print(err, line)
raise
source_ids.add(feed_location["id"])
with api_locations_path.open() as api_locations_file:
api_locations = json.load(api_locations_file)
original_num_api_locations = len(api_locations["features"])
api_locations["features"] = [
feature for feature in api_locations["features"]
if set(feature["properties"].get("concordances", [])) & source_ids
]
api_locations_json = json.dumps(api_locations)
output_locations_path.write_text(api_locations_json)
print(f"Filtered {original_num_api_locations} to "
f"{len(api_locations['features'])}")
@cli.command()
def version() -> None:
"""Get the library version."""
click.echo(click.style("0.1.0", bold=True))
if __name__ == "__main__":
cli()