-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmission_generation.py
147 lines (104 loc) · 4.37 KB
/
mission_generation.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import random
import math
import json
from categories import categories
import googlemaps
GMAPS_API_KEY = 'AIzaSyDHqBdySdfEGwzje_-LFwMbv7R5PLWnTac'
EARTH_RADIUS = 6371000
MISSION_MODEL = 'api.mission'
OBJECTIVE_MODEL = 'api.objective'
NUM_OBJECTIVES_MIN = 1
NUM_OBJECTIVES_MAX = 7
OBJECTIVE_RADIUS = 3000
STARTING_MISSION_ID = 1
STARTING_OBJECTIVE_ID = 1
OUTPUT_FILE_MISSIONS = 'Missions.json'
OUTPUT_FILE_OBJECTIVES = 'Objectives.json'
# https://jordinl.com/posts/2019-02-15-how-to-generate-random-geocoordinates-within-given-radius
def _get_random_coordinate_within_radius(lat, lon, radius):
lat = math.radians(lat)
lon = math.radians(lon)
distance = math.sqrt(random.random() * (radius ** 2))
delta_lat = math.cos(random.random() * math.pi) * distance / EARTH_RADIUS
sign = random.choice([-1, 1])
delta_lon = sign * math.acos((math.cos(distance / EARTH_RADIUS) - math.cos(delta_lat)) / (math.cos(lat) * math.cos(delta_lat + lat)) + 1)
return_lat = math.degrees(lat + delta_lat)
return_lon = math.degrees(lon + delta_lon)
return return_lat, return_lon
def _generate_objective(lat, lon, search_term):
gmaps = googlemaps.Client(key=GMAPS_API_KEY)
search = gmaps.places(search_term, location=(lat, lon), radius=OBJECTIVE_RADIUS)
search_choice = random.choice(search['results'])
gmaps_id = search_choice['id']
name = search_choice['name']
formatted_address = search_choice['formatted_address']
location_lat = search_choice['geometry']['location']['lat']
location_lon = search_choice['geometry']['location']['lng']
objective = {
gmaps_id: {
'name': name,
'formatted_address': formatted_address,
'latitude': location_lat,
'longitude': location_lon
}}
return objective
def _generate_mission(lat, lon, radius):
# Pick a random category and number of objectives
category = random.choice(categories)
num_objectives = random.randrange(NUM_OBJECTIVES_MIN, NUM_OBJECTIVES_MAX + 1)
print(f'DEBUG: Category: {category["name"]}, {num_objectives} objectives')
# Get random coordinate within radius
gen_lat, gen_long = _get_random_coordinate_within_radius(lat, lon, radius)
print(f'DEBUG: Gen coordinates: ({gen_lat}, {gen_long})')
# Generate unique set of objectives
objectives = []
while len(objectives) < num_objectives:
search_term = random.choice(category['types'])
objective = _generate_objective(gen_lat, gen_long, search_term.replace('_', ' '))
if objective not in objectives:
objectives.append(objective)
# Re-format dictionary to make more sense
mission = dict()
mission['category'] = category['name']
mission['latitude'] = lat
mission['longitude'] = lon
mission['objectives'] = []
for objective in objectives:
gmaps_id = list(objective.keys())[0]
name = objective[gmaps_id]['name']
formatted_address = objective[gmaps_id]['formatted_address']
latitude = objective[gmaps_id]['latitude']
longitude = objective[gmaps_id]['longitude']
mission['objectives'].append({'id': gmaps_id,
'name': name,
'formatted_address': formatted_address,
'latitude': latitude,
'longitude': longitude})
return mission
def main():
print('========== ExploreYourCity Mission Generator ==========')
# latitude = float('Region latitude: ')
# longitude = float('Region longitude: ')
# Temp
latitude = 42.336040
longitude = -71.095378
radius = 5 * 1000
count = int(input('How many missions to generate?: '))
missions = []
for _ in range(count):
mission = _generate_mission(latitude, longitude, radius)
missions.append(mission)
mission_id = STARTING_MISSION_ID
objective_id = STARTING_OBJECTIVE_ID
for mission in missions:
mission_output = {'model': MISSION_MODEL,
'pk': mission_id,
'fields': {
'latitude': mission['latitude'],
'longitude': mission['longitude']
}}
mission_id += 1
for objective in mission['objectives']:
objective_data = {}
if __name__ == '__main__':
main()