2014-02-23 54 views
1

我是python的新手,所以請原諒我這個新手問題。我有一個類和函數:如何將變量傳遞給mapnik數據源類?

class Datasource(mapnik.PythonDatasource): 
    def __init__(self): 
     super(Datasource, self).__init__() 

    def features(self, query): 
     return mapnik.PythonDatasource.wkb_features(mvt) 

def tileLayer(request, version, shapefile_id, zoom, x, y): 
    mvt = requestHandler(request, zoom, x, y) 

    datasource = mapnik.Python(factory='Datasource') 

我怎麼能叫mapnik.Python(factory='Datasource')

回答

1

時,您可以添加額外的參數mapnik.Python方法傳遞mvt變量的Datasource類 - 它們將被傳遞到數據源__init__

mvt = requestHandler(request, zoom, x, y) 
datasource = mapnik.Python(factory='Datasource', mvt=mvt) 

附加參數Datasource__init__方法定義,並用它來保存mvt in class instance:

class Datasource(mapnik.PythonDatasource): 
    def __init__(self, mvt): 
     super(Datasource, self).__init__() 
     self.mvt = mvt 

    def features(self, query): 
     return mapnik.PythonDatasource.wkb_features(self.mvt)