Skip to content

Instantly share code, notes, and snippets.

@procrastinatio
Created May 11, 2016 10:03
Show Gist options
  • Save procrastinatio/713a92701651bc2b82090aa61a902359 to your computer and use it in GitHub Desktop.
Save procrastinatio/713a92701651bc2b82090aa61a902359 to your computer and use it in GitHub Desktop.
Using Mapproxy for Grid operation
>>> from mapproxy.srs import SRS
>>> from mapproxy.grid import TileGrid, tile_grid
# Grid creation
>>> grid = TileGrid(SRS(21781),
... bbox=(20000,30000,900000,350000), origin='ul',
... res=[4000,3750,3500,3250,3000,2750,2500,2250,2000,1750,1500,1250,1000,750,650,500,250,100,50,20,10,5,2.5,2,1.5,1,0.5,0.25,0.1])
# Grid origin
>>> grid.origin
'ul'
# Grid BBOX
>>> grid.bbox
(20000, 30000, 900000, 350000)
# Resolution at level 0
>>> grid.resolution(0)
4000
# A given tile bbox
>>> grid.tile_bbox((0, 0, 1))
(20000.0, -610000.0, 980000.0, 350000.0)
# A tile address, given a coordinate
>>> grid.tile(600000,200000,14)
(3, 0, 14)
# Grid pyramid
>>> for grid_size in grid.grid_sizes:
... print grid_size
...
(1, 1)
(1, 1)
(1, 1)
(2, 1)
(2, 1)
(2, 1)
(2, 1)
(2, 1)
(2, 1)
(2, 1)
(3, 1)
(3, 1)
(4, 2)
(5, 2)
(6, 2)
(7, 3)
(14, 5)
(35, 13)
(69, 25)
(172, 63)
(344, 125)
(688, 250)
(1375, 500)
(1719, 625)
(2292, 834)
(3438, 1250)
(6875, 2500)
(13750, 5000)
(34375, 12500)
# Tiles address at a given level
>>> req_bbox = (600000, 200000, 650000, 250000)
>>> bbox, grid_size, tiles= grid.get_affected_level_tiles(req_bbox, 15)
>>> bbox
(532000.0, 94000.0, 660000.0, 350000.0)
>>> grid_size
(1, 2)
>>> for t in tiles:
... print t, grid.tile_bbox(t)
...
(4, 0, 15) (532000.0, 222000.0, 660000.0, 350000.0)
(4, 1, 15) (532000.0, 94000.0, 660000.0, 222000.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment