Forum

Author Topic: Export Orthomosaic by shape extent with shape label in filename  (Read 167 times)

vbarty

  • Newbie
  • *
  • Posts: 7
    • View Profile
Export Orthomosaic by shape extent with shape label in filename
« on: February 23, 2021, 03:52:02 PM »
Hi,
I have an ortomosaic that I need to split by blocks and the in the filename of each exported block I need to have shape label.

I am trying to proceed with Python script, but I must say I am only a beginner in Python scripting, so maby I am doing something wrong.

I have rewieved some topics that already mention this problem but it did not help me.
One of the problems I have that wasn's menton i those topics is that I can't use the shape with blocks as Outer Boundary - I am already using differenet shape for that as I need two boundaries - it is a network project with buffer along the line, so I need to set the buffer as my Outer Boundary.

I was trying to go the following way - for each block in my shapes layer I try to export orthomosaic that would have the region set to the block's extent. But I must do somerhing wrog there, as each time whole orthomosaic is being exported, like the boundaries were not set at all. I tried setting the boundaries in different ways, but non of it worked.
At lest the file name is correct..

Below is part of the code that I use
Code: [Select]
import Metashape
#compression
compression = Metashape.ImageCompression()
compression.tiff_compression = Metashape.ImageCompression.TiffCompressionLZW
compression.jpeg_quality = 95
compression.tiff_big = False
compression.tiff_compression = True
compression.tiff_overviews = True

#export
chunk = Metashape.app.document.chunk
ortho = chunk.orthomosaic
ortho_projection = ortho.projection

tiles =[]
for shp in chunk.shapes:
if shp.selected == True:
tiles.append(shp)

for tile in tiles:
       t_min = tile.vertices[1]
       t_max = tile.vertices[3]
       tile_region = Metashape.BBox()
       tile_region.min = Metashape.Vector(t_min)
       tile_region.max = Metashape.Vector(t_max)
       tile_label = tile.label
       chunk.exportRaster(path = out_path + line_name + '_'+tile_label+'.tif', projection = ortho_projection, region = tile_region, resolution_x = pixel_size_x_m,  resolution_y = pixel_size_y_m, image_compression=compression, save_world = True, white_background = False,)

When I call tile_region i get Metashape.BBox at 0x288af34ea90 which does't tell me nothing about the actual boundaries.
I also tried to use something like
 
Code: [Select]
       tile_region = Metashape.BBox(tile.vertices)

which also returned some bounding box (different than in the code above), but didnt do the job.

Can anyone point at what am I doing wrong with my code?
It is very easy to set correct boundaries in orthomosaic export dialog box in the Setup Boundaries section, but then i dont get correct names for my files.
   

Edit:
Sorry i messed up the code when posting here, now it is like I tried it.


« Last Edit: February 23, 2021, 04:28:09 PM by vbarty »

vbarty

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Export Orthomosaic by shape extent with shape label in filename
« Reply #1 on: February 23, 2021, 05:39:04 PM »
Ok, now I tried the code with different project (with the same shape type for blocks) and the script seemed to work fine... The Python console showed generating multiple rasters with correct names, and size of one block, but... the output folder is empty. What happened here?

And still the same code does not work with the main project..   Python console says that one big raster is being exported (size way bigger than size of one block)

Paulo

  • Hero Member
  • *****
  • Posts: 525
    • View Profile
Re: Export Orthomosaic by shape extent with shape label in filename
« Reply #2 on: February 24, 2021, 04:28:45 AM »
Hi vbarty,

I would suggest to check the shapes.crs to see if is is same as ortho.crs. To avoid this problem of different CRSs, I would modify script as:
Code: [Select]
...
t_min = chunk.crs.transform(tile.vertices[1],chunk.shapes.crs,ortho.crs)[0:2]
t_max = chunk.crs.transform(tile.vertices[3],chunk.shapes.crs,ortho.crs)[0:2]
tile_region = Metashape.BBox()
tile_region.min = t_min
tile_region.max = t_max
...
and to get correct LZW tiff compression please remove line
Code: [Select]
compression.tiff_compression = True from compression definition section.
Hope this helps,
« Last Edit: February 24, 2021, 05:12:30 AM by Paulo »
Best Regards,

Paul Pelletier

vbarty

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Export Orthomosaic by shape extent with shape label in filename
« Reply #3 on: February 24, 2021, 10:38:17 AM »
Hi Paulo,

Thaks for reply, I used your code and seems to work really well, so thank you!
But I think it was my rookie mistake... I got the min max vertices wrong, it should be
   
Code: [Select]
t_min = tile.vertices[3]
t_max = tile.vertices[1]

With that even my code works.
I just noticed it now when checking your proposal, so you helped me a lot :)
Also thanks for the compression advice.