This repository was archived by the owner on Jul 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocode.py
More file actions
77 lines (67 loc) · 2.9 KB
/
geocode.py
File metadata and controls
77 lines (67 loc) · 2.9 KB
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
from geopy.geocoders import Nominatim
import json
def geocode(targetFolder, warnSkip=False):
geolocator = Nominatim(user_agent='geopy test')
inputData = json.load(open(f'{targetFolder}/geocode/inputData.json'))
try:
outputDataLoaded = json.load(
open(f'{targetFolder}/geocode/outputData.json'))
except:
print('⚠️ outputData file not found: No data with be auto-loaded')
try:
manualData = json.load(open(f'{targetFolder}/geocode/outputManualData.json'))
except:
manualData = {}
print('⚠️ outputManual file not found: No manual data with be auto-loaded')
stationPosManualToSet = []
outputData = {}
for location in inputData:
try:
outputData[location] = outputDataLoaded[location]
print('loaded from output')
except:
try:
geocodedLocation = geolocator.geocode(location)
outputData[location] = {
"lat": geocodedLocation.latitude, "lon": geocodedLocation.longitude}
print('loaded from api')
except:
try:
outputData[location] = manualData[location]
except:
stationPosManualToSet.append(location)
print(f'⚠️ No data found for {location}')
else:
print(f'🎉 Manual data auto-loaded for {location}')
print('data load done')
#for location in stationPosManualToSet:
# while True:
# userInput = input(
# f'Enter lat & lon for {location} separated by a comma\nExample: 19.8,20.95\n')
# result = userInput.split(',')
# try:
# lat = float(result[0])
# lon = float(result[1])
# except:
# print(f'{userInput} is not valid, please only enter numbers')
# if input(f'Location Name: {location}\nLat: {result[0]}\nLon: {result[1]}\nPress "y" to confirm:\n') == 'y':
# outputData[location] = manualData[location] = {"lat": lat, "lon": lon}
# print(f'✔ {location} data set')
# break
# else:
# if warnSkip:
# print('⚠ Skipping items may cause unexpected behavior')
# if input("s to skip") == 's':
# break
print('🥳 Data Ready 🥳')
#if warnSkip:
# print('⚠ Not saving data may cause unexpected behavior')
#
#if input('Would you like to save data?\n') == 'y':
with open(f'{targetFolder}/geocode/outputData.json', "w") as outfile:
json.dump(outputData, outfile)
#print('✔ Data Saved')
#if input('Would you like to save manually filled data?\n') == 'y':
with open(f'{targetFolder}/geocode/outputManualData.json', "w") as outfile:
json.dump(manualData, outfile)
#print('✔ Manual Data Saved')