# Path: TileStache/py3_compat.py
# def is_string_type(val):
# 
# Path: TileStache/py3_compat.py
# def is_string_type(val):
# 
# Path: TileStache/Core.py
# class KnownUnknown(Exception):
#     """ There are known unknowns. That is to say, there are things that we now know we don't know.
# 
#         This exception gets thrown in a couple places where common mistakes are made.
#     """
#     pass


from time import time
from os.path import exists
from itertools import count
from glob import glob
from tempfile import mkstemp
from .py3_compat import reduce, urlopen, urljoin, urlparse, allocate_lock
from .py3_compat import unichr
from TileStache.Core import KnownUnknown
from TileStache.Geography import getProjectionByName
    from PIL import Image
import os
import logging
import json
    import mapnik
    import Image
        #
        if global_mapnik_lock.acquire():
            try:
                if self.mapnik is None:
                    self.mapnik = get_mapnikMap(self.mapfile)
                    logging.debug('TileStache.Mapnik.GridProvider.renderArea() %.3f to load %s', time() - start_time, self.mapfile)

                self.mapnik.width = width
                self.mapnik.height = height
                self.mapnik.zoom_to_box(Box2d(xmin, ymin, xmax, ymax))

                if self.layer_id_key is not None:
                    grids = []

                    for (index, fields) in self.layers:
                        datasource = self.mapnik.layers[index].datasource
                        if isinstance(fields, list):
                            fields = [str(f) for f in fields]
                        else:
                            fields = datasource.fields()
                        grid = mapnik.Grid(width, height)
                        mapnik.render_layer(self.mapnik, grid, layer=index, fields=fields)
                        grid = grid.encode('utf', resolution=self.scale, features=True)

                        for key in grid['data']:
                            grid['data'][key][self.layer_id_key] = self.mapnik.layers[index].name

                        grids.append(grid)

                    # global_mapnik_lock.release()

# Path: TileStache/py3_compat.py
# def is_string_type(val):
# 
# Path: TileStache/py3_compat.py
# def is_string_type(val):
# 
# Path: TileStache/Core.py
# class KnownUnknown(Exception):
#     """ There are known unknowns. That is to say, there are things that we now know we don't know.
# 
#         This exception gets thrown in a couple places where common mistakes are made.
#     """
#     pass


# Path: TileStache/Mapnik.py
from time import time
from os.path import exists
from itertools import count
from glob import glob
from tempfile import mkstemp
from .py3_compat import reduce, urlopen, urljoin, urlparse, allocate_lock
from .py3_compat import unichr
from TileStache.Core import KnownUnknown
from TileStache.Geography import getProjectionByName
    from PIL import Image
import os
import logging
import json
    import mapnik
    import Image

        #
        if global_mapnik_lock.acquire():
            try:
                if self.mapnik is None:
                    self.mapnik = get_mapnikMap(self.mapfile)
                    logging.debug('TileStache.Mapnik.GridProvider.renderArea() %.3f to load %s', time() - start_time, self.mapfile)

                self.mapnik.width = width
                self.mapnik.height = height
                self.mapnik.zoom_to_box(Box2d(xmin, ymin, xmax, ymax))

                if self.layer_id_key is not None:
                    grids = []

                    for (index, fields) in self.layers:
                        datasource = self.mapnik.layers[index].datasource
                        if isinstance(fields, list):
                            fields = [str(f) for f in fields]
                        else:
                            fields = datasource.fields()
                        grid = mapnik.Grid(width, height)
                        mapnik.render_layer(self.mapnik, grid, layer=index, fields=fields)
                        grid = grid.encode('utf', resolution=self.scale, features=True)

                        for key in grid['data']:
                            grid['data'][key][self.layer_id_key] = self.mapnik.layers[index].name

                        grids.append(grid)

                    # global_mapnik_lock.release()

                    outgrid = reduce(merge_grids, grids)