Skip to content

Commit

Permalink
Add totalNumberOfTiles with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loicgasser committed Dec 7, 2017
1 parent 941d530 commit f7d7141
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gatilegrid/tilegrids.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def tileAddress(self, zoom, point):
]

def intersectsExtent(self, extent):
"Determine if an extent intersects this instance extent"
return \
self.extent[0] <= extent[2] and self.extent[2] >= extent[0] and \
self.extent[1] <= extent[3] and self.extent[3] >= extent[1]
Expand Down Expand Up @@ -273,6 +274,18 @@ def numberOfTilesAtZoom(self, zoom):
[minRow, minCol, maxRow, maxCol] = self.getExtentAddress(zoom)
return (maxCol - minCol + 1) * (maxRow - minRow + 1)

def totalNumberOfTiles(self, minZoom=None, maxZoom=None):
"Return the total number of tiles for this instance extent"
nbTiles = 0
minZoom = minZoom or 0
if maxZoom:
maxZoom = maxZoom + 1
else:
maxZoom = len(self.RESOLUTIONS)
for zoom in xrange(minZoom, maxZoom):
nbTiles += self.numberOfTilesAtZoom(zoom)
return nbTiles

def getResolution(self, zoom):
"Return the image resolution at a given zoom level"
return self.tileSize(zoom) / self.tileSizePx
Expand Down
39 changes: 39 additions & 0 deletions tests/test_tilegrids.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,45 @@ def testNumberOfTilesLV03(self):
self.assertEqual(nb, nbx * nby)
self.assertGreater(nbx, nby)

def testTotalNumberOfTilesLV03(self):
zoom = 20
gagrid = GeoadminTileGridLV03()

[minRow, minCol, maxRow, maxCol] = gagrid.getExtentAddress(zoom)
nb20 = gagrid.numberOfTilesAtZoom(zoom)
nbx = gagrid.numberOfXTilesAtZoom(zoom)
nby = gagrid.numberOfYTilesAtZoom(zoom)
self.assertGreater(maxCol, maxRow)
self.assertEqual(len([t for t in gagrid.iterGrid(zoom, zoom)]), nb20)
self.assertEqual(nb20, 23500)
self.assertEqual(nb20, nbx * nby)
self.assertGreater(nbx, nby)

zoom = 21
[minRow, minCol, maxRow, maxCol] = gagrid.getExtentAddress(zoom)
nb21 = gagrid.numberOfTilesAtZoom(zoom)
nbx = gagrid.numberOfXTilesAtZoom(zoom)
nby = gagrid.numberOfYTilesAtZoom(zoom)
self.assertGreater(maxCol, maxRow)
self.assertEqual(len([t for t in gagrid.iterGrid(zoom, zoom)]), nb21)
self.assertEqual(nb21, 93750)
self.assertEqual(nb21, nbx * nby)
self.assertGreater(nbx, nby)

zoom = 22
[minRow, minCol, maxRow, maxCol] = gagrid.getExtentAddress(zoom)
nb22 = gagrid.numberOfTilesAtZoom(zoom)
nbx = gagrid.numberOfXTilesAtZoom(zoom)
nby = gagrid.numberOfYTilesAtZoom(zoom)
self.assertGreater(maxCol, maxRow)
self.assertEqual(len([t for t in gagrid.iterGrid(zoom, zoom)]), nb22)
self.assertEqual(nb22, 375000)
self.assertEqual(nb22, nbx * nby)
self.assertGreater(nbx, nby)

self.assertEqual(gagrid.totalNumberOfTiles(20, 22), nb20 + nb21 + nb22)
self.assertEqual(gagrid.totalNumberOfTiles(), 285721952)

def testNumberOfTilesLV95(self):
zoom = 20
gagrid = GeoadminTileGridLV95()
Expand Down

0 comments on commit f7d7141

Please sign in to comment.