22
22
from starlette .types import ASGIApp
23
23
from starlette_cramjam .middleware import CompressionMiddleware
24
24
25
+ from rio_viz .algorithm import AVAILABLE_ALGORITHM , AlgorithmMetadata
26
+ from rio_viz .dependency import PostProcessParams
25
27
from rio_viz .resources .enums import RasterFormat , VectorTileFormat , VectorTileType
26
28
27
29
from titiler .core .dependencies import (
36
38
HistogramParams ,
37
39
ImageParams ,
38
40
ImageRenderingParams ,
39
- PostProcessParams ,
40
41
StatisticsParams ,
41
42
)
42
43
from titiler .core .models .mapbox import TileJSON
@@ -295,19 +296,26 @@ async def preview(
295
296
# Adapt options for each reader type
296
297
self ._update_params (src_dst , layer_params )
297
298
298
- data = await src_dst .preview (
299
+ img = await src_dst .preview (
299
300
** layer_params ,
300
301
** dataset_params ,
301
302
** img_params ,
302
303
)
303
304
dst_colormap = getattr (src_dst , "colormap" , None )
304
305
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 )
307
314
308
- image = data .post_process (** postprocess_params )
315
+ if not format :
316
+ format = RasterFormat .jpeg if img .mask .all () else RasterFormat .png
309
317
310
- content = image .render (
318
+ content = img .render (
311
319
img_format = format .driver ,
312
320
colormap = colormap or dst_colormap ,
313
321
** format .profile ,
@@ -360,17 +368,24 @@ async def part(
360
368
# Adapt options for each reader type
361
369
self ._update_params (src_dst , layer_params )
362
370
363
- data = await src_dst .part (
371
+ img = await src_dst .part (
364
372
[minx , miny , maxx , maxy ],
365
373
** layer_params ,
366
374
** dataset_params ,
367
375
** img_params ,
368
376
)
369
377
dst_colormap = getattr (src_dst , "colormap" , None )
370
378
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 )
372
387
373
- content = image .render (
388
+ content = img .render (
374
389
img_format = format .driver ,
375
390
colormap = colormap or dst_colormap ,
376
391
** format .profile ,
@@ -415,17 +430,24 @@ async def geojson_part(
415
430
# Adapt options for each reader type
416
431
self ._update_params (src_dst , layer_params )
417
432
418
- data = await src_dst .feature (
433
+ img = await src_dst .feature (
419
434
geom .dict (exclude_none = True ), ** layer_params , ** dataset_params
420
435
)
421
436
dst_colormap = getattr (src_dst , "colormap" , None )
422
437
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 )
425
443
426
- image = data .post_process (** postprocess_params )
444
+ if postprocess_params .color_formula :
445
+ img .apply_color_formula (postprocess_params .color_formula )
427
446
428
- content = image .render (
447
+ if not format :
448
+ format = RasterFormat .jpeg if img .mask .all () else RasterFormat .png
449
+
450
+ content = img .render (
429
451
img_format = format .driver ,
430
452
colormap = colormap or dst_colormap ,
431
453
** format .profile ,
@@ -475,7 +497,7 @@ async def tile(
475
497
# Adapt options for each reader type
476
498
self ._update_params (src_dst , layer_params )
477
499
478
- tile_data = await src_dst .tile (
500
+ img = await src_dst .tile (
479
501
x ,
480
502
y ,
481
503
z ,
@@ -502,22 +524,27 @@ async def tile(
502
524
_mvt_encoder = partial (run_in_threadpool , pixels_encoder )
503
525
504
526
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 ,
508
530
feature_type = feature_type .value ,
509
531
) # type: ignore
510
532
511
533
# Raster Tile
512
534
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 )
517
543
518
- image = tile_data .post_process (** postprocess_params )
544
+ if not format :
545
+ format = RasterFormat .jpeg if img .mask .all () else RasterFormat .png
519
546
520
- content = image .render (
547
+ content = img .render (
521
548
img_format = format .driver ,
522
549
colormap = colormap or dst_colormap ,
523
550
** format .profile ,
@@ -646,6 +673,35 @@ async def wmts(
646
673
media_type = "application/xml" ,
647
674
)
648
675
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
+
649
705
@self .router .get (
650
706
"/" ,
651
707
responses = {200 : {"description" : "Simple COG viewer." }},
0 commit comments