(self)
Generation of the base tiles (the lowest in the pyramid) directly from the input raster
Generation of the base tiles (the lowest in the pyramid) directly from the input raster
Generation of the base tiles ( the lowest in the pyramid ) directly from the input raster
def generate_base_tiles(self):
        """Generation of the base tiles (the lowest in the pyramid) directly from the input raster"""

        print('Generating Base Tiles:')

        if self.options.verbose:

            # mx, my = self.out_gt[0], self.out_gt[3] # OriginX, OriginY
            # px, py = self.mercator.MetersToPixels( mx, my, self.tmaxz)
            # print("Pixel coordinates:", px, py, (mx, my))

            print('')
            print('Tiles generated from the max zoom level:')
            print('----------------------------------------')
            print('')

        # Set the bounds

        (tminx, tminy, tmaxx, tmaxy) = self.tminmax[self.tmaxz]

        # Just the center tile
        # tminx = tminx+ (tmaxx - tminx)/2
        # tminy = tminy+ (tmaxy - tminy)/2
        # tmaxx = tminx
        # tmaxy = tminy

        ds = self.out_ds
        tilebands = self.dataBandsCount + 1
        querysize = self.querysize

        if self.options.verbose:
            print ('dataBandsCount: ', self.dataBandsCount)
            print ('tilebands: ', tilebands)

        # print(tminx, tminy, tmaxx, tmaxy)

        tcount = (1 + abs(tmaxx - tminx)) * (1 + abs(tmaxy - tminy))

        # print(tcount)

        ti = 0

        tz = self.tmaxz
        yrange = range(tmaxy, tminy - 1, -1)
        if self.options.leaflet:
            yrange = range(tminy, tmaxy + 1)

        for ty in yrange:
            for tx in range(tminx, tmaxx + 1):

                if self.stopped:
                    break
                ti += 1
                tilefilename = os.path.join(self.output, str(tz),
                        str(tx), '%s.%s' % (ty, self.tileext))
                if self.options.verbose:
                    print (ti, '/', tcount, tilefilename)  # , "( TileMapService: z / x / y )"

                if self.options.resume and os.path.exists(tilefilename):
                    if self.options.verbose:
                        print('Tile generation skiped because of --resume')
                    else:
                        self.progressbar(ti / float(tcount))
                    continue

                # Create directories for the tile

                if not os.path.exists(os.path.dirname(tilefilename)):
                    os.makedirs(os.path.dirname(tilefilename))

                if self.options.profile == 'mercator':

                    # Tile bounds in EPSG:900913

                    b = self.mercator.TileBounds(tx, ty, tz)
                elif self.options.profile == 'geodetic':
                    b = self.geodetic.TileBounds(tx, ty, tz)

                # print("\tgdalwarp -ts 256 256 -te %s %s %s %s %s %s_%s_%s.tif" % ( b[0], b[1], b[2], b[3], "tiles.vrt", tz, tx, ty))

                # Don't scale up by nearest neighbour, better change the querysize
                # to the native resolution (and return smaller query tile) for scaling

                if self.options.profile in ('mercator', 'geodetic'):
                    (rb, wb) = self.geo_query(ds, b[0], b[3], b[2],
                            b[1])
                    nativesize = wb[0] + wb[2]  # Pixel size in the raster covering query geo extent
                    if self.options.verbose:
                        print ('\tNative Extent (querysize',
                               nativesize, '): ', rb, wb)

                    # Tile bounds in raster coordinates for ReadRaster query

                    (rb, wb) = self.geo_query(
                        ds,
                        b[0],
                        b[3],
                        b[2],
                        b[1],
                        querysize=querysize,
                        )

                    (rx, ry, rxsize, rysize) = rb
                    (wx, wy, wxsize, wysize) = wb
                else:

                      # 'raster' profile:

                    tsize = int(self.tsize[tz])  # tilesize in raster coordinates for actual zoom
                    xsize = self.out_ds.RasterXSize  # size of the raster in pixels
                    ysize = self.out_ds.RasterYSize
                    if tz >= self.nativezoom:
                        querysize = self.tilesize  # int(2**(self.nativezoom-tz) * self.tilesize)

                    rx = tx * tsize
                    rxsize = 0
                    if tx == tmaxx:
                        rxsize = xsize % tsize
                    if rxsize == 0:
                        rxsize = tsize

                    rysize = 0
                    if ty == tmaxy:
                        rysize = ysize % tsize
                    if rysize == 0:
                        rysize = tsize
                    if self.options.leaflet:
                        ry = ty * tsize
                    else:
                        ry = ysize - ty * tsize - rysize

                    (wx, wy) = (0, 0)
                    (wxsize, wysize) = (int(rxsize / float(tsize)
                            * self.tilesize), int(rysize / float(tsize)
                            * self.tilesize))
                    if not self.options.leaflet:
                        if wysize != self.tilesize:
                            wy = self.tilesize - wysize

                if self.options.verbose:
                    print ('\tReadRaster Extent: ', (rx, ry, rxsize,
                           rysize), (wx, wy, wxsize, wysize))

                # Query is in 'nearest neighbour' but can be bigger in then the tilesize
                # We scale down the query to the tilesize by supplied algorithm.

                # Tile dataset in memory

                dstile = self.mem_drv.Create('', self.tilesize,
                        self.tilesize, tilebands)
                data = ds.ReadRaster(
                    rx,
                    ry,
                    rxsize,
                    rysize,
                    wxsize,
                    wysize,
                    band_list=list(range(1, self.dataBandsCount + 1)),
                    )
                alpha = self.alphaband.ReadRaster(
                    rx,
                    ry,
                    rxsize,
                    rysize,
                    wxsize,
                    wysize,
                    )

                if self.tilesize == querysize:

                    # Use the ReadRaster result directly in tiles ('nearest neighbour' query)

                    dstile.WriteRaster(
                        wx,
                        wy,
                        wxsize,
                        wysize,
                        data,
                        band_list=list(range(1, self.dataBandsCount
                                + 1)),
                        )
                    dstile.WriteRaster(
                        wx,
                        wy,
                        wxsize,
                        wysize,
                        alpha,
                        band_list=[tilebands],
                        )
                else:

                    # Note: For source drivers based on WaveLet compression (JPEG2000, ECW, MrSID)
                    # the ReadRaster function returns high-quality raster (not ugly nearest neighbour)
                    # TODO: Use directly 'near' for WaveLet files
                    # Big ReadRaster query in memory scaled to the tilesize - all but 'near' algo

                    dsquery = self.mem_drv.Create('', querysize,
                            querysize, tilebands)

                    # TODO: fill the null value in case a tile without alpha is produced (now only png tiles are supported)
                    # for i in range(1, tilebands+1):
                    #   dsquery.GetRasterBand(1).Fill(tilenodata)

                    dsquery.WriteRaster(
                        wx,
                        wy,
                        wxsize,
                        wysize,
                        data,
                        band_list=list(range(1, self.dataBandsCount
                                + 1)),
                        )
                    dsquery.WriteRaster(
                        wx,
                        wy,
                        wxsize,
                        wysize,
                        alpha,
                        band_list=[tilebands],
                        )

                    self.scale_query_to_tile(dsquery, dstile,
                            tilefilename)
                    del dsquery

                del data

                if self.options.resampling != 'antialias':

                    # Write a copy of tile to png/jpg

                    self.out_drv.CreateCopy(tilefilename, dstile,
                            strict=0)

                del dstile

                # Create a KML file for this tile.

                if self.kml:
                    kmlfilename = os.path.join(self.output, str(tz),
                            str(tx), '%d.kml' % ty)
                    if not self.options.resume \
                        or not os.path.exists(kmlfilename):
                        f = open(kmlfilename, 'w')
                        f.write(self.generate_kml(tx, ty, tz))
                        f.close()

                if not self.options.verbose:
                    self.progressbar(ti / float(tcount))
def generate_base_tiles ( self ) : print ( 'Generating Base Tiles:' ) if self . options . verbose : # mx, my = self.out_gt[0], self.out_gt[3] # OriginX, OriginY # px, py = self.mercator.MetersToPixels( mx, my, self.tmaxz) # print("Pixel coordinates:", px, py, (mx, my)) print ( '' ) print ( 'Tiles generated from the max zoom level:' ) print ( '----------------------------------------' ) print ( '' ) # Set the bounds ( tminx , tminy , tmaxx , tmaxy ) = self . tminmax [ self . tmaxz ] # Just the center tile # tminx = tminx+ (tmaxx - tminx)/2 # tminy = tminy+ (tmaxy - tminy)/2 # tmaxx = tminx # tmaxy = tminy ds = self . out_ds tilebands = self . dataBandsCount + 1 querysize = self . querysize if self . options . verbose : print ( 'dataBandsCount: ' , self . dataBandsCount ) print ( 'tilebands: ' , tilebands ) # print(tminx, tminy, tmaxx, tmaxy) tcount = ( 1 + abs ( tmaxx - tminx ) ) * ( 1 + abs ( tmaxy - tminy ) ) # print(tcount) ti = 0 tz = self . tmaxz yrange = range ( tmaxy , tminy - 1 , - 1 ) if self . options . leaflet : yrange = range ( tminy , tmaxy + 1 ) for ty in yrange : for tx in range ( tminx , tmaxx + 1 ) : if self . stopped : break ti += 1 tilefilename = os . path . join ( self . output , str ( tz ) , str ( tx ) , '%s.%s' % ( ty , self . tileext ) ) if self . options . verbose : print ( ti , '/' , tcount , tilefilename ) # , "( TileMapService: z / x / y )" if self . options . resume and os . path . exists ( tilefilename ) : if self . options . verbose : print ( 'Tile generation skiped because of --resume' ) else : self . progressbar ( ti / float ( tcount ) ) continue # Create directories for the tile if not os . path . exists ( os . path . dirname ( tilefilename ) ) : os . makedirs ( os . path . dirname ( tilefilename ) ) if self . options . profile == 'mercator' : # Tile bounds in EPSG:900913 b = self . mercator . TileBounds ( tx , ty , tz ) elif self . options . profile == 'geodetic' : b = self . geodetic . TileBounds ( tx , ty , tz ) # print("\tgdalwarp -ts 256 256 -te %s %s %s %s %s %s_%s_%s.tif" % ( b[0], b[1], b[2], b[3], "tiles.vrt", tz, tx, ty)) # Don't scale up by nearest neighbour, better change the querysize # to the native resolution (and return smaller query tile) for scaling if self . options . profile in ( 'mercator' , 'geodetic' ) : ( rb , wb ) = self . geo_query ( ds , b [ 0 ] , b [ 3 ] , b [ 2 ] , b [ 1 ] ) nativesize = wb [ 0 ] + wb [ 2 ] # Pixel size in the raster covering query geo extent if self . options . verbose : print ( '\tNative Extent (querysize' , nativesize , '): ' , rb , wb ) # Tile bounds in raster coordinates for ReadRaster query ( rb , wb ) = self . geo_query ( ds , b [ 0 ] , b [ 3 ] , b [ 2 ] , b [ 1 ] , querysize = querysize , ) ( rx , ry , rxsize , rysize ) = rb ( wx , wy , wxsize , wysize ) = wb else : # 'raster' profile: tsize = int ( self . tsize [ tz ] ) # tilesize in raster coordinates for actual zoom xsize = self . out_ds . RasterXSize # size of the raster in pixels ysize = self . out_ds . RasterYSize if tz >= self . nativezoom : querysize = self . tilesize # int(2**(self.nativezoom-tz) * self.tilesize) rx = tx * tsize rxsize = 0 if tx == tmaxx : rxsize = xsize % tsize if rxsize == 0 : rxsize = tsize rysize = 0 if ty == tmaxy : rysize = ysize % tsize if rysize == 0 : rysize = tsize if self . options . leaflet : ry = ty * tsize else : ry = ysize - ty * tsize - rysize ( wx , wy ) = ( 0 , 0 ) ( wxsize , wysize ) = ( int ( rxsize / float ( tsize ) * self . tilesize ) , int ( rysize / float ( tsize ) * self . tilesize ) ) if not self . options . leaflet : if wysize != self . tilesize : wy = self . tilesize - wysize if self . options . verbose : print ( '\tReadRaster Extent: ' , ( rx , ry , rxsize , rysize ) , ( wx , wy , wxsize , wysize ) ) # Query is in 'nearest neighbour' but can be bigger in then the tilesize # We scale down the query to the tilesize by supplied algorithm. # Tile dataset in memory dstile = self . mem_drv . Create ( '' , self . tilesize , self . tilesize , tilebands ) data = ds . ReadRaster ( rx , ry , rxsize , rysize , wxsize , wysize , band_list = list ( range ( 1 , self . dataBandsCount + 1 ) ) , ) alpha = self . alphaband . ReadRaster ( rx , ry , rxsize , rysize , wxsize , wysize , ) if self . tilesize == querysize : # Use the ReadRaster result directly in tiles ('nearest neighbour' query) dstile . WriteRaster ( wx , wy , wxsize , wysize , data , band_list = list ( range ( 1 , self . dataBandsCount + 1 ) ) , ) dstile . WriteRaster ( wx , wy , wxsize , wysize , alpha , band_list = [ tilebands ] , ) else : # Note: For source drivers based on WaveLet compression (JPEG2000, ECW, MrSID) # the ReadRaster function returns high-quality raster (not ugly nearest neighbour) # TODO: Use directly 'near' for WaveLet files # Big ReadRaster query in memory scaled to the tilesize - all but 'near' algo dsquery = self . mem_drv . Create ( '' , querysize , querysize , tilebands ) # TODO: fill the null value in case a tile without alpha is produced (now only png tiles are supported) # for i in range(1, tilebands+1): #   dsquery.GetRasterBand(1).Fill(tilenodata) dsquery . WriteRaster ( wx , wy , wxsize , wysize , data , band_list = list ( range ( 1 , self . dataBandsCount + 1 ) ) , ) dsquery . WriteRaster ( wx , wy , wxsize , wysize , alpha , band_list = [ tilebands ] , ) self . scale_query_to_tile ( dsquery , dstile , tilefilename ) del dsquery del data if self . options . resampling != 'antialias' : # Write a copy of tile to png/jpg self . out_drv . CreateCopy ( tilefilename , dstile , strict = 0 ) del dstile # Create a KML file for this tile. if self . kml : kmlfilename = os . path . join ( self . output , str ( tz ) , str ( tx ) , '%d.kml' % ty ) if not self . options . resume or not os . path . exists ( kmlfilename ) : f = open ( kmlfilename , 'w' ) f . write ( self . generate_kml ( tx , ty , tz ) ) f . close ( ) if not self . options . verbose : self . progressbar ( ti / float ( tcount ) )