Example #1
0
def request(config_content, layer_name, format, row, column, zoom):
    '''
    Helper method to write config_file to disk and do
    request
    '''
    if sys.version_info.major == 2:
        is_string = isinstance(config_content, basestring)
    else:
        is_string = isinstance(config_content, (str, bytes))

    if is_string:
        absolute_file_name = create_temp_file(config_content)
        config = parseConfig(absolute_file_name)

    else:
        config = parseConfig(config_content)

    layer = config.layers[layer_name]
    coord = Coordinate(int(row), int(column), int(zoom))
    mime_type, tile_content = getTile(layer, coord, format)

    if is_string:
        os.remove(absolute_file_name)

    return mime_type, tile_content
Example #2
0
    def doRequest(self):

        '''
        http://localhost:8000/tms/test-client/qdjango/10/rt/15/17410/11915.png
        http://localhost:8000/tms/test-client/qdjango/10/rt/13/4348/2979.png
        :return:
        '''

        configDict = settings.TILESTACHE_CONFIG_BASE
        configDict['layers'][self.layer_name] = Layer.objects.get(project_id=self.projectId, name=self.layer_name).tilestache_conf

        '''
        configDict['layers']['rt'] = {
            "provider": {
                "name": "url template",
                "template": "http://www502.regione.toscana.it/wmsraster/com.rt.wms.RTmap/wms?map=wmspiapae&SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&LAYERS=rt_piapae.carta_dei_caratteri_del_paesaggio.50k.ct.rt&STYLES=&FORMAT=image/png&TRANSPARENT=undefined&CRS=EPSG:3857&WIDTH=$width&HEIGHT=$height&bbox=$xmin,$ymin,$xmax,$ymax"
            },
            "projection": "spherical mercator"
        }
        '''
        config = Config.buildConfiguration(configDict)
        layer = config.layers[self.layer_name]
        coord = Coordinate(int(self.tile_row), int(self.tile_column), int(self.tile_zoom))
        tile_mimetype, tile_content = getTile(layer, coord, self.tile_format, ignore_cached=False)

        return HttpResponse(content_type=tile_mimetype, content=tile_content)
Example #3
0
def request(config_content, layer_name, format, row, column, zoom):
    '''
    Helper method to write config_file to disk and do
    request
    '''
    if sys.version_info.major == 2:
        is_string = isinstance(config_content, str)
    else:
        is_string = isinstance(config_content, (str, bytes))

    if is_string:
        absolute_file_name = create_temp_file(config_content)
        config = parseConfig(absolute_file_name)

    else:
        config = parseConfig(config_content)

    layer = config.layers[layer_name]
    coord = Coordinate(int(row), int(column), int(zoom))
    mime_type, tile_content = getTile(layer, coord, format)

    if is_string:
        os.remove(absolute_file_name)

    return mime_type, tile_content
Example #4
0
def request(config_file_content, layer_name, format, row, column, zoom):
    '''
    Helper method to write config_file_content to disk and do
    request
    '''

    absolute_file_name = create_temp_file(config_file_content)
    config = parseConfigfile(absolute_file_name)
    layer = config.layers[layer_name]
    coord = Coordinate(int(row), int(column), int(zoom))
    mime_type, tile_content = getTile(layer, coord, format)

    os.remove(absolute_file_name)

    return mime_type, tile_content
Example #5
0
    def create_tiles(self):
        '''
        Create the tiles to store in the cache
        '''
        print "Creating the tiles"
        for (offset, count, coord) in self.coordinates:
            path = '%s/%d/%d/%d.%s' % (self.layer.name(), coord.zoom, coord.column, coord.row, self.extension)
            progress = {"tile": path,
                        "offset": offset + 1,
                        "total": count}
    
            #
            # Fetch a tile.
                        
            rendered = False
            
            while not rendered:
                try:
                    mimetype, content = getTile(self.layer, coord, self.extension, self.ignore_cached)

                    if mimetype and 'json' in mimetype and self.callback:
                        js_path = '%s/%d/%d/%d.js' % (self.layer.name(), coord.zoom, coord.column, coord.row)
                        js_body = '%s(%s);' % (self.callback, content)
                        js_size = len(js_body) / 1024
                        
                        layer.config.cache.save(js_body, layer, coord, 'JS')
                    # get status
                    self.currentTile = progress['offset']  
                    print ('Creating tile {0} of {1}. Located at {2}').format(progress.offset, progress.count, progress.tile) 
                except Exception as e:
                    print e
                    break
                
                else:
                    #
                    # Successfully got the tile.
                    #
                    rendered = True
                    progress['size'] = '%dKB' % (len(content) / 1024)
                    self.totalTile =  progress['total']                  
                    
Example #6
0
    def seed(self, tiles, ignore_cached=False, callback=None, enable_retries=True):

        status = []
        error_list = []

        for (layer, coord, format, progress) in tiles:
            #
            # Fetch a tile.
            #

            attempts = enable_retries and 3 or 1
            rendered = False

            while not rendered:
                if self.verbose:
                    logging.info("%(offset)d of %(total)d..." % progress)

                try:
                    mimetype, content = getTile(layer, coord, format, ignore_cached)

                    if "json" in mimetype and callback:
                        js_path = "%s/%d/%d/%d.js" % (layer.name(), coord.zoom, coord.column, coord.row)
                        js_body = "%s(%s);" % (callback, content)
                        js_size = len(js_body) / 1024

                        layer.config.cache.save(js_body, layer, coord, "JS")
                        logging.info("%s (%dKB)" % (js_path, js_size))

                    elif callback:
                        print >> stderr, "(callback ignored)",

                except:
                    #
                    # Something went wrong: try again? Log the error?
                    #
                    attempts -= 1

                    if self.verbose:
                        print >> stderr, "Failed %s, will try %s more." % (
                            progress["tile"],
                            ["no", "once", "twice"][attempts],
                        )

                    if attempts == 0:
                        msg = "%(zoom)d/%(column)d/%(row)d\n" % coord.__dict__
                        logging.error(msg)
                        error_list.append(msg)
                        break

                else:
                    #
                    # Successfully got the tile.
                    #
                    rendered = True
                    progress["size"] = "%dKB" % (len(content) / 1024)

                    if self.verbose:
                        logging.info("%(tile)s (%(size)s)" % progress)

            status.append(progress["tile"])

        return (status, error_list)
Example #7
0
        #
        # Fetch a tile.
        #

        attempts = options.enable_retries and 3 or 1
        rendered = False

        while not rendered:
            if options.verbose:
                print('%(offset)d of %(total)d...' % progress,
                      end=' ',
                      file=stderr)

            try:
                mimetype, content = getTile(layer, coord, extension,
                                            options.ignore_cached)

                if mimetype and 'json' in mimetype and options.callback:
                    js_path = '%s/%d/%d/%d.js' % (layer.name(), coord.zoom,
                                                  coord.column, coord.row)
                    js_body = '%s(%s);' % (options.callback, content)
                    js_size = len(js_body) / 1024

                    layer.config.cache.save(js_body, layer, coord, 'JS')
                    print('%s (%dKB)' % (js_path, js_size),
                          end=' ',
                          file=stderr)

                elif options.callback:
                    print('(callback ignored)', end=' ', file=stderr)
Example #8
0
        coords = []

        for path in paths:
            path_ = pathinfo_pat.match(path)

            if path_ is None:
                raise KnownUnknown(
                    '"%s" is not a path I understand. I was expecting something more like "0/0/0.png".'
                    % path)

            row, column, zoom, extension = [path_.group(p) for p in 'yxze']
            coord = Coordinate(int(row), int(column), int(zoom))

            coords.append(coord)

    except KnownUnknown as e:
        parser.error(str(e))

    for coord in coords:
        # render
        mimetype, content = getTile(layer, coord, extension)

        # save
        handle, filename = mkstemp(prefix='tile-', suffix='.' + extension)
        os.write(handle, content)
        os.close(handle)

        # inform
        print(filename)
Example #9
0
                    "offset": offset + 1,
                    "total": count}

        #
        # Fetch a tile.
        #
        
        attempts = options.enable_retries and 3 or 1
        rendered = False
        
        while not rendered:
            if options.verbose:
                print >> stderr, '%(offset)d of %(total)d...' % progress,
    
            try:
                status_code, headers, content = getTile(layer, coord, extension, options.ignore_cached)
                mimetype = headers['Content-Type']
                
                if mimetype and 'json' in mimetype and options.callback:
                    js_path = '%s/%d/%d/%d.js' % (layer.name(), coord.zoom, coord.column, coord.row)
                    js_body = '%s(%s);' % (options.callback, content)
                    js_size = len(js_body) / 1024
                    
                    layer.config.cache.save(js_body, layer, coord, 'JS')
                    print >> stderr, '%s (%dKB)' % (js_path, js_size),
            
                elif options.callback:
                    print >> stderr, '(callback ignored)',
            
            except:
                #
Example #10
0
                raise KnownUnknown('"%s" is not a valid numeric zoom level.' % zoom)

            zooms[i] = int(zoom)

        if options.padding < 0:
            raise KnownUnknown("A negative padding will not work.")

        padding = options.padding

    except KnownUnknown, e:
        parser.error(str(e))

    for (offset, count, coord) in generateCoordinates(ul, lr, zooms, padding):
        path = "%s/%d/%d/%d.%s" % (layer.name(), coord.zoom, coord.column, coord.row, extension)

        progress = {"tile": path, "offset": offset + 1, "total": count}

        if options.verbose:
            print >> stderr, "%(offset)d of %(total)d..." % progress,

        mimetype, content = getTile(layer, coord, extension, options.ignore_cached)
        progress["size"] = "%dKB" % (len(content) / 1024)

        if options.verbose:
            print >> stderr, "%(tile)s (%(size)s)" % progress

        if progressfile:
            fp = open(progressfile, "w")
            json_dump(progress, fp)
            fp.close()
Example #11
0
                raise KnownUnknown('"%s" is not a valid numeric zoom level.' % zoom)

            zooms[i] = int(zoom)

        if options.padding < 0:
            raise KnownUnknown("A negative padding will not work.")

        padding = options.padding

    except KnownUnknown, e:
        parser.error(str(e))

    for (offset, count, coord) in generateCoordinates(ul, lr, zooms, padding):
        path = "%s/%d/%d/%d.%s" % (layer.name(), coord.zoom, coord.column, coord.row, extension)

        progress = {"tile": path, "offset": offset + 1, "total": count}

        if options.verbose:
            print >> stderr, "%(offset)d of %(total)d..." % progress,

        mimetype, content = getTile(layer, coord, extension)
        progress["size"] = "%dKB" % (len(content) / 1024)

        if options.verbose:
            print >> stderr, "%(tile)s (%(size)s)" % progress

        if progressfile:
            fp = open(progressfile, "w")
            json_dump(progress, fp)
            fp.close()
Example #12
0
    path.insert(0, '.')

    options, zooms = parser.parse_args()

    lat1, lon1, lat2, lon2 = options.bbox
    south, west = min(lat1, lat2), min(lon1, lon2)
    north, east = max(lat1, lat2), max(lon1, lon2)

    northwest = Location(north, west)
    southeast = Location(south, east)

    webmerc = SphericalMercator()

    ul = webmerc.locationCoordinate(northwest)
    lr = webmerc.locationCoordinate(southeast)

    for (i, zoom) in enumerate(zooms):
        if not zoom.isdigit():
            raise KnownUnknown('"%s" is not a valid numeric zoom level.' % zoom)

        zooms[i] = int(zoom)

    layer = SeedingLayer(options.demdir, options.tiledir, options.tmpdir, options.source)

    for (offset, count, coord) in generateCoordinates(ul, lr, zooms, 0):

        mimetype, content = getTile(layer, coord, 'TIFF', True)

        print coord
Example #13
0
        progress = {"tile": path, "offset": offset + 1, "total": count}

        #
        # Fetch a tile.
        #

        attempts = options.enable_retries and 3 or 1
        rendered = False

        while not rendered:
            if options.verbose:
                print >> stderr, '%(offset)d of %(total)d...' % progress,

            try:
                status_code, headers, content = getTile(
                    layer, coord, extension, options.ignore_cached)
                mimetype = headers['Content-Type']

                if mimetype and 'json' in mimetype and options.callback:
                    js_path = '%s/%d/%d/%d.js' % (layer.name(), coord.zoom,
                                                  coord.column, coord.row)
                    js_body = '%s(%s);' % (options.callback, content)
                    js_size = len(js_body) / 1024

                    layer.config.cache.save(js_body, layer, coord, 'JS')
                    print >> stderr, '%s (%dKB)' % (js_path, js_size),

                elif options.callback:
                    print >> stderr, '(callback ignored)',

            except:
Example #14
0
def base_layer(z, x, y):
    config = parseConfigfile('tiles.conf')
    content_type, tile = getTile(config.layers['peru'], Coordinate(y, x, z),
                                 'png')
    return send_file(StringIO(tile), mimetype=content_type)
Example #15
0
        
        layer = config.layers[options.layer]
        
        coords = []
        
        for path in paths:
            path_ = pathinfo_pat.match(path)
            
            if path_ is None:
                raise KnownUnknown('"%s" is not a path I understand. I was expecting something more like "0/0/0.png".' % path)
            
            row, column, zoom, extension = [path_.group(p) for p in 'yxze']
            coord = Coordinate(int(row), int(column), int(zoom))

            coords.append(coord)

    except KnownUnknown, e:
        parser.error(str(e))
    
    for coord in coords:
        # render
        _, _, content = getTile(layer, coord, extension)
        
        # save
        handle, filename = mkstemp(prefix='tile-', suffix='.'+extension)
        os.write(handle, content)
        os.close(handle)
        
        # inform
        print filename
Example #16
0
        coords = []

        for path in paths:
            path_ = pathinfo_pat.match(path)

            if path_ is None:
                raise KnownUnknown(
                    '"%s" is not a path I understand. I was expecting something more like "0/0/0.png".'
                    % path)

            row, column, zoom, extension = [path_.group(p) for p in 'yxze']
            coord = Coordinate(int(row), int(column), int(zoom))

            coords.append(coord)

    except KnownUnknown, e:
        parser.error(str(e))

    for coord in coords:
        # render
        _, _, content = getTile(layer, coord, extension)

        # save
        handle, filename = mkstemp(prefix='tile-', suffix='.' + extension)
        os.write(handle, content)
        os.close(handle)

        # inform
        print filename
    def start_seed(self):
        print("TilestacheSeeder run seed")

        options = self.assign_default_value(self.options)

        config = buildConfiguration(options['config'])
        layer = config.layers[options['name']]

        bbox = self.get_bbox(options['bbox'])

        self.validate_bbox(bbox)

        northwest = Location(bbox['north'], bbox['west'])
        southeast = Location(bbox['south'], bbox['east'])
        ul = layer.projection.locationCoordinate(northwest)
        lr = layer.projection.locationCoordinate(southeast)

        coordinates = self.get_coordinates(
            options, {
                'ul': ul,
                'lr': lr,
                'zooms': options['zooms'],
                'padding': options['padding']
            })
        ignore_cached = options['ignore_cached']
        extension = options['extension']
        verbose = options['verbose']

        for (offset, count, coord) in coordinates:
            path = '%s/%d/%d/%d.%s' % (layer.name(), coord.zoom, coord.column,
                                       coord.row, options['extension'])
            progress = {"tile": path, "offset": offset + 1, "total": count}
            #
            # Fetch a tile.
            #
            attempts = options['enable_retries']
            rendered = False

            while not rendered:
                if verbose:
                    print('%(offset)d of %(total)d...' % progress,
                          end=' ',
                          file=stderr)
                    # print("{offset} of {total}...".format(offset=progress['offset'], total=progress['total']))

                try:
                    mimetype, content = getTile(layer, coord, extension,
                                                ignore_cached)
                    if mimetype and 'json' in mimetype and options['callback']:
                        js_path = '%s/%d/%d/%d.js' % (layer.name(), coord.zoom,
                                                      coord.column, coord.row)
                        js_body = '%s(%s);' % (options['callback'], content)
                        js_size = len(js_body) / 1024
                        layer.config.cache.save(js_body, layer, coord, 'JS')
                        print('%s (%dKB)' % (js_path, js_size),
                              end=' ',
                              file=stderr)
                    elif options['callback']:
                        print('(callback ignored)', end=' ', file=stderr)
                except:
                    #
                    # Something went wrong: try again? Log the error?
                    #
                    attempts -= 1

                    if verbose:
                        print('Failed %s, will try %s more.' %
                              (progress['tile'], ['no', 'once', 'twice'
                                                  ][attempts]),
                              file=stderr)

                    if attempts == 0:
                        if not options['error_list']:
                            raise

                        fp = open(options['error_list'], 'a')
                        fp.write('%(zoom)d/%(column)d/%(row)d\n' %
                                 coord.__dict__)
                        fp.close()
                        break
                else:
                    #
                    # Successfully got the tile.
                    #
                    rendered = True
                    progress['size'] = '%dKB' % (len(content) / 1024)

                    if verbose:
                        print('%(tile)s (%(size)s)' % progress, file=stderr)

            if options['progressfile']:
                fp = open(options['progressfile'], 'w')
                json_dump(progress, fp)
                fp.close()