Monday, February 21, 2011

Example C# code to directly change existing LiDAR LAS file field values

For more efficient manipulation of LiDAR LAS files, C# can be used to directly change values in binary LAS files. Here is a simple example code snippet for locating and changing the Z bounding range fields of an existing LiDAR LAS file.

Essentially, the LAS file has to be opened as a binary file stream first. Then the file stream pointer has to be moved to the correct starting offset of a field before you can change the field values of the existing LAS file using the binary writer's write method. You have to ensure the C# data types match the LAS file fields though. In this case, the starting offsets for the Max Z and Min Z fields (doubles at 8 bytes each) are at  211 and 219 bytes respectively.

For more details about the LAS fields and the byte offsets, click on this link http://liblas.org/development/specifications.html.


double maxz, minz;
//....etc....
//Somewhere here the Z bounding range (minz and maxz) is calculated

//Open a file stream to the LAS file
FileStream fs = File.Open(@"C:\Temp\file.las", FileMode.Open);
if (fs.CanSeek)
{
    BinaryWriter binWriter = new BinaryWriter(fs);
    //move to the location of the maxz field in the LAS file
    fs.Seek(211, SeekOrigin.Begin); 
    //Write the maxz double value to the LAS file
    binWriter.Write(maxz);
    //move to the location of the minz field in the LAS file
    fs.Seek(219, SeekOrigin.Begin); 
    //Write the minz double value to the LAS file
    binWriter.Write(minz);
    //Close the writer
    binWriter.Close();
}
//Close the LAS file
fs.Close();
}

5 comments:

  1. Have you ever worked on extracting Header information from .LAS file. If so could you let me know the same.

    Thanks in advance,
    Deeksha

    ReplyDelete
  2. deeksha, you can use the libraries from liblas.org. There is a dotnetliblas wrapper that you can use in your code.

    ReplyDelete
  3. I'm trying to use the dotnetliblas wrapper. But I'm getting error like 'Unable to load dll "liblas1.dll". Can anyone help me to resolve this error.

    ReplyDelete
  4. Sivakumar, it is probably something simple like the liblas1.dll is not in the path of the wrapper assembly.

    ReplyDelete
  5. Yes. It is not available in the dotnetliblas download. I compiled the dotnetliblas in vs2008 and it created the 'dotnetLibLAS.dll' file. When I access it, it looks for liblas1.dll. How to get this dll?

    Thanks
    Sivakumar

    ReplyDelete