forked from enricofer/go2mapillary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapillary_coverage.py
287 lines (249 loc) · 12.1 KB
/
mapillary_coverage.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# -*- coding: utf-8 -*-
"""
/***************************************************************************
go2mapillary
A QGIS plugin
mapillary explorer
-------------------
begin : 2016-01-21
git sha : $Format:%H$
copyright : (C) 2016 by enrico ferreguti
email : enricofer@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import math
import os
import urllib
from shapely.geometry import Polygon
import mapbox_vector_tile
import requests
import math
import json
import datetime
import mercantile
import tempfile
from qgis.PyQt.QtCore import QSettings
from qgis.core import QgsPointXY, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsVectorLayer, QgsProject
SERVER_URL = r"https://d25uarhxywzl1j.cloudfront.net/v0.1/{z}/{x}/{y}.mvt"
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)
def num2deg(xtile, ytile, zoom):
n = 2.0 ** zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (lat_deg, lon_deg)
def ZoomForPixelSize(pixelSize):
"Maximal scaledown zoom of the pyramid closest to the pixelSize."
for i in range(30):
if pixelSize > (180 / 256.0 / 2**i):
return i-1 if i!=0 else 0 # We don't want to scale up
#get the range of tiles that intersect with the bounding box of the polygon
def getTileRange(bnds, zoom):
xm=bnds[0]
xmx=bnds[2]
ym=bnds[1]
ymx=bnds[3]
bottomRight=(xmx,ym)
starting=deg2num(ymx,xm, zoom)
ending=deg2num(ym,xmx, zoom) # this will be the tiles containing the ending
x_range=(starting[0],ending[0])
y_range=(starting[1],ending[1])
return(x_range,y_range)
#to get the tile as a polygon object
def getTileASpolygon(z,y,x):
nw=num2deg(x,y,z)
se=num2deg(x+1, y+1, z)
xm=nw[1]
xmx=se[1]
ym=se[0]
ymx=nw[0]
tile_bound=Polygon([(xm,ym),(xmx,ym),(xmx,ymx),(xm,ymx)])
return tile_bound
#to tell if the tile intersects with the given polygon
def doesTileIntersects(z, y, x, polygon):
if(z<10): #Zoom tolerance; Below these zoom levels, only check if tile intersects with bounding box of polygon
return True
else:
#get the four corners
tile=getTileASpolygon(x,y,z)
return polygon.intersects(tile)
#convert the URL to get URL of Tile
def getURL(x,y,z,url):
u=url.replace("{x}", str(x))
u=u.replace("{y}", str(y))
u=u.replace("{z}", str(z))
return u
def getProxiesConf():
s = QSettings() # getting proxy from qgis options settings
proxyEnabled = s.value("proxy/proxyEnabled", "")
proxyType = s.value("proxy/proxyType", "")
proxyHost = s.value("proxy/proxyHost", "")
proxyPort = s.value("proxy/proxyPort", "")
proxyUser = s.value("proxy/proxyUser", "")
proxyPassword = s.value("proxy/proxyPassword", "")
if proxyEnabled == "true" and proxyType == 'HttpProxy': # test if there are proxy settings
proxyDict = {
"http": "http://%s:%s@%s:%s" % (proxyUser, proxyPassword, proxyHost, proxyPort),
"https": "http://%s:%s@%s:%s" % (proxyUser, proxyPassword, proxyHost, proxyPort)
}
return proxyDict
else:
return None
class mapillary_coverage:
expire_time = datetime.timedelta(hours=12)
def __init__(self,iface):
self.iface = iface
self.cache_dir = os.path.join(tempfile.gettempdir(),'go2mapillary')
print ("CACHE_DIR", self.cache_dir)
if not os.path.exists(self.cache_dir):
os.makedirs(self.cache_dir)
self.actual_ranges = None
self.images = None
def transformToWGS84(self, pPoint):
# transformation from the current SRS to WGS84
crcMappaCorrente = self.iface.mapCanvas().mapSettings().destinationCrs() # get current crs
crsSrc = crcMappaCorrente
crsDest = QgsCoordinateReferenceSystem(4326) # WGS 84
xform = QgsCoordinateTransform(crsSrc, crsDest, QgsProject.instance())
return xform.transform(pPoint) # forward transformation: src -> dest
def download_tiles(self):
#calculate zoom_level con current canvas extents
ex = self.iface.mapCanvas().extent()
wgs84_minimum = self.transformToWGS84(QgsPointXY (ex.xMinimum(),ex.yMinimum()))
wgs84_maximum = self.transformToWGS84(QgsPointXY (ex.xMaximum(),ex.yMaximum()))
bounds =(wgs84_minimum.x(),wgs84_minimum.y(),wgs84_maximum.x(),wgs84_maximum.y())
map_units_per_pixel = (wgs84_maximum.x() - wgs84_minimum.x())/self.iface.mapCanvas().width()
zoom_level = ZoomForPixelSize(map_units_per_pixel)
if zoom_level > 14:
zoom_level = 14
ranges = getTileRange(bounds, zoom_level)
#print ("ZOOM_LEVEL", zoom_level, "RANGES", self.actual_ranges, ranges)
if not self.actual_ranges or not (
ranges[0][0]==self.actual_ranges[0][0] and
ranges[0][1]==self.actual_ranges[0][1] and
ranges[1][0]==self.actual_ranges[1][0] and
ranges[1][1]==self.actual_ranges[1][1]):
print ("ZOOM_LEVEL", zoom_level, "NEW RANGES", ranges, "LAST RANGES", self.actual_ranges)
self.actual_ranges = ranges
x_range = ranges[0]
y_range = ranges[1]
overview_features = []
coverage_features = []
images_features = []
for y in range(y_range[0], y_range[1] + 1):
for x in range(x_range[0], x_range[1] + 1):
folderPath = os.path.join(self.cache_dir, str(zoom_level), str(x))
filePathMvt = os.path.join(folderPath, str(y) + '.mvt')
#filePathJson = os.path.join(folderPath, str(y) + '.json')
if not os.path.exists(folderPath):
os.makedirs(folderPath)
res = None
if not os.path.exists(filePathMvt) or (datetime.datetime.fromtimestamp(os.path.getmtime(filePathMvt)) < (datetime.datetime.now() - self.expire_time) ):
# make the URL
url = getURL(x, y, zoom_level, SERVER_URL)
res = requests.get(url, proxies=getProxiesConf())
print ('MISS',x, y, zoom_level, res, url)
if res.status_code == 200:
with open(filePathMvt, 'wb') as f:
f.write(res.content)
print (os.path.getmtime(filePathMvt))
if os.path.exists(filePathMvt):
if not res:
with open(filePathMvt, "rb") as f:
mvt = f.read()
print ('CACHE', x, y, zoom_level)
else:
mvt = res.content
bounds = mercantile.bounds(x,y,zoom_level)
tile_box = (bounds.west,bounds.south,bounds.east,bounds.north)
json_data = mapbox_vector_tile.decode(mvt, quantize_bounds=tile_box)
if "mapillary-sequence-overview" in json_data:
overview_features = overview_features + json_data["mapillary-sequence-overview"]["features"]
elif "mapillary-sequences" in json_data:
coverage_features = coverage_features + json_data["mapillary-sequences"]["features"]
if "mapillary-images" in json_data:
images_features = images_features + json_data["mapillary-images"]["features"]
geojson_overview_file = os.path.join(self.cache_dir, "mapillary_overview.geojson")
if overview_features:
self.overview = True
geojson_overview = {
"type": "FeatureCollection",
"features": overview_features
}
with open(geojson_overview_file, 'w') as outfile:
json.dump(geojson_overview, outfile)
else:
self.overview = None
if os.path.exists(geojson_overview_file):
os.remove(geojson_overview_file)
geojson_coverage_file = os.path.join(self.cache_dir, "mapillary_coverage.geojson")
if coverage_features:
self.coverage = True
geojson_coverage = {
"type": "FeatureCollection",
"features": coverage_features
}
with open(geojson_coverage_file, 'w') as outfile:
json.dump(geojson_coverage, outfile)
else:
self.coverage = None
if os.path.exists(geojson_coverage_file):
os.remove(geojson_coverage_file)
geojson_images_file = os.path.join(self.cache_dir, "mapillary_images.geojson")
if zoom_level==14 and images_features:
self.images = True
geojson_images = {
"type": "FeatureCollection",
"features": images_features
}
with open(geojson_images_file, 'w') as outfile:
json.dump(geojson_images, outfile)
else:
self.images = None
if os.path.exists(geojson_images_file):
os.remove(geojson_images_file)
else:
pass
#print ("SAME RANGES")
def has_overview(self):
return self.overview
def has_coverage(self):
return self.coverage
def has_images(self):
return self.images
def get_overview_layer(self):
if self.overview:
self.mapillary_overview_layer = QgsVectorLayer(os.path.join(self.cache_dir, 'mapillary_overview.geojson'), "Mapillary Coverage", "ogr")
self.mapillary_overview_layer.setCrs(QgsCoordinateReferenceSystem(4326))
else:
self.mapillary_overview_layer = None
return self.mapillary_overview_layer
def get_coverage_layer(self):
if self.coverage:
self.mapillary_coverage_layer = QgsVectorLayer(os.path.join(self.cache_dir, 'mapillary_coverage.geojson'), "Mapillary Coverage", "ogr")
self.mapillary_coverage_layer.setCrs(QgsCoordinateReferenceSystem(4326))
else:
self.mapillary_coverage_layer = None
return self.mapillary_coverage_layer
def get_images_layer(self):
if self.images:
self.mapillary_images_layer = QgsVectorLayer(os.path.join(self.cache_dir, 'mapillary_images.geojson'), "Mapillary Images", "ogr")
self.mapillary_images_layer.setCrs (QgsCoordinateReferenceSystem(4326))
else:
self.mapillary_images_layer = None
return self.mapillary_images_layer
def update_coverage(self):
self.download_tiles()