How to change values in a LAS file using the ENVI Lidar programming interface (API)
ENVI LiDAR is an interactive geospatial software environment that lets you create realistic 3D visualizations and easily extract important features and products from raw LiDAR point cloud data. The elevation information contained within LiDAR can be used to create Digital Elevation Models (DEMs), or be included in your geospatial products like line-of-sight or right-of way analyses.
Occasionally, it might be desirable to alter the values contained in a Lidar .LAS format file such as elevation (z) or attribute (RGB, class). ENVI LiDAR's API can be used to edit and write out a new .LAS file with the desired changes. The following code examples alter the data within a .LAS file by 1) adding a constant value to the elevations within the file and 2) setting the RGB value of a classified .LAS. The first example code uses the sample .LAS dataset that is contained in the ENVI LiDAR distribution. The second example uses a classified .LAS file.
;-------------------------------
; Example code that changes the elevation (z) values in a .LAS file by adding a constant
PRO AddConstantZValue
COMPILE_OPT idl2
e = e3de(/headless)
lasFileName = 'C:\ProgramFiles\Exelis\ENVILiDAR51\DataSample\DataSample.las'
;Directly read the input LAS filecontaining classification values
oLidar = e.openlidar(lasFileName,/DIRECT_READ)
startIndex = 0
blockSize = 500000
allClass = MAKE_ARRAY(blockSize, /BYTE)
increaseZby = 10
; create the E3DLidarWriter
outFileName = 'C:\Temp\IncreasedZby10.las'
writer = E3DLidar(outFileName,/OVERWRITE)
allread = 0
;Cycle through the points
WHILE (allread eq 0) DO BEGIN
pointsInRange = oLidar.GetPointsInRange(startIndex,blockSize, $
ALL_ATTRIBS=ALL_ATTRIBS, $
COUNT=nPointsRead)
;increase the z value byincreaseZby
pointsInRange[2,*] = pointsInRange[2,*] + increaseZby
;write the updatedpoint values and all attributes
writer.WritePoints, pointsInRange,ALL_ATTRIBS=all_attribs
startIndex += nPointsRead
IF (nPointsRead LT blocksize) THEN allread = 1
ENDWHILE
;Save and close output LAS file
writer.SAVE
writer.CLOSE
e.CLOSE
END
;--------------------------
; Example code that changes an attribute value in a .LAS file by assigning an RGB value to a class
PRO setRGBbyLASclass
COMPILE_OPT idl2
e = e3de(/headless)
;Specify the classified input LASfile name
lasFileName = 'C:\temp\classifiedPointcloud.las'
;Directly read (don’t create a project) the input LAS file containing classification values
oLidar = e.openlidar(lasFileName,/DIRECT_READ)
startIndex = 0
blockSize = 500000
allClass = MAKE_ARRAY(blockSize, /BYTE)
; create the output file
outFileName = FILE_DIRNAME(lasFileName) + '\RGBbyClassification.las'
writer = E3DLidar(outFileName,/OVERWRITE)
allread = 0
;Cycle through the points, setting the RGBvalue based on
;classification values of the points
WHILE (allread eq 0) DO BEGIN
pointsInRange = oLidar.GetPointsInRange(startIndex,blockSize, $
ALL_ATTRIBS=ALL_ATTRIBS, $
COUNT=nPointsRead)
;*ALL_ATTRIBS.CLASSIFICATIONS returns the classification codes stored in
; the las file
inClass = *ALL_ATTRIBS.CLASSIFICATIONS
IF (nPointsRead LT blocksize) THEN allread = 1
allClass =[allClass,inClass]
uniqAllClass = allClass[UNIQ(allClass, SORT(allClass))]
rgb = MAKE_ARRAY(3,nPointsRead, /BYTE)
FOREACH i, uniqAllClass DO BEGIN
CASE i OF
;Unprocessed
0: BEGIN
index = WHERE(inClass EQ 0)
red=190
green=190
blue=190
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
;Unclassified
1: BEGIN
index = WHERE(inClass EQ 1)
red=255
green=255
blue=255
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
;Terrain
2: BEGIN
index = WHERE(inClass EQ 2)
red=255
green=177
blue=100
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
;Near Terrain
3: BEGIN
index = WHERE(inClass EQ 3)
red=128
green=255
blue=0
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
;Trees
5: BEGIN
index = WHERE(inClass EQ 5)
red=0
green=147
blue=0
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
;Buildings
6: BEGIN
index = WHERE(inClass EQ 6)
red=217
green=217
blue=0
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
;Power lines
14: BEGIN
index = WHERE(inClass EQ 14)
red=255
green=100
blue=255
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
;Power poles
15: BEGIN
index = WHERE(inClass EQ 15)
red=160
green=80
blue=0
rgb[0,index] = red
rgb[1,index] = green
rgb[2,index] = blue
END
ELSE: PRINT, 'Bad value'
ENDCASE
ENDFOREACH
writer.WritePoints, pointsInRange,ALL_ATTRIBS=all_attribs, RGB=rgb
startIndex += nPointsRead
ENDWHILE
;Save and close output LAS file
writer.SAVE
writer.CLOSE
e.CLOSE
END
Reviewed 8/25/2014 MM