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
#[derive(Debug, PartialEq)]
pub struct Extent {
pub minx: f64,
pub miny: f64,
pub maxx: f64,
pub maxy: f64,
}
#[derive(Debug, PartialEq)]
pub struct ExtentInt {
pub minx: u32,
pub miny: u32,
pub maxx: u32,
pub maxy: u32,
}
#[derive(Debug, PartialEq)]
pub enum Origin {
TopLeft,
BottomLeft,
}
#[derive(Debug, PartialEq)]
pub enum Unit {
M,
DD,
Ft,
}
#[derive(Debug)]
pub struct Grid {
width: u16,
height: u16,
pub extent: Extent,
pub srid: i32,
pub units: Unit,
resolutions: Vec<f64>,
pub origin: Origin,
pub reverse_y: bool,
}
impl Grid {
pub fn wgs84() -> Grid {
Grid {
width: 256,
height: 256,
extent: Extent {
minx: -180.0,
miny: -90.0,
maxx: 180.0,
maxy: 90.0,
},
srid: 4326,
units: Unit::DD,
resolutions: vec![0.703125000000000,
0.351562500000000,
0.175781250000000,
8.78906250000000e-2,
4.39453125000000e-2,
2.19726562500000e-2,
1.09863281250000e-2,
5.49316406250000e-3,
2.74658203125000e-3,
1.37329101562500e-3,
6.86645507812500e-4,
3.43322753906250e-4,
1.71661376953125e-4,
8.58306884765625e-5,
4.29153442382812e-5,
2.14576721191406e-5,
1.07288360595703e-5,
5.36441802978516e-6],
origin: Origin::BottomLeft,
reverse_y: false,
}
}
pub fn web_mercator() -> Grid {
Grid {
width: 256,
height: 256,
extent: Extent {
minx: -20037508.3427892480,
miny: -20037508.3427892480,
maxx: 20037508.3427892480,
maxy: 20037508.3427892480,
},
srid: 3857,
units: Unit::M,
resolutions: vec![156543.0339280410,
78271.51696402048,
39135.75848201023,
19567.87924100512,
9783.939620502561,
4891.969810251280,
2445.984905125640,
1222.992452562820,
611.4962262814100,
305.7481131407048,
152.8740565703525,
76.43702828517624,
38.21851414258813,
19.10925707129406,
9.554628535647032,
4.777314267823516,
2.388657133911758,
1.194328566955879,
0.5971642834779395,
0.2985821417389700,
0.1492910708694850,
0.0746455354347424,
0.0373227677173712],
origin: Origin::BottomLeft,
reverse_y: true,
}
}
pub fn nlevels(&self) -> u8 {
self.resolutions.len() as u8
}
pub fn maxzoom(&self) -> u8 {
self.nlevels() - 1
}
pub fn pixel_width(&self, zoom: u8) -> f64 {
self.resolutions[zoom as usize]
}
pub fn scale_denominator(&self, zoom: u8) -> f64 {
let pixel_screen_width = 0.0254 / 96.0;
self.pixel_width(zoom) / pixel_screen_width
}
pub fn tile_extent(&self, z: u8, x: u32, y: u32) -> Extent {
let zoom = z;
let xtile = x;
let ytile = if self.reverse_y {
let res = self.resolutions[zoom as usize];
let unitheight = self.height as f64 * res;
let maxy = ((self.extent.maxy - self.extent.minx - 0.01 * unitheight) / unitheight)
.ceil() as u32;
maxy.saturating_sub(y).saturating_sub(1)
} else {
y
};
let res = self.resolutions[zoom as usize];
let tile_sx = self.width as f64;
let tile_sy = self.height as f64;
match self.origin {
Origin::BottomLeft => {
Extent {
minx: self.extent.minx + (res * xtile as f64 * tile_sx),
miny: self.extent.miny + (res * ytile as f64 * tile_sy),
maxx: self.extent.minx + (res * (xtile + 1) as f64 * tile_sx),
maxy: self.extent.miny + (res * (ytile + 1) as f64 * tile_sy),
}
}
Origin::TopLeft => {
Extent {
minx: self.extent.minx + (res * xtile as f64 * tile_sx),
miny: self.extent.maxy - (res * (ytile + 1) as f64 * tile_sy),
maxx: self.extent.minx + (res * (xtile + 1) as f64 * tile_sx),
maxy: self.extent.maxy - (res * ytile as f64 * tile_sy),
}
}
}
}
pub fn level_limit(&self, zoom: u8) -> (u32, u32) {
let res = self.resolutions[zoom as usize];
let unitheight = self.height as f64 * res;
let unitwidth = self.width as f64 * res;
let maxy = ((self.extent.maxy - self.extent.miny - 0.01 * unitheight) / unitheight)
.ceil() as u32;
let maxx = ((self.extent.maxx - self.extent.miny - 0.01 * unitwidth) / unitwidth)
.ceil() as u32;
(maxx, maxy)
}
pub fn tile_limits(&self, extent: Extent, tolerance: i32) -> Vec<ExtentInt> {
const EPSILON: f64 = 0.0000001;
let nlevels = self.resolutions.len() as u8;
(0..nlevels)
.map(|i| {
let res = self.resolutions[i as usize];
let unitheight = self.height as f64 * res;
let unitwidth = self.width as f64 * res;
let (level_maxx, level_maxy) = self.level_limit(i);
let (mut minx, mut maxx, mut miny, mut maxy) =
match self.origin {
Origin::BottomLeft => {
((((extent.minx - self.extent.minx) / unitwidth + EPSILON)
.floor() as i32) - tolerance,
(((extent.maxx - self.extent.minx) / unitwidth - EPSILON)
.ceil() as i32) + tolerance,
(((extent.miny - self.extent.miny) / unitheight + EPSILON)
.floor() as i32) - tolerance,
(((extent.maxy - self.extent.miny) / unitheight - EPSILON)
.ceil() as i32) + tolerance)
}
Origin::TopLeft => {
((((extent.minx - self.extent.minx) / unitwidth + EPSILON)
.floor() as i32) - tolerance,
(((extent.maxx - self.extent.minx) / unitwidth - EPSILON)
.ceil() as i32) + tolerance,
(((self.extent.maxy - extent.maxy) / unitheight + EPSILON)
.floor() as i32) - tolerance,
(((self.extent.maxy - extent.miny) / unitheight - EPSILON)
.ceil() as i32) + tolerance)
}
};
if minx < 0 {
minx = 0;
}
if maxx > level_maxx as i32 {
maxx = level_maxx as i32
};
if miny < 0 {
miny = 0
};
if maxy > level_maxy as i32 {
maxy = level_maxy as i32
};
ExtentInt {
minx: minx as u32,
maxx: maxx as u32,
miny: miny as u32,
maxy: maxy as u32,
}
})
.collect()
}
}