Forum

Author Topic: method to crop dense cloud according to coordinates  (Read 5720 times)

joshua19881228

  • Newbie
  • *
  • Posts: 9
    • View Profile
method to crop dense cloud according to coordinates
« on: August 23, 2022, 03:52:11 PM »
Hello,

Is there any method to crop dense cloud according to geo-coordinates using python script?

best regards,
Joshua

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15438
    • View Profile
Re: method to crop dense cloud according to coordinates
« Reply #1 on: August 23, 2022, 05:47:55 PM »
Hello Joshua,

You can create a polygonal shape, set its type to OuterBoundary and then duplicate the dense cloud using clip to boundary shapes option.

Please try to follow this approach using GUI - just draw a shape in Model view. If it works similar to the expected behavior, I can assist you with the Python script, just specify, how the the boundary would be defined (as a text file with XYZ coordinates, for example?).
Best regards,
Alexey Pasumansky,
Agisoft LLC

joshua19881228

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: method to crop dense cloud according to coordinates
« Reply #2 on: August 23, 2022, 08:11:27 PM »
Hello,

Thank you, Alexey. It's exactly what I need. The boundary would be defined as an array with XYZ coordinates, which is in UTM (easting, northing) system

best regards,
Joshua

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15438
    • View Profile
Re: method to crop dense cloud according to coordinates
« Reply #3 on: August 24, 2022, 03:38:27 PM »
Hello Joshua,

Please check the following script:
Code: [Select]
import Metashape

def crop_dense_cloud_by_shape():

doc = Metashape.app.document
chunk = doc.chunk
if not chunk:
print("Empty project, script aborted")
return 0

path = Metashape.app.getOpenFileName("Specify the input file with shape coordinates:", filter= "*.txt")
if not path:
print("File not selected, script aborted.")
return 0
file = open(path, "rt")
lines = file.readlines()
if len(lines) < 3:
print("Unable to create polygonal shape, script aborted")
file.close()
return 0
coords = []
for line in lines:
if line.strip().startswith("#"):
continue #skipping commented lines
if len(line.strip()) < 5:
continue #too short line
try:
x, y, z = line.strip().split(" ",2)
x = float(x)
y = float(y)
z = float(z)
except:
print("Something wrong while reading file, line:\n" + line + "\nScript aborted")
file.close()
return 0
coords.append(Metashape.Vector([x, y, z]))
file.close()
if len(coords) < 3:
print("Unable to create polygonal shape, script aborted")
return 0


if not chunk.shapes:
chunk.shapes = Metashape.Shapes()
if chunk.dense_cloud.crs:
chunk.shapes.crs = chunk.dense_cloud.crs
else:
chunk.shapes.crs = chunk.crs
shapes = chunk.shapes
crs = shapes.crs
for shape in chunk.shapes:
shape.boundary_type = Metashape.Shape.BoundaryType.NoBoundary

layer = chunk.shapes.addGroup()
layer.label = "Cropping boundary"
shape = shapes.addShape()
shape.label = "Cropping boundary"
shape.group = layer
shape.geometry.type = Metashape.Geometry.Type.PolygonType
shape.geometry = Metashape.Geometry.Polygon(coords)
shape.boundary_type = Metashape.Shape.BoundaryType.OuterBoundary

print("Shape created.")

task = Metashape.Tasks.DuplicateAsset()
task.asset_key = chunk.dense_cloud.key
task.asset_type = Metashape.DataSource.DenseCloudData
task.clip_to_boundary = True
task.apply(chunk)

chunk.dense_cloud.label = "Cropped dense cloud"

print("Script finished")
return 1

Metashape.app.addMenuItem("Custom menu/Crop dense cloud by shape", crop_dense_cloud_by_shape)

on start you'll be asked to specify the path to the txt file with the boundary coordinates. It is expected that each line contains X, Y and Z values (Easting, Northing, Altitude or Longitude, Latitude, Altitude) separated with "space". If any modification to the input format is required, let me know.
Best regards,
Alexey Pasumansky,
Agisoft LLC

joshua19881228

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: method to crop dense cloud according to coordinates
« Reply #4 on: August 26, 2022, 02:44:00 PM »
Hello Alexey,

Thank you very much. The code works perfectly.

Best Regards,
Joshua