-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetGoogleElevationData.py
More file actions
180 lines (150 loc) · 4.61 KB
/
GetGoogleElevationData.py
File metadata and controls
180 lines (150 loc) · 4.61 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
######################
# ver 0.1
# Python 3.4
#
# Reads geodata from a file and adds the elevation data using the google elevation api:
# https://developers.google.com/maps/documentation/elevation/intro
#
# The Elevation API has the following limits in place:
#
# Users of the free API:
#
# 2500 requests per 24 hour period.
# 512 locations per request.
# 5 requests per second.
#
# Google Maps API for Work customers:
#
# 100 000 requests per 24 hour period.
# 512 locations per request.
# 10 requests per second.
#
#
######################
######################
#
# JSON response for request:
#https://maps.googleapis.com/maps/api/elevation/json?&locations=40.714728,-73.998672|-34.397,150.644
#
#{
# "results" : [
# {
# "elevation" : 8.883694648742676,
# "location" : {
# "lat" : 40.714728,
# "lng" : -73.998672
# },
# "resolution" : 76.35161590576172
# },
# {
# "elevation" : 392.5118713378906,
# "location" : {
# "lat" : -34.397,
# "lng" : 150.644
# },
# "resolution" : 152.7032318115234
# }
# ],
# "status" : "OK"
#}
#
######################
import urllib.request
import requests
import json
import glob
import os
import shutil
import sys
def getGeoDataFile (tempFileName):
if glob.glob(tempFileName):
print("Datei gefunden.")
return tempFileName
else:
fileName = ""
weiter = True
while (weiter):
fileName = input("Bitte den Dateinamen angeben (z.B. geodata.txt):")
if glob.glob(fileName):
weiter = False
print("Datei gefunden.")
return fileName
else:
print("Datei im aktuellen Ordner nicht gefunden.")
def readGeoDataFile(fileName):
geoData = []
counter = 0
if fileName is not None:
f = open(fileName, 'r')
for line in f:
if line != "":
counter += 1
geoData.append(line)
print(str(counter) + " Zeilen gelesen.")
return geoData
else:
print("Dateiname ist None!")
return
def createGeoDataString (geoData):
geodataString = ""
lineNr = 0
for line in geoData:
temp = line.strip().split(",")
lineNr += 1
if len(temp) >= 3:
if lineNr == 512: # reached 512 limit for free google elevation API
print("Limit von 512 locations für den Request erreicht.")
print("511 Locations werden übermittelt...")
#TODO: split data into 512 chunks an transmit data this way...
return geodataString
if lineNr == 1:
geodataString = geodataString + "&locations="+ temp[1] + "," + temp[2]
else:
geodataString = geodataString + "|" + temp[1] + "," + temp[2]
else:
print("Zeile " + str(lineNr) + "nicht geparsed!")
return geodataString
def getGoogleElevationData (geoData):
testUrl = "http://maps.googleapis.com/maps/api/elevation/json?&locations=52.411302,%2012.537468"
baseURL = "http://maps.googleapis.com/maps/api/elevation/json?"
elevationData = []
geoDataString = createGeoDataString(geoData)
url = baseURL + geoDataString + "&sensor=false"
print(url)
req = requests.get(url)
status = req.json()["status"]
results = req.json()["results"]
if status == "OK":
for data in results:
elevationData.append(data["elevation"])
return elevationData
else:
print("An error occured: "+ status)
return
def writeGeoData (geoData, elevationData, path, fileName):
textLine = ""
if fileName is not None:
baseName = os.path.splitext(fileName)[0]
baseName = baseName + "_elev.txt"
else:
baseName = "output.txt"
file = open(fileName, 'w+')
for index, line in enumerate(geoData):
print(line)
###########
# without globals, still crappy
##########
def main(argv=None):
if argv is None:
argv = sys.argv
print(argv)
if len(argv) > 1:
fileName = getGeoDataFile(argv[1])
else:
fileName = getGeoDataFile(" ")
geoData = readGeoDataFile(fileName)
elevationData = getGoogleElevationData(geoData)
writeGeoData(geoData, elevationData, argv[0], fileName)
sys.exit()
if __name__ == "__main__":
main()