LiDAR Data Export – How to Export LiDAR Data with TreeID to CSV

csvlidarlidrrrlidar

I have used the lidR package in R to run a point cloud segmentation of trees using the li2012 algorithm. I am interested in exporting my results to a csv file, but the methods I have tried did not include the treeID column.

As suggested in this post: (Exporting coordinates from LAS files to CSV?) I used las2txt and rLiDAR but neither option included the treeID column in the csv it generated.

  • How is it possible to export to csv and include the tree segmentation
    results?
  • Would it work best to create a spatial points data frame following lidR book Chapter 11 then use the SF or SP package to convert it to a csv? (https://jean-romain.github.io/lidRbook/tba.html)

Additionally as a second topic, I am interested in linking my results with a postgresql / postGIS database. Is a conversion to csv followed by a table creation in postgresql efficient? Any better way?

Best Answer

Run the example code in the help:

LASfile <- system.file("extdata", "MixedConifer.laz", package="lidR")
 las <- readLAS(LASfile, select = "xyz", filter = "-drop_z_below 0")
 
 # Using Li et al. (2012)
 las <- segment_trees(las, li2012(R = 3, speed_up = 5))

Converting that to an sf object gives one point per LAS input point (37,000 of them), with tree ID of 269 trees:

library(sf)
lsp = st_as_sf(as.spatial(las))
plot(lsp[,"treeID"])
range(lsp$treeID,na.rm=TRUE)
# [1]   1 269

enter image description here

(Note that I can't find a direct las-to-sf function, so I'm using lidR::as.spatial to make an sp object, then st_as_sf to convert to sf.)

To save that as CSV, convert to a data frame with the coordinates:

> lspdf = cbind(st_drop_geometry(lsp), st_coordinates(lsp))
> head(lspdf)
     Z treeID        X       Y
1 0.07     80 481349.5 3813011
2 0.11     80 481348.7 3813011
3 0.04     80 481348.7 3813010

and that can be saved using write.csv or write.table or whatever.

If you want one record per tree, use tree_metrics to summarize over trees, convert to sf:

metrics <- tree_metrics(las, ~list(z_max = max(Z))) # calculate tree metrics
metrics.sf = st_as_sf(metrics)
head(metrics.sf)
# Simple feature collection with 6 features and 2 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 481325.2 ymin: 3813006 xmax: 481349.8 ymax: 3813011
# Projected CRS: NAD83 / UTM zone 12N
#   treeID z_max                 geometry
# 1     80 23.00 POINT (481349.9 3813011)
# 2     83 22.96 POINT (481336.1 3813011)
# 3     67 23.46 POINT (481334.6 3813006)

that is 269 rows, one per tree. Convert to data frame as before and save as before.

Related Question