I wrote the below code to build a 25cm ortho (rather than full-res) with integer cell bounds, and it works great if I copy/paste into the console, but if I run it from the <>run script command and send to network processing, or if I launch it as a batch/run script (and send the batch to network processing) it fails to generate any network tasks (only shows as "running script" and the node reports "no network tasks to do!" from my else statement)
- note that I've only tested this on my local workstation configured as host/monitor/node/client, but I haven't seen this behavior on other scripts I tested.
import Metashape
import math
import os
from os import path
#-------------------------------------------------------#
#define user-set variables
raster_rounding_multiple = 40 # Default = 40 - This will be the multiple that the raster resolution is multiplied by to define the units the min/max extents are rounded to (DEM is 10 so this keeps unit rounding consistent)
raster_resolution = 0.25 # Default = 0.25 - cell size of exported ortho
raster_crop = True # Default = True - True means Bounding Box in rounded IN - minimum extent is rounded up and maximum extent is rounded down from raster edges. False is reversed
# TODO - make it so metashape checks to see if this is an interpolated raster (shrink) or uninterpolated (grow?)
# ALSO - maybe we want to project the xy coordinates of the 3D dense cloud region and use those instead? this will result in no/minimal collar though...
ortho_subdir = 'ortho' # this is a subdir that will be created under the document (PSX) path
ortho_suffix = '_NAD83_2011_UTM18' #suffix to append to ortho (future - modify this to grab from WKT)
raster_rounding_interval = raster_rounding_multiple * raster_resolution
app = Metashape.app
doc = app.document
network_tasks = list()
for chunk in doc.chunks:
if chunk.elevation:
print(chunk.label)
out_projection = chunk.elevation.projection
compression = Metashape.ImageCompression()
compression.tiff_compression = Metashape.ImageCompression.TiffCompressionLZW
compression.tiff_big = True
compression.tiff_overviews = True
compression.tiff_tiled = True
def round_down(x):
return int(raster_rounding_interval * math.floor(float(x)/raster_rounding_interval))
def round_up(x):
return int(raster_rounding_interval * math.ceil(float(x)/raster_rounding_interval))
#chunk.elevation.crs(wkt) #returns CRS for DEM
testbox = Metashape.BBox() #create a bounding box for the raster
print('')
print('original DEM BBox coordinates:')
print('min: ', Metashape.Vector((min(chunk.elevation.left, chunk.elevation.right), min(chunk.elevation.bottom, chunk.elevation.top))))
print('max: ', Metashape.Vector((max(chunk.elevation.left, chunk.elevation.right), max(chunk.elevation.bottom, chunk.elevation.top))))
if raster_crop:
testbox.min = Metashape.Vector((round_up(min(chunk.elevation.left, chunk.elevation.right)), round_up(min(chunk.elevation.bottom, chunk.elevation.top))))
testbox.max = Metashape.Vector((round_down(max(chunk.elevation.left, chunk.elevation.right)), round_down(max(chunk.elevation.bottom, chunk.elevation.top))))
else:
testbox.min = Metashape.Vector((round_down(min(chunk.elevation.left, chunk.elevation.right)), round_down(min(chunk.elevation.bottom, chunk.elevation.top))))
testbox.max = Metashape.Vector((round_up(max(chunk.elevation.left, chunk.elevation.right)), round_up(max(chunk.elevation.bottom, chunk.elevation.top))))
if raster_crop:
print('extent was SHRUNK to: ')
print('min: ',testbox.min)
print('max: ',testbox.max)
else:
print('extent was GROWN to: ')
print('min: ',testbox.min)
print('max: ',testbox.max)
print('building ortho in network mode')
task = Metashape.Tasks.BuildOrthomosaic()
task.blending_mode = Metashape.BlendingMode.AverageBlending
task.cull_faces = False
task.fill_holes = True
task.projection = out_projection
task.region = testbox
task.resolution = raster_resolution
task.resolution_x = raster_resolution
task.resolution_y = raster_resolution
task.refine_seamlines = False
task.subdivide_task = True
task.surface_data = Metashape.DataSource.ElevationData
n_task = Metashape.NetworkTask()
n_task.name = task.name
n_task.params = task.encode()
n_task.frames.append((chunk.key, 0))
network_tasks.append(n_task)
if network_tasks:
print('sending ', len(network_tasks), 'for processing')
client = Metashape.NetworkClient()
client.connect(app.settings.network_host) #server ip
batch_id = client.createBatch(doc.path, network_tasks)
client.resumeBatch(batch_id)
else:
print('no network tasks to do!')
print('script complete')