2222from starlette .types import ASGIApp
2323from starlette_cramjam .middleware import CompressionMiddleware
2424
25+ from rio_viz .algorithm import AVAILABLE_ALGORITHM , AlgorithmMetadata
26+ from rio_viz .dependency import PostProcessParams
2527from rio_viz .resources .enums import RasterFormat , VectorTileFormat , VectorTileType
2628
2729from titiler .core .dependencies import (
3638 HistogramParams ,
3739 ImageParams ,
3840 ImageRenderingParams ,
39- PostProcessParams ,
4041 StatisticsParams ,
4142)
4243from titiler .core .models .mapbox import TileJSON
@@ -295,19 +296,26 @@ async def preview(
295296 # Adapt options for each reader type
296297 self ._update_params (src_dst , layer_params )
297298
298- data = await src_dst .preview (
299+ img = await src_dst .preview (
299300 ** layer_params ,
300301 ** dataset_params ,
301302 ** img_params ,
302303 )
303304 dst_colormap = getattr (src_dst , "colormap" , None )
304305
305- if not format :
306- format = RasterFormat .jpeg if data .mask .all () else RasterFormat .png
306+ if postprocess_params .image_process :
307+ img = postprocess_params .image_process .apply (img )
308+
309+ if postprocess_params .rescale :
310+ img .rescale (postprocess_params .rescale )
311+
312+ if postprocess_params .color_formula :
313+ img .apply_color_formula (postprocess_params .color_formula )
307314
308- image = data .post_process (** postprocess_params )
315+ if not format :
316+ format = RasterFormat .jpeg if img .mask .all () else RasterFormat .png
309317
310- content = image .render (
318+ content = img .render (
311319 img_format = format .driver ,
312320 colormap = colormap or dst_colormap ,
313321 ** format .profile ,
@@ -360,17 +368,24 @@ async def part(
360368 # Adapt options for each reader type
361369 self ._update_params (src_dst , layer_params )
362370
363- data = await src_dst .part (
371+ img = await src_dst .part (
364372 [minx , miny , maxx , maxy ],
365373 ** layer_params ,
366374 ** dataset_params ,
367375 ** img_params ,
368376 )
369377 dst_colormap = getattr (src_dst , "colormap" , None )
370378
371- image = data .post_process (** postprocess_params )
379+ if postprocess_params .image_process :
380+ img = postprocess_params .image_process .apply (img )
381+
382+ if postprocess_params .rescale :
383+ img .rescale (postprocess_params .rescale )
384+
385+ if postprocess_params .color_formula :
386+ img .apply_color_formula (postprocess_params .color_formula )
372387
373- content = image .render (
388+ content = img .render (
374389 img_format = format .driver ,
375390 colormap = colormap or dst_colormap ,
376391 ** format .profile ,
@@ -415,17 +430,24 @@ async def geojson_part(
415430 # Adapt options for each reader type
416431 self ._update_params (src_dst , layer_params )
417432
418- data = await src_dst .feature (
433+ img = await src_dst .feature (
419434 geom .dict (exclude_none = True ), ** layer_params , ** dataset_params
420435 )
421436 dst_colormap = getattr (src_dst , "colormap" , None )
422437
423- if not format :
424- format = RasterFormat .jpeg if data .mask .all () else RasterFormat .png
438+ if postprocess_params .image_process :
439+ img = postprocess_params .image_process .apply (img )
440+
441+ if postprocess_params .rescale :
442+ img .rescale (postprocess_params .rescale )
425443
426- image = data .post_process (** postprocess_params )
444+ if postprocess_params .color_formula :
445+ img .apply_color_formula (postprocess_params .color_formula )
427446
428- content = image .render (
447+ if not format :
448+ format = RasterFormat .jpeg if img .mask .all () else RasterFormat .png
449+
450+ content = img .render (
429451 img_format = format .driver ,
430452 colormap = colormap or dst_colormap ,
431453 ** format .profile ,
@@ -475,7 +497,7 @@ async def tile(
475497 # Adapt options for each reader type
476498 self ._update_params (src_dst , layer_params )
477499
478- tile_data = await src_dst .tile (
500+ img = await src_dst .tile (
479501 x ,
480502 y ,
481503 z ,
@@ -502,22 +524,27 @@ async def tile(
502524 _mvt_encoder = partial (run_in_threadpool , pixels_encoder )
503525
504526 content = await _mvt_encoder (
505- tile_data .data ,
506- tile_data .mask ,
507- tile_data .band_names ,
527+ img .data ,
528+ img .mask ,
529+ img .band_names ,
508530 feature_type = feature_type .value ,
509531 ) # type: ignore
510532
511533 # Raster Tile
512534 else :
513- if not format :
514- format = (
515- RasterFormat .jpeg if tile_data .mask .all () else RasterFormat .png
516- )
535+ if postprocess_params .image_process :
536+ img = postprocess_params .image_process .apply (img )
537+
538+ if postprocess_params .rescale :
539+ img .rescale (postprocess_params .rescale )
540+
541+ if postprocess_params .color_formula :
542+ img .apply_color_formula (postprocess_params .color_formula )
517543
518- image = tile_data .post_process (** postprocess_params )
544+ if not format :
545+ format = RasterFormat .jpeg if img .mask .all () else RasterFormat .png
519546
520- content = image .render (
547+ content = img .render (
521548 img_format = format .driver ,
522549 colormap = colormap or dst_colormap ,
523550 ** format .profile ,
@@ -646,6 +673,35 @@ async def wmts(
646673 media_type = "application/xml" ,
647674 )
648675
676+ @self .router .get (
677+ "/algorithm" ,
678+ response_model = List [AlgorithmMetadata ],
679+ )
680+ def algo (request : Request ):
681+ """Handle /algorithm."""
682+ algos = []
683+ for k , v in AVAILABLE_ALGORITHM .items ():
684+ props = v .schema ()["properties" ]
685+ ins = {
686+ k .replace ("input_" , "" ): v
687+ for k , v in props .items ()
688+ if k .startswith ("input_" )
689+ }
690+ outs = {
691+ k .replace ("output_" , "" ): v
692+ for k , v in props .items ()
693+ if k .startswith ("output_" )
694+ }
695+ params = {
696+ k : v
697+ for k , v in props .items ()
698+ if not k .startswith ("input_" ) and not k .startswith ("output_" )
699+ }
700+ algos .append (
701+ AlgorithmMetadata (name = k , inputs = ins , outputs = outs , params = params )
702+ )
703+ return algos
704+
649705 @self .router .get (
650706 "/" ,
651707 responses = {200 : {"description" : "Simple COG viewer." }},
0 commit comments