# Treetop location in 3D: How can I find the coordinates (x,y,z) of point cloud's local maxima within polygon vectors?

I'm doing forestry analysis of some point cloud data and I have used segmentation procedures (i.e. watershed algorithm, etc..) to delineate the crown boundaries of individual trees in 2D.

The vector file that represents the tree crown boundaries has a 'Tree ID' field with unique values for each tree.

I want to use the original point cloud and the vectors as inputs and find the coordinates (x,y,z) of the local maxima (i.e. the tree tops) for each tree.

I'm aware of the option to rasterize the point cloud and compute the maxima in 2D using traditional zonal statistics, but want to test the departure between the points as located this way versus locating them directly from the cloud within the polygons.

Solutions in R, QGIS or Python would be preferred, though I also have access to ArcGIS, Matlab, and ENVI.

I found a solution using FUSION and PDAL.

First, I used @andre silva's answer from this post to clip my point clouds using the shapefiles so that I created new point clouds for each of my trees and stored them together in a new folder. Clipping LAS data using shapefile polygons and open source software?

The code in FUSION looks like this, where the flag "shape" coerces the names of the outputs to include my "tree ID" variable:

polyclipdata /multifile /shape:1,* path\to\shapefile.shp path\to\clipped_outputs.las path\to\las_file.las


Next, I created a PDAL pipeline to read in all of the .las files individually and find the points with maximum "Z" values for each. The files are merged and output as both a .las point cloud and an ESRI shapefile with the treetop points.

{
"pipeline":[
{
"filename":"*.las"
},
{
"type":"filters.locate",
"dimension":"Z",
"minmax":"max"
},
{
"type":"filters.merge"
},
{
"type" : "writers.las",
"forward": "all",
"filename" : "crown_maxima.las"
},
{
"type":"writers.ogr",
"filename":"crown_maxima.shp",
"measure_dim":"Z"
}
]
}


This took me way longer than it should have so I hope it will save someone else some time!