-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadWebTiles.m
150 lines (130 loc) · 4.07 KB
/
readWebTiles.m
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
function [imgArray, scale] = readWebTiles(latitude, longitude, zoom, pad, url)
%Reads a map area from an online tile server
%
%Obey the relevant tile server usage policy or your API credentials may be
%revoked
%
% EXAMPLE
% [imgArray, scale] = readWebTiles(41.661, -91.536, 15, 2, 'https://a.tile.openstreetmap.org/{zoom}/{x}/{y}.png');
%
% INPUT:
% latitude - the latitude of the desired location
% (-90,90) in degrees
% longitude - the longitude of the desired location
% [-180,180] in degrees
% zoom - the level of zoom
% [0,infinity)
% pad - the amount of tile to include from the center, ie 2 would pad
% by 2 in each direction for a 5x5 grid of tiles loaded
% [0,infinity)
% url - the url of the tile server, formatted with wildcards
% surrounded by curly brackets, variables x, y, zoom
%
% OUTPUT:
% imgArray - the map area presented as an array of 0-255 values
% scale - the scale measured as meters/pixel
%
%https://wiki.openstreetmap.org/wiki/Tiles
if latitude <= -90 || 90 <= latitude
error('latitude must be between (-90,90) degrees');
end
if longitude < -180 || 180 < longitude
error('longitude must be between [-180,180] degrees');
end
if zoom < 0
error('zoom must be non-negative');
end
if rem(zoom,1) ~= 0
error('zoom must be an integer');
end
if pad < 0
error('Pad must be non-negative');
end
if rem(pad,1) ~= 0
error('pad must be an integer');
end
start=1;
count=1;
count2=1;
for i=1:length(url)
if url(i) == '{'
subs = url(start:i-1);
if count == 1
s1 = subs;
elseif count == 2
s2 = subs;
elseif count == 3
s3 = subs;
else
error('Illegal formatting');
end
start = i+1;
count = count + 1;
end
if url(i) == '}'
subs = url(start:i-1);
if subs == 'x'
if count2 == 1
xs = 1;
elseif count2 == 2
xs = 2;
elseif count2 == 3
xs = 3;
else
error('Illegal formatting');
end
elseif subs == 'y'
if count2 == 1
ys = 1;
elseif count2 == 2
ys = 2;
elseif count2 == 3
ys = 3;
else
error('Illegal formatting');
end
elseif isequal(subs,'zoom')
if count2 == 1
zs = 1;
elseif count2 == 2
zs = 2;
elseif count2 == 3
zs = 3;
else
error('Illegal formatting');
end
else
error('Illegal formatting');
end
start = i+1;
count2 = count2 +1;
end
end
s4 = url(start:length(url));
[tileX, tileY] = EPSG3857(latitude,longitude,zoom);
%determine size of tiles
tileSize = size(webread(s1 + string(0) + s2 + string(0) + s3 + string(0) + s4), 1);
imgArray = zeros(tileSize*(1+pad*2),tileSize*(1+pad*2));
for i=-pad:pad
for j=-pad:pad
x = i + tileX;
y = j + tileY;
if xs==1 && ys==2 && zs==3
url = s1 + string(x) + s2 + string(y) + s3 + string(zoom) + s4;
elseif xs==1 && ys==3 && zs==2
url = s1 + string(x) + s2 + string(zoom) + s3 + string(y) + s4;
elseif xs==2 && ys==1 && zs==3
url = s1 + string(y) + s2 + string(x) + s3 + string(zoom) + s4;
elseif xs==2 && ys==3 && zs==1
url = s1 + string(zoom) + s2 + string(x) + s3 + string(y) + s4;
elseif xs==3 && ys==1 && zs==2
url = s1 + string(y) + s2 + string(zoom) + s3 + string(x) + s4;
elseif xs==3 && ys==2 && zs==1
url = s1 + string(zoom) + s2 + string(y) + s3 + string(x) + s4;
end
imgArray(tileSize*(j+pad)+1 : tileSize*(j+pad+1) , tileSize*(i+pad)+1 : tileSize*(i+pad+1)) = webread(url);
end
end
scale = pixelScale(latitude, zoom, tileSize);
imshow(rescale(imgArray))
end