Forum

Author Topic: script inserting points with Z label  (Read 1688 times)

geodezja3D

  • Newbie
  • *
  • Posts: 7
    • View Profile
script inserting points with Z label
« on: June 17, 2024, 01:25:51 PM »
Hi,

New to scripting so I need some help.
I would like the script to insert grid of points with Z coordinate as labels. I generated code as below but it does not work.

import Metashape
import math

doc = Metashape.app.document
chunk = doc.chunk

if not chunk:
    raise Exception("No chunk loaded")

grid_step = 1.0
region = chunk.region
bbox = region.bounding_box
min_point = bbox.min
max_point = bbox.max

x_steps = math.ceil((max_point.x - min_point.x) / grid_step)
y_steps = math.ceil((max_point.y - min_point.y) / grid_step)

points = []
for i in range(x_steps + 1):
    for j in range(y_steps + 1):
        x = min_point.x + i * grid_step
        y = min_point.y + j * grid_step
        # Szukamy wysokości dla danej współrzędnej (x, y) na modelu 3D
        ray_origin = Metashape.Vector([x, y, max_point.z])
        ray_direction = Metashape.Vector([0, 0, -1])
        point = chunk.model.pickPoint(ray_origin, ray_direction)
        if point is not None:
            point_coord = point.coord
            height = point_coord.z
            points.append((Metashape.Vector([x, y, height]), str(height)))

for point, name in points:
    marker = chunk.addMarker(point)
    marker.label = name

Could you correct this?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15029
    • View Profile
Re: script inserting points with Z label
« Reply #1 on: June 20, 2024, 05:30:42 PM »
Hello geodezja3D,

Do you want to create markers or point shapes?
Best regards,
Alexey Pasumansky,
Agisoft LLC

geodezja3D

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: script inserting points with Z label
« Reply #2 on: June 21, 2024, 03:23:21 PM »
Hello! I would like to create point shapes.

Hello geodezja3D,

Do you want to create markers or point shapes?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15029
    • View Profile
Re: script inserting points with Z label
« Reply #3 on: June 24, 2024, 01:05:31 PM »
Hello geodezja3D,

Please see the example below that creates a grid of point shapes with elevation calculated based on DEM altitude in corresponding XY location:

Code: [Select]
import Metashape

def convert_meters_to_deg(value, chunk):
crs = chunk.crs
if chunk.crs.authority != 'EPSG::4326':
return Metashape.Vector([value, value])

T = chunk.transform.matrix
origin = chunk.region.center
origin_geoc = T.mulp(origin)
origin_geog = crs.project(origin_geoc)

#longitude
v_x = (crs.unproject(origin_geog + Metashape.Vector([1E-5,0,0])) - origin_geoc).norm() * 1E5
#latitude
v_y = (crs.unproject(origin_geog + Metashape.Vector([0,1E-5,0])) - origin_geoc).norm() * 1E5
return Metashape.Vector([value / v_x, value / v_y])

def create_grid():

doc = Metashape.app.document
chunk = doc.chunk
if not chunk:
print("Empty project, script aborted")
return 0
if not chunk.elevation:
print("Missing DEM in the active chunk, script aborted")
return 0

STEP = Metashape.app.getFloat("Please input the grid step (in meters) for point shapes:", 10)
if STEP <= 0:
print("Wrong number! Step value should be positive. Script aborted.")
return 0

DEM = chunk.elevation
if not chunk.shapes:
chunk.shapes = Metashape.Shapes()
chunk.shapes.crs = elevation.crs
shapes = chunk.shapes

limits = [[DEM.left, DEM.right], [DEM.bottom, DEM.top]]
points = chunk.shapes.addGroup()
points.label = "Automatic point shapes (step {:.2f} meters)".format(STEP)
step = convert_meters_to_deg(STEP, chunk)
x = limits[0][0]
y = limits[1][0]
while x < limits[0][1]:
y = limits[1][0]
while y < limits[1][1]:
altitude = DEM.altitude(Metashape.Vector([x, y]))
if altitude == -32767:
y += step.y
continue #skipping point
point = chunk.shapes.addShape()
point.group = points
point.geometry = Metashape.Geometry.Point(Metashape.Vector([x,y,altitude]))
point.label = "{:.3f}".format(altitude)
y += step.y
x += step.x
return 1

label = "Custom Menu/Create grid of points from DEM"
print("To execute this script press {}".format(label))
Metashape.app.addMenuItem(label, create_grid)
Best regards,
Alexey Pasumansky,
Agisoft LLC

geodezja3D

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: script inserting points with Z label
« Reply #4 on: June 26, 2024, 04:14:52 PM »
Works great. Thank you!