Skip to content

Commit e5a43b8

Browse files
committed
Move Grid creation for mercator and geodetic pyramids to static methods on the Grid class
1 parent 14e925f commit e5a43b8

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

src/main/groovy/geoscript/layer/Grid.groovy

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,33 @@ class Grid {
5757
this.yResolution = yResolution
5858
this.size = this.width * this.height
5959
}
60+
61+
/**
62+
* Create Grids for a Global Geodetic Pyramid as defined by
63+
* http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification#global-geodetic
64+
* @param maxZoomLevel The max zoom level
65+
* @return A List of Grids
66+
*/
67+
static createGlobalGeodeticGrids(int maxZoomLevel) {
68+
(0..maxZoomLevel).collect { int z ->
69+
int col = Math.pow(2, z + 1)
70+
int row = Math.pow(2, z)
71+
double res = 0.703125 / Math.pow(2, z)
72+
new Grid(z, col, row, res, res)
73+
}
74+
}
75+
76+
/**
77+
* Create Grids for a Global Mercator Pyramid as defined by
78+
* http://wiki.openstreetmap.org/wiki/Zoom_levels
79+
* @param maxZoomLevel The max zoom level
80+
* @return A List of Grids
81+
*/
82+
static createGlobalMercatorGrids(int maxZoomLevel) {
83+
(0..maxZoomLevel).collect { int z ->
84+
int n = Math.pow(2, z)
85+
double res = 156412.0 / n
86+
new Grid(z, n, n, res, res)
87+
}
88+
}
6089
}

src/main/groovy/geoscript/layer/Pyramid.groovy

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class Pyramid {
234234

235235
/**
236236
* Create a Pyramid with Grids for common global web mercator tile sets.
237+
* http://wiki.openstreetmap.org/wiki/Zoom_levels
237238
* @param options The optional named parameters:
238239
* <ul>
239240
* <li>origin = The Pyramid Origin (defaults to bottom left)</li>
@@ -254,12 +255,7 @@ class Pyramid {
254255
tileHeight: 256
255256
)
256257
int maxZoom = options.get("maxZoom", 19)
257-
p.grids = (0..maxZoom).collect { int z ->
258-
int n = Math.pow(2, z)
259-
// http://wiki.openstreetmap.org/wiki/Zoom_levels
260-
double res = 156412.0 / n
261-
new Grid(z, n, n, res, res)
262-
}
258+
p.grids = Grid.createGlobalMercatorGrids(maxZoom)
263259
p
264260
}
265261

@@ -283,12 +279,7 @@ class Pyramid {
283279
tileHeight: 256
284280
)
285281
int maxZoom = options.get("maxZoom", 19)
286-
p.grids = (0..maxZoom).collect { int z ->
287-
int col = Math.pow(2, z + 1)
288-
int row = Math.pow(2, z)
289-
double res = 0.703125 / Math.pow(2, z)
290-
new Grid(z, col, row, res, res)
291-
}
282+
p.grids = Grid.createGlobalGeodeticGrids(maxZoom)
292283
p
293284
}
294285
}

src/test/groovy/geoscript/layer/GridTest.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,28 @@ class GridTest {
2222
assertEquals "Grid(z:1, width:2, height:3, size:6, xResolution:520.0, yResolution:250.0)", grid.toString()
2323
}
2424

25+
@Test
26+
void createGlobalGeodeticGrids() {
27+
List<Grid> grids = Grid.createGlobalGeodeticGrids(5)
28+
assertEquals(6, grids.size())
29+
assertEquals(0, grids.get(0).z)
30+
assertEquals(0.703125, grids.get(0).xResolution, 0.001)
31+
assertEquals(0.703125, grids.get(0).yResolution, 0.001)
32+
assertEquals(5, grids.get(5).z)
33+
assertEquals(0.02197265625, grids.get(5).xResolution, 0.001)
34+
assertEquals(0.02197265625, grids.get(5).yResolution, 0.001)
35+
}
36+
37+
@Test
38+
void createGlobalMercatorGrids() {
39+
List<Grid> grids = Grid.createGlobalMercatorGrids(5)
40+
assertEquals(6, grids.size())
41+
assertEquals(0, grids.get(0).z)
42+
assertEquals(156412.0, grids.get(0).xResolution, 0.001)
43+
assertEquals(156412.0, grids.get(0).yResolution, 0.001)
44+
assertEquals(5, grids.get(5).z)
45+
assertEquals(4887.875, grids.get(5).xResolution, 0.001)
46+
assertEquals(4887.875, grids.get(5).yResolution, 0.001)
47+
}
48+
2549
}

0 commit comments

Comments
 (0)