Generate a thumbnail on the fly for a particular task
(self, request, pk=None, project_pk=None)
| 549 | |
| 550 | class TaskThumbnail(TaskNestedView): |
| 551 | def get(self, request, pk=None, project_pk=None): |
| 552 | """ |
| 553 | Generate a thumbnail on the fly for a particular task |
| 554 | """ |
| 555 | task = self.get_and_check_task(request, pk) |
| 556 | orthophoto_path = task.get_check_file_asset_path("orthophoto.tif") |
| 557 | if orthophoto_path is None: |
| 558 | raise exceptions.NotFound() |
| 559 | |
| 560 | thumb_size = 256 |
| 561 | try: |
| 562 | thumb_size = max(1, min(1024, int(request.query_params.get('size', 256)))) |
| 563 | except ValueError: |
| 564 | pass |
| 565 | |
| 566 | with rasterio.open(orthophoto_path, "r") as raster: |
| 567 | ci = raster.colorinterp |
| 568 | indexes = (1, 2, 3,) |
| 569 | |
| 570 | # More than 4 bands? |
| 571 | if len(ci) > 4: |
| 572 | # Try to find RGBA band order |
| 573 | if ColorInterp.red in ci and \ |
| 574 | ColorInterp.green in ci and \ |
| 575 | ColorInterp.blue in ci: |
| 576 | indexes = (ci.index(ColorInterp.red) + 1, |
| 577 | ci.index(ColorInterp.green) + 1, |
| 578 | ci.index(ColorInterp.blue) + 1,) |
| 579 | elif len(ci) < 3: |
| 580 | raise exceptions.NotFound() |
| 581 | |
| 582 | if ColorInterp.alpha in ci: |
| 583 | indexes += (ci.index(ColorInterp.alpha) + 1, ) |
| 584 | |
| 585 | if task.crop is not None: |
| 586 | cutline, (minx, miny, maxx, maxy) = geom_transform_wkt_bbox(task.crop, raster, 'raster') |
| 587 | |
| 588 | w = maxx - minx |
| 589 | h = maxy - miny |
| 590 | win = rasterio.windows.Window(minx, miny, w, h) |
| 591 | ratio = w / h |
| 592 | if ratio > 1: |
| 593 | out_width = thumb_size |
| 594 | out_height = int(thumb_size / ratio) |
| 595 | else: |
| 596 | out_height = thumb_size |
| 597 | out_width = int(thumb_size * ratio) |
| 598 | |
| 599 | |
| 600 | with WarpedVRT(raster, cutline=cutline, nodata=0) as vrt: |
| 601 | rgb = vrt.read(indexes=indexes, window=win, fill_value=0, out_shape=( |
| 602 | len(indexes), |
| 603 | out_height, |
| 604 | out_width, |
| 605 | ), resampling=rasterio.enums.Resampling.nearest) |
| 606 | img = np.zeros((len(indexes), thumb_size, thumb_size), dtype=rgb.dtype) |
| 607 | y_offset = (thumb_size - out_height) // 2 |
| 608 | x_offset = (thumb_size - out_width) // 2 |
nothing calls this directly
no test coverage detected