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:
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)