forked from stgibson/social_distance_web_scraping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-to-mr.py
128 lines (116 loc) · 6.2 KB
/
upload-to-mr.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import maproulette, os, json, urllib.request
from fieldnames import *
presets = {}
with urllib.request.urlopen("https://hackmd.io/@1ec5/BJ7h-KIPw/download") as doc:
for line in doc:
line = line.decode()
if line.startswith("---"):
break
for line in doc:
line = line.decode()
if '|' not in line:
continue
category, tags, preset = line.split('|')
presets[category.strip()] = preset.strip()
print("For category", category.strip(), "use preset", preset[:50].strip())
config = maproulette.Configuration(api_key=os.getenv("MR_API_KEY"))
proj_api = maproulette.Project(config)
proj_name = "Santa Clara County Social distancing protocol import"
try:
data = proj_api.get_project_by_name(proj_name)["data"]
proj = maproulette.ProjectModel(name=data["name"], id=data["id"])
existing = True
print("Using existing project ID", proj.id)
except maproulette.api.errors.NotFoundError:
proj = maproulette.ProjectModel(name=proj_name)
existing = False
print("Creating new project")
if not existing:
proj.description = """Importing business facilities based on [social distancing protocols](https://sdp.sccgov.org/) that business owners have filed with the [Santa Clara County Public Health Department](https://en.wikipedia.org/wiki/Santa_Clara_County_Public_Health_Department) under [COVID-19](https://en.wikipedia.org/wiki/COVID-19_pandemic_in_the_San_Francisco_Bay_Area) public health orders. See the [proposal on the OpenStreetMap wiki](https://wiki.openstreetmap.org/wiki/Santa_Clara_County,_California/Social_distancing_protocol_import) for more information."""
data = proj_api.create_project(proj)["data"]
proj = maproulette.ProjectModel(name=data["name"], id=data["id"])
print("Created project ID", proj.id)
challenges = proj_api.get_project_challenges(proj.id, limit=len(presets)+1)["data"]
challengeNameToData = {chal["name"]: chal for chal in challenges}
chal_api = maproulette.Challenge(config)
for cat_id in range(59):
chal_file = open("cat_id_%d.geojsonl"%cat_id)
cat_name = json.loads(chal_file.readline())["features"][0]["properties"][CATEGORY]
chal_file.seek(0)
if cat_name not in presets:
cat_name = "Other"
if cat_name == "Other, please specify":
print("Run add-other-category.py on cat_id_%d.geojsonl first"%cat_id)
continue
chal_name = "SDP import: "+cat_name
if chal_name in challengeNameToData:
data = challengeNameToData[chal_name]
chal = maproulette.ChallengeModel(name=data["name"], id=data["id"], parent=proj.id)
existing = True
print("Using existing challenge ID", chal.id)
else:
chal = maproulette.ChallengeModel(name=chal_name, parent=proj.id)
existing = False
print("Creating new challenge", repr(chal_name))
if not existing:
chal.description = proj.description
chal.instruction = """Follow the directions [here](https://docs.google.com/document/d/1vnR8xpxZlOPvMibsPX0TqFy99k_BaFs5aiszIDedgPo/preview). In short: Verify the business address and location (skip addresses that are the owner's house), then add a point for that business using the provided name and address."""
if cat_name in presets:
chal.instruction += " For this type of business we recommend the presets "+presets[cat_name]+"."
chal.enabled = True
chal.keywords = ["covid19", "#covid19", "covid-19", "covid"]
chal.check_in_comment = "Add business according to local social distancing protocol #c4sj #South-Bay-OSM #maproulette"
chal.check_in_source = "Santa Clara County Public Health Department"
chal.default_basemap_id = "Bing"
chal.default_basemap = 4
chal.high_priority_rule = \
maproulette.PriorityRuleModel(
condition=maproulette.priority_rule.Conditions.AND,
rules=[
maproulette.PriorityRule(
priority_value="HubDist.500",
priority_type=maproulette.priority_rule.Types.DOUBLE,
priority_operator=maproulette.priority_rule.NumericOperators.GREATER_THAN_OR_EQUAL
)
]
).to_json()
chal.medium_priority_rule = \
maproulette.PriorityRuleModel(
condition=maproulette.priority_rule.Conditions.AND,
rules=[
maproulette.PriorityRule(
priority_value="HubDist.500",
priority_type=maproulette.priority_rule.Types.DOUBLE,
priority_operator=maproulette.priority_rule.NumericOperators.LESS_THAN
),
maproulette.PriorityRule(
priority_value="HubDist.100",
priority_type=maproulette.priority_rule.Types.DOUBLE,
priority_operator=maproulette.priority_rule.NumericOperators.GREATER_THAN_OR_EQUAL
)
]
).to_json()
chal.low_priority_rule = \
maproulette.PriorityRuleModel(
condition=maproulette.priority_rule.Conditions.AND,
rules=[
maproulette.PriorityRule(
priority_value="HubDist.100",
priority_type=maproulette.priority_rule.Types.DOUBLE,
priority_operator=maproulette.priority_rule.NumericOperators.LESS_THAN
),
maproulette.PriorityRule(
priority_value="HubDist.0",
priority_type=maproulette.priority_rule.Types.DOUBLE,
priority_operator=maproulette.priority_rule.NumericOperators.GREATER_THAN_OR_EQUAL
)
]
).to_json()
chal.default_zoom = 19
chal.min_zoom = 17
data = chal_api.create_challenge(chal)["data"]
chal = maproulette.ChallengeModel(name=data["name"], id=data["id"], parent=proj.id)
print("Created challenge ID", chal.id)
print("Uploading tasks")
for line in chal_file:
chal_api.add_tasks_to_challenge({k: v for k, v in json.loads(line).items() if v is not None}, chal.id)