Package: rio-tiler-mosaic
rio-tiler-mosaicA rio-tiler plugin for creating tiles from multiple observations.Install$pipinstallrio-tiler-mosaicOr$gitclonehttp://github.com/cogeotiff/rio-tiler-mosaic
$cdrio-tiler-mosaic
$pipinstall-e.Rio-tiler + MosaicThe goal of this rio-tiler plugin is to create tiles from multiple observations.Because user might want to choose which pixel goes on top of the tile, this plugin comes with 5 differentspixel selectionalgorithms:First: takes the first pixel receivedHighest: loop though all the assets and return the highest valueLowest: loop though all the assets and return the lowest valueMean: compute the mean value of the whole stackMedian: compute the median value of the whole stackAPImosaic_tiler(assets, tile_x, tile_y, tile_z, tiler, pixel_selection=None, chunk_size=5, kwargs)Inputs:assets : list, tuple of rio-tiler compatible assets (url or sceneid)tile_x : Mercator tile X index.tile_y : Mercator tile Y index.tile_z : Mercator tile ZOOM level.tiler: Rio-tiler's tiler function (e.g rio_tiler.landsat8.tile)pixel_selection : optionalpixel selectionalgorithm (default: "first").chunk_size: optional, control the number of asset to process per loop.kwargs: Rio-tiler tiler module specific otions.Returns:tile, mask : tuple of ndarray Return tile and mask data.Examplesfromrio_tiler.ioimportCOGReaderfromrio_tiler_mosaic.mosaicimportmosaic_tilerfromrio_tiler_mosaic.methodsimportdefaultsdeftiler(src_path:str,*args,**kwargs)->Tuple[numpy.ndarray,numpy.ndarray]:withCOGReader(src_path)ascog:returncog.tile(*args,**kwargs)assets=["mytif1.tif","mytif2.tif","mytif3.tif"]tile=(1000,1000,9)x,y,z=tile# Use Default First value methodmosaic_tiler(assets,x,y,z,tiler)# Use Highest value: defaults.HighestMethod()mosaic_tiler(assets,x,y,z,tiler,pixel_selection=defaults.HighestMethod())# Use Lowest value: defaults.LowestMethod()mosaic_tiler(assets,x,y,z,tiler,pixel_selection=defaults.LowestMethod())TheMosaicMethodinterfacetherio-tiler-mosaic.methods.base.MosaicMethodBaseclass defines an abstract
interface for allpixel selectionmethods allowed byrio-tiler-mosaic. its methods and properties are:is_done: property, returns a boolean indicating if the process is done filling the tiledata: property, returns the outputtileandmasknumpy arraysfeed(tile: numpy.ma.ndarray): method, update the tileThe MosaicMethodBase class is not intended to be used directly but as an abstract base class, a template for concrete implementations.Writing your own Pixel Selection methodThe rules for writing your ownpixel selection algorithmclass are as follows:Must inherit from MosaicMethodBaseMust provide concrete implementations of all the above methods.Seerio_tiler_mosaic.methods.defaultsclasses for examples.Smart Multi-ThreadingWhen dealing with an important number of image, you might not want to process the whole stack, especially if the pixel selection method stops when the tile is filled. To allow better optimization,rio-tiler-mosaicis fetching the tiles in parallel (threads) but to limit the number of files we also embeded the fetching in a loop (creating 2 level of processing):assets=["1.tif","2.tif","3.tif","4.tif","5.tif","6.tif"]# 1st level loop - Creates chuncks of assetsforchunksin_chunks(assets,chunk_size):# 2nd level loop - Uses threads for process each `chunck`withfutures.ThreadPoolExecutor(max_workers=max_threads)asexecutor:future_tasks=[executor.submit(_tiler,asset)forassetinchunks]By default the chunck_size is equal to the number or threads (or the number of assets if no threads=0)More on threadingThe number of threads used can be set in the function call with thethreads=options. By default it will be equal tomultiprocessing.cpu_count() * 5or to the MAX_THREADS environment variable.
In some case, threading can slow down your application. You can set threads to0to run the tiler in a loop without using a ThreadPool.ExampleSee/exampleContribution & DevelopmentIssues and pull requests are more than welcome.dev install$gitclonehttps://github.com/cogeotiff/rio-tiler-mosaic.git
$cdrio-tiler-mosaic
$pipinstall-e.[dev]Python3.6 onlyThis repo is set to usepre-committo runflake8,pydocstringandblack("uncompromising Python code formatter") when commiting new code.$pre-commitinstallImplementationcogeo-mosaic