1
+ from collections import namedtuple
2
+
3
+ # Implementation code
4
+
5
+ TileDef = namedtuple ('TileDef' , 'xstart ystart xwidth ywidth' )
6
+
7
+ def create_grid_from_extent (width , height , xtile , ytile ):
8
+
9
+ return [TileDef (xstart , ystart , xtile , ytile )
10
+ for xstart in xrange (0 , width , xtile )
11
+ for ystart in xrange (0 , height , ytile )]
12
+
13
+
14
+ def round_up_divide (a , b ):
15
+ """Divide a / b and round up. Integers"""
16
+ return (a + (- a % b ))// b
17
+
18
+ ## Test Code
19
+ ImageDef = namedtuple ('ImageDef' , 'width height tilewidth tileheight' )
20
+ def test_simple_grid ():
21
+ imagedef = ImageDef (1000 , 1000 , 100 , 100 )
22
+ create_and_validate_grid (imagedef )
23
+
24
+ def test_trivial_grid ():
25
+ imagedef = ImageDef (100 , 100 , 100 , 100 )
26
+ create_and_validate_grid (imagedef )
27
+
28
+ def test_slightly_larger_grid ():
29
+ imagedef = ImageDef (1001 , 1000 , 100 , 100 )
30
+ create_and_validate_grid (imagedef )
31
+
32
+
33
+ def create_and_validate_grid (imagedef ):
34
+ tiles = create_grid_from_extent (* imagedef )
35
+
36
+ num_expected_tiles = round_up_divide (imagedef .width , imagedef .tilewidth ) * round_up_divide (imagedef .height , imagedef .tileheight )
37
+ assert (len (tiles ) == num_expected_tiles )
38
+ total_width = 0
39
+ for t in tiles :
40
+ if t .ystart == 0 :
41
+ total_width += t .xwidth
42
+ assert (type (t ) == TileDef )
43
+ assert (t .xwidth <= imagedef .tilewidth )
44
+ assert (t .xstart < imagedef .width )
45
+ assert (t .xstart + t .xwidth <= imagedef .width + imagedef .tilewidth )
46
+
47
+ assert (total_width >= imagedef .width )
48
+
49
+
50
+ def test_expanded_extents ():
51
+ pass
52
+
53
+ def test_gridded_geo_box ():
54
+ pass
0 commit comments