160 9 Tile Serving
Listing 9.4 Python code for tile service with encoded parameters.
1 import cgi # the cgi module is only used for some query string parsing
2 class TileWSGIApp :
3
4 def init (self , datastore):
5self.datastore = datastore
6
7 def doGet ( self , environ , start response):
8status=’200OK’
9 headers = [( ’Content−type’, ’text/plain’)]
10
11 queryParams = cgi . FieldStorage (environ [ ’wsgi. input ’] , environ=environ )
12
13 requestType = queryParams. getfirst ( ’REQUEST’ , ’’) . lower ()
14 if (requestType in [’gettile ’, ’tileexists ’, ’getformat’]):
15 layerName = queryParams. getfirst (’LAYER’, ’’)
16 l e v e l = i n t ( q u e r yPa r a m s . g e t f i r s t ( ’LEVEL ’ , −1) )
17 row = int (queryParams . getfirst ( ’ROW’ , −1) )
18 column = int (queryParams . get first ( ’COLUMN’ , −1) )
19
20 if ( requestType == ’ gettile ’) :
21 d a t a = s e l f . datastore . getTileImage (layerName , level , row,
column)
22 if (data != None):
23 headers = [( ’Content−type ’ ,
24 s e l f . datastore . getTileFormat(layerName , level ,
25 row, column))]
26
27 elif (requestType == ’tileexists ’):
28 exists = self . datastore . tileExists (layerName , level , row,
column)
29 if (exists):
30 d a t a = ’ True ’
31 else :
32 data = ’False ’
33
34 elif ( requestType == ’getformat ’) :
35 d a t a = ’%s ’ % s e l f . datastore . getTileFormat(layerName , level ,
36 row , column )
37
38 else :
39 d a t a = s e l f . getLayerList()
40 else :
41 d a t a = s e l f . getLayerList()
42
43 s t a r t response( status , headers )
44 return [data]
45
46 def getLayerList( self ):
47 strbuf = cStringIO . StringIO ()
48 l a y e r s = s e l f . datastore . getLayersAvailable ()
49 for layer in layers :
50 strbuf . write ( ’LAYER: %s\n’ % layer)
51 tileRanges = self . datastore . getTileRanges ( layer )
52 for tileRange in tileRanges :
53 if (tileRange != None):
54 strbuf . write( ’LEVEL: %s,%s,%s,%s,%s\n’ %
55 ( tileRange . level , tileRange .mincol ,
56 tileRange .minrow, tileRange .maxcol ,
57 tileRange .maxrow))
58 return strbuf.getvalue ()