-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice.py
More file actions
232 lines (172 loc) · 7.1 KB
/
service.py
File metadata and controls
232 lines (172 loc) · 7.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import requests
import json
import numpy
import gpx_parser as parser
import multiprocessing as mp
import time
import pickle
import os.path
import math
from PIL import Image
from io import BytesIO
from lxml import etree
prev = ''
class ImageData:
def __init__(self, json_data):
self.panoId = json_data['Location']['panoId']
self.pano_yaw_deg = json_data['Projection']['pano_yaw_deg']
self.lat = float(json_data['Location']['lat'])
self.lon = float(json_data['Location']['lng'])
def addExtraInfo(self, point_id, file_name):
self.point_id = point_id
self.file_name = file_name
def __str__(self):
res = 'Pano Id:' + self.panoId + '\n'
if self.point_id != None:
res += 'Id:' + str(self.point_id) + '\n'
if self.file_name != None:
res += 'File name:' + self.file_name + '\n'
return res
def getTuple(self):
return self.panoId, self.pano_yaw_deg, self.lat, self.lon, self.point_id, self.file_name
def get(link):
html = False
while 1:
try:
html = requests.get(link)
break
except requests.exceptions.ConnectionError:
print('Sleeping...')
time.sleep(5)
return html
def getPanoId(lat, lon):
html = get('https://cbk0.google.com/cbk?output=json&ll=' + str(lat) + ',' + str(lon)).json()
if 'Location' in html:
return ImageData(html)
else:
return False
def getImage(panoId, pano_yaw_deg, point_id, lat, lon, file_name, direction):
tile_size = 512
width = tile_size * 13
height = tile_size * 7
result_image = Image.new('RGB', (width, height))
for y in range(7):
for x in range(13):
response = get('https://geo0.ggpht.com/cbk?cb_client=maps_sv.tactile&authuser=0&hl=ru&gl=ru&panoid=' + panoId + '&output=tile&x='\
+ str(x) + '&y=' + str(y) + '&zoom=4&nbt&fover=2')
img = Image.open(BytesIO(response.content))
result_image.paste(im=img, box=(x * tile_size, y * tile_size))
print('Downloaded: x = {}, y = {}'.format(x, y))
shiftImage(result_image, direction - float(pano_yaw_deg))
return result_image, file_name
def getCoords(gpx_filename):
coord_list = []
with open(gpx_filename, 'r') as gpx_file:
gpx = parser.parse(gpx_file)
print("{} tracks loaded".format(len(gpx)))
for track in gpx:
print('Track with {} segments and {} points'.format(len(track), track.get_points_no()))
for segment in track:
print('Segment with %s points % len(segment)')
for point in segment:
coord_list.append(point)
return coord_list
def extendCoords(coords, distanseBetweenPoints):
print(len(coords))
newCoords = []
for i in range(len(coords) - 1):
dst = coords[i].distance_2d(coords[i + 1])
new_cnt = int(dst // distanseBetweenPoints)
lat = coords[i].latitude
lon = coords[i].longitude
if new_cnt > 0:
for j in range(new_cnt):
lat += (coords[i + 1].latitude - coords[i].latitude) / new_cnt
lon += (coords[i + 1].longitude - coords[i].longitude) / new_cnt
newCoords.append((lat, lon))
else:
newCoords.append((lat, lon))
return newCoords
def shiftImage(image, angle):
if angle < 0:
angle = 360 - abs(angle)
separator = round(image.size[0] * angle / 360)
firstImage = image.crop((0, 0, separator, image.size[1]))
secondImage = image.crop((separator, 0, image.size[0], image.size[1]))
image.paste(secondImage, box=(0, 0))
image.paste(firstImage, box=(secondImage.size[0], 0))
def calculate_initial_compass_bearing(pointA, pointB):
"""
Calculates the bearing between two points.
The formulae used is the following:
θ = atan2(sin(Δlong).cos(lat2),
cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong))
:Parameters:
- `pointA: The tuple representing the latitude/longitude for the
first point. Latitude and longitude must be in decimal degrees
- `pointB: The tuple representing the latitude/longitude for the
second point. Latitude and longitude must be in decimal degrees
:Returns:
The bearing in degrees
:Returns Type:
float
"""
if (type(pointA) != tuple) or (type(pointB) != tuple):
raise TypeError("Only tuples are supported as arguments")
lat1 = math.radians(pointA[0])
lat2 = math.radians(pointB[0])
diffLong = math.radians(pointB[1] - pointA[1])
x = math.sin(diffLong) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1)
* math.cos(lat2) * math.cos(diffLong))
initial_bearing = math.atan2(x, y)
# Now we have the initial bearing but math.atan2 return values
# from -180° to + 180° which is not what we want for a compass bearing
# The solution is to normalize the initial bearing as shown below
initial_bearing = math.degrees(initial_bearing)
compass_bearing = (initial_bearing + 360) % 360
return compass_bearing
if __name__ == '__main__':
coords = extendCoords(getCoords('route.gpx'), 5)
imageData = []
pointsDataFileName = 'points.dat'
directions = []
threads = 32
if not os.path.isfile(pointsDataFileName):
print('File will be created')
imagesCounter = 0
pool = mp.Pool(threads)
prevPanoId = ''
for i in range(0, len(coords), threads):
panoDataMap = pool.starmap(getPanoId, [(coords[j][0], coords[j][1]) for j in range(i, min(i + threads, len(coords)))])
for j in range(len(panoDataMap)):
if panoDataMap[j] != False:
if not imagesCounter or prevPanoId != panoDataMap[j].panoId:
prevPanoId = panoDataMap[j].panoId
img = panoDataMap[j]
img.addExtraInfo(i + j, str(imagesCounter) + '.jpg')
imageData.append(img.getTuple())
print(imageData[-1])
imagesCounter += 1
pool.close()
with open(pointsDataFileName, 'wb') as f:
pickle.dump(imageData, f)
else:
print('File opened')
with open(pointsDataFileName, 'rb') as f:
imageData = pickle.load(f)
for i in range(len(imageData)-1):
panoId1, pano_yaw_deg1, lat1, lon1, point_id1, file_name1 = imageData[i]
panoId2, pano_yaw_deg2, lat2, lon2, point_id2, file_name2 = imageData[i + 1]
directions.append(calculate_initial_compass_bearing((lat1, lon1), (lat2, lon2)))
directions.append(directions[-1])
count = 0
threads = 8
pool = mp.Pool(threads)
for i in range(0, len(imageData), threads):
results = pool.starmap(getImage, [(imageData[j] + (directions[j],)) for j in range(i, min(i + threads, len(imageData)))])
print(results)
for j in results:
j[0].save(j[1])
print('Saved:', j[1])
pool.close()