Forum

Author Topic: Crop DEM using polygon from external file  (Read 1899 times)

Benjamim

  • Newbie
  • *
  • Posts: 7
    • View Profile
Crop DEM using polygon from external file
« on: May 21, 2024, 08:44:58 PM »
Hello everyone,

I'm currently using Metashape Professional 2.1.1 with Python 2.12 to automate some tasks.

In my script, I load laser scan data and generate various DEMs. I want to crop and export my outputs (DEMs, rasters) using a polygon from a GeoJSON file. However, I haven't been able to find a way to achieve this from the documentation.

Can anyone point me in the right direction? While I don't expect Metashape to directly support GeoJSON, I believe there should be a way to crop rasters using polygons through the API. Any guidance or suggestions would be greatly appreciated!
Best regards,
Benjamim Oliveira

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15067
    • View Profile
Re: Crop DEM using polygon from external file
« Reply #1 on: June 07, 2024, 01:18:30 PM »
Hello Benjamim,

If you need to crop the DEM on export only, then you need to assign the boundary type to the imported shape:
Code: [Select]
shape.boundary_type = Metashape.Shape.BoundaryType.OuterBoundarythen in the exportRaster command enable clipping to boundary shapes:
Code: [Select]
chunk.exportRaster(clip_to_boundary=True) #other parameters omitted in the example
Best regards,
Alexey Pasumansky,
Agisoft LLC

Benjamim

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crop DEM using polygon from external file
« Reply #2 on: June 11, 2024, 01:16:43 PM »
Hi Alexey,

I tested it, and it works perfectly. Thank you for your help!

To add to the discussion and assist others who might read this in the future, there's a method called "importShapes" that allows you to load shapes from an external file in a very straightforward way, allowing even to directly set the shape as an outer boundary:
Code: [Select]
chunk.importShapes(
    path="/path/to/file.geojson",
    boundary_type= Metashape.Shape.BoundaryType.OuterBoundary,
    format=Metashape.ShapesFormat.ShapesFormatGeoJSON)

Thanks again!
Best regards,
Benjamim Oliveira