Added comments, replaced private fields with auto properties

This commit is contained in:
Brychan Dempsey 2021-11-16 14:02:05 +13:00
parent f430800a45
commit c966dfe4e9
3 changed files with 101 additions and 108 deletions

View File

@ -12,13 +12,22 @@ namespace LASFormat
{ {
public class LASFile : IDisposable public class LASFile : IDisposable
{ {
// based on the LAS format specifications from asprs - The imaging and Geospatial Information Society - https://www.asprs.org/divisions-committees/lidar-division/laser-las-file-format-exchange-activities
// Parses LAS files, such that the data is in a more readily-usable format
//
// Todos:
// - Split a LAS file into subsections, probably about 100m^2
// - generate heightmaps from sections, 0-255 per point
// - Output heightmaps in a Unity- and Unreal- ready format
// - Ouput vertex colours for each point, if available
// - Allow exporting different layers of a file
private const int V = 16384; private const int V = 16384;
private const int V1 = V * (ushort.MaxValue +1); private const int V1 = V * (ushort.MaxValue +1);
// File Structure (v1.4): // File Structure (v1.4):
// FileHeader // File Header (a.k.a. Public Header Block)
// VLRs (Variable Length Records) // VLRs (Variable Length Records) - Any number of these (max 65535 bytes)
// PDRs (Point Data Records) // PDRs (Point Data Records) - Any number of
// EVLRs (Extended VRLs) // EVLRs (Extended VRLs)
Stream source; Stream source;
Stream VLRStream; Stream VLRStream;
@ -44,14 +53,14 @@ namespace LASFormat
public Type PDRType { get; private set; } public Type PDRType { get; private set; }
/// <summary> /// <summary>
/// Opens the .las file from the provided stream /// Opens the LAS file from the provided stream
/// </summary> /// </summary>
/// <param name="source"></param> /// <param name="source"></param>
public LASFile(Stream source) public LASFile(Stream source)
{ {
this.source = source; this.source = source;
Header = new FileHeader(); Header = new FileHeader();
Header.ReadHeader(source); Header.ReadHeader(source); // Reads intrinsics from the stream, such as
PDRType = Type.GetType("LASRead.LASFormat.PDR" + Header.PointDataRecordFormat.ToString()); PDRType = Type.GetType("LASRead.LASFormat.PDR" + Header.PointDataRecordFormat.ToString());

View File

@ -39,53 +39,14 @@ namespace LASRead.LASFormat
} }
public class FileHeader public class FileHeader
{ {
ushort headerSize;
// Follows the correct order of the header
char[] fileSignature;
ushort fileSourceID;
ushort globalEncoding;
// Optional Fields are filled with zero
uint GUIDData1 = 0;
ushort GUIDData2 = 0;
ushort GUIDData3 = 0;
byte[] GUIDData4 = new byte[] {0,0,0,0,0,0,0,0};
byte versionMajor;
byte versionMinor;
char[] systemIdentifier;
char[] generatingSoftware;
ushort fileCreationDayOfYear;
ushort fileCreationYear;
uint dataOffset;
uint numberVLRs; // Variable Length Records
byte pointDataRecordFormat;
ushort pointDataRecordLength;
uint legacyNumberOfPointRecords;
uint[] legacyNumberOfPointByReturn;
double x_scaleFactor;
double y_scaleFactor;
double z_scaleFactor;
double x_offset;
double y_offset;
double z_offset;
double x_max;
double x_min;
double y_max;
double y_min;
double z_max;
double z_min;
ulong startOfWaveformDPR; // Data Packet Record
ulong startOfFirstExtendedVLR;
uint numberOfExtendedVLRs;
ulong numberPointRecords;
ulong[] numberPointsByReturn;
/// <summary> /// <summary>
/// Signature of the data. Should always be "LASF" /// Signature of the data. Should always be "LASF"
/// </summary> /// </summary>
public char[] FileSignature { get => fileSignature; set => fileSignature = value; } public char[] FileSignature { get; set; }
/// <summary> /// <summary>
/// ID Associated with the data. Intended to differentiate between different sources /// ID Associated with the data. Intended to differentiate between different sources
/// </summary> /// </summary>
public ushort FileSourceID { get => fileSourceID; set => fileSourceID = value; } public ushort FileSourceID { get; set; }
/// <summary> /// <summary>
/// Flags Indicating the formats of certain data <br /> /// Flags Indicating the formats of certain data <br />
/// 0 : GPS Time Format (0 - Week time; 1 - standard GPS Time) <br /> /// 0 : GPS Time Format (0 - Week time; 1 - standard GPS Time) <br />
@ -95,161 +56,161 @@ namespace LASRead.LASFormat
/// 4 : Coordinate Reference System is WKT, else GeoTIFF <br /> /// 4 : Coordinate Reference System is WKT, else GeoTIFF <br />
/// 5-15 : Reserved <br /> /// 5-15 : Reserved <br />
/// </summary> /// </summary>
public ushort GlobalEncoding { get => globalEncoding; set => globalEncoding = value; } public ushort GlobalEncoding { get; set; }
public uint GUIDData11 { get => GUIDData1; set => GUIDData1 = value; } public uint GUIDData11 { get; set; } = 0;
public ushort GUIDData21 { get => GUIDData2; set => GUIDData2 = value; } public ushort GUIDData21 { get; set; } = 0;
public ushort GUIDData31 { get => GUIDData3; set => GUIDData3 = value; } public ushort GUIDData31 { get; set; } = 0;
public byte[] GUIDData41 { get => GUIDData4; set => GUIDData4 = value; } public byte[] GUIDData41 { get; set; } = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
public byte VersionMajor { get => versionMajor; set => versionMajor = value; } public byte VersionMajor { get; set; }
public byte VersionMinor { get => versionMinor; set => versionMinor = value; } public byte VersionMinor { get; set; }
/// <summary> /// <summary>
/// Identifies the hardware used to collect the data <br /> /// Identifies the hardware used to collect the data <br />
/// Can be MERGE, MODIFICATION, EXTRACTION, TRANSFORMATION, OTHER, or a custom expression /// Can be MERGE, MODIFICATION, EXTRACTION, TRANSFORMATION, OTHER, or a custom expression
/// </summary> /// </summary>
public char[] SystemIdentifier { get => systemIdentifier; set => systemIdentifier = value; } public char[] SystemIdentifier { get; set; }
/// <summary> /// <summary>
/// Identifies the software used to encode the data /// Identifies the software used to encode the data
/// </summary> /// </summary>
public char[] GeneratingSoftware { get => generatingSoftware; set => generatingSoftware = value; } public char[] GeneratingSoftware { get; set; }
/// <summary> /// <summary>
/// The day of the year this file was created /// The day of the year this file was created
/// </summary> /// </summary>
public ushort FileCreationDayOfYear { get => fileCreationDayOfYear; set => fileCreationDayOfYear = value; } public ushort FileCreationDayOfYear { get; set; }
/// <summary> /// <summary>
/// The year this data was collected /// The year this data was collected
/// </summary> /// </summary>
public ushort FileCreationYear { get => fileCreationYear; set => fileCreationYear = value; } public ushort FileCreationYear { get; set; }
/// <summary> /// <summary>
/// The size of this header /// The size of this header
/// </summary> /// </summary>
public ushort HeaderSize { get => headerSize; set => headerSize = value; } public ushort HeaderSize { get; set; }
/// <summary> /// <summary>
/// The offset of the first Point-Data-Record (PDR) /// The offset of the first Point-Data-Record (PDR)
/// </summary> /// </summary>
public uint DataOffset { get => dataOffset; set => dataOffset = value; } public uint DataOffset { get; set; }
/// <summary> /// <summary>
/// The number of Variable Length Records (VLRs) present /// The number of Variable Length Records (VLRs) present
/// </summary> /// </summary>
public uint NumberVLRs { get => numberVLRs; set => numberVLRs = value; } public uint NumberVLRs { get; set; }
/// <summary> /// <summary>
/// An integer representing the format of the PDR data. <br /> /// An integer representing the format of the PDR data. <br />
/// See <see cref="IPointDataRecord"/> /// See <see cref="IPointDataRecord"/>
/// </summary> /// </summary>
public byte PointDataRecordFormat { get => pointDataRecordFormat; set => pointDataRecordFormat = value; } public byte PointDataRecordFormat { get; set; }
/// <summary> /// <summary>
/// The length of a PDR record /// The length of a PDR record
/// </summary> /// </summary>
public ushort PointDataRecordLength { get => pointDataRecordLength; set => pointDataRecordLength = value; } public ushort PointDataRecordLength { get; set; }
/// <summary> /// <summary>
/// Number of point records for legacy compatibility (32 bits) /// Number of point records for legacy compatibility (32 bits)
/// </summary> /// </summary>
public uint LegacyNumberOfPointRecords { get => legacyNumberOfPointRecords; set => legacyNumberOfPointRecords = value; } public uint LegacyNumberOfPointRecords { get; set; }
/// <summary> /// <summary>
/// todo /// todo
/// </summary> /// </summary>
public uint[] LegacyNumberOfPointByReturn { get => legacyNumberOfPointByReturn; set => legacyNumberOfPointByReturn = value; } public uint[] LegacyNumberOfPointByReturn { get; set; }
/// <summary> /// <summary>
/// Scale factor of the X axis. Multiply by this value to get the true value /// Scale factor of the X axis. Multiply by this value to get the true value
/// </summary> /// </summary>
public double X_scaleFactor { get => x_scaleFactor; set => x_scaleFactor = value; } public double X_scaleFactor { get; set; }
/// <summary> /// <summary>
/// Scale factor of the Y axis. Multiply by this value to get the true value /// Scale factor of the Y axis. Multiply by this value to get the true value
/// </summary> /// </summary>
public double Y_scaleFactor { get => y_scaleFactor; set => y_scaleFactor = value; } public double Y_scaleFactor { get; set; }
/// <summary> /// <summary>
/// Scale factor of the Z axis. Multiply by this value to get the true value /// Scale factor of the Z axis. Multiply by this value to get the true value
/// </summary> /// </summary>
public double Z_scaleFactor { get => z_scaleFactor; set => z_scaleFactor = value; } public double Z_scaleFactor { get; set; }
/// <summary> /// <summary>
/// Offset of the X axis. <br /> /// Offset of the X axis. <br />
/// Calculate Coordinates like so: <br /> /// Calculate Coordinates like so: <br />
/// X_final = X * X_scaleFactor + X_offset /// X_final = X * X_scaleFactor + X_offset
/// </summary> /// </summary>
public double X_offset { get => x_offset; set => x_offset = value; } public double X_offset { get; set; }
/// <summary> /// <summary>
/// Offset of the Y axis. <br /> /// Offset of the Y axis. <br />
/// Calculate Coordinates like so: <br /> /// Calculate Coordinates like so: <br />
/// Y_final = Y * Y_scaleFactor + Y_offset /// Y_final = Y * Y_scaleFactor + Y_offset
/// </summary> /// </summary>
public double Y_offset { get => y_offset; set => y_offset = value; } public double Y_offset { get; set; }
/// <summary> /// <summary>
/// Offset of the Z axis. <br /> /// Offset of the Z axis. <br />
/// Calculate Coordinates like so: <br /> /// Calculate Coordinates like so: <br />
/// Z_final = Z * Z_scaleFactor + Z_offset /// Z_final = Z * Z_scaleFactor + Z_offset
/// </summary> /// </summary>
public double Z_offset { get => z_offset; set => z_offset = value; } public double Z_offset { get; set; }
/// <summary> /// <summary>
/// Largest X value in the data /// Largest X value in the data
/// </summary> /// </summary>
public double X_max { get => x_max; set => x_max = value; } public double X_max { get; set; }
/// <summary> /// <summary>
/// Smallest X value in the data /// Smallest X value in the data
/// </summary> /// </summary>
public double X_min { get => x_min; set => x_min = value; } public double X_min { get; set; }
/// <summary> /// <summary>
/// Largest Y value in the data /// Largest Y value in the data
/// </summary> /// </summary>
public double Y_max { get => y_max; set => y_max = value; } public double Y_max { get; set; }
/// <summary> /// <summary>
/// Smallest Y value in the data /// Smallest Y value in the data
/// </summary> /// </summary>
public double Y_min { get => y_min; set => y_min = value; } public double Y_min { get; set; }
/// <summary> /// <summary>
/// Largest Z value in the data /// Largest Z value in the data
/// </summary> /// </summary>
public double Z_max { get => z_max; set => z_max = value; } public double Z_max { get; set; }
/// <summary> /// <summary>
/// Smallest Z value in the data /// Smallest Z value in the data
/// </summary> /// </summary>
public double Z_min { get => z_min; set => z_min = value; } public double Z_min { get; set; }
/// <summary> /// <summary>
/// <b>New in 1.4</b><br /> /// <b>New in 1.4</b><br />
/// Start of the Waveform Data Packet Records /// Start of the Waveform Data Packet Records
/// </summary> /// </summary>
public ulong StartOfWaveformDPR { get => startOfWaveformDPR; set => startOfWaveformDPR = value; } public ulong StartOfWaveformDPR { get; set; }
/// <summary> /// <summary>
/// <b>New in 1.4</b><br /> /// <b>New in 1.4</b><br />
/// Start of the first Extended Variable Length Record (EVLR) /// Start of the first Extended Variable Length Record (EVLR)
/// </summary> /// </summary>
public ulong StartOfFirstExtendedVLR { get => startOfFirstExtendedVLR; set => startOfFirstExtendedVLR = value; } public ulong StartOfFirstExtendedVLR { get; set; }
/// <summary> /// <summary>
/// <b>New in 1.4</b><br /> /// <b>New in 1.4</b><br />
/// Number of Extended Variable Length Records /// Number of Extended Variable Length Records
/// </summary> /// </summary>
public uint NumberOfExtendedVLRs { get => numberOfExtendedVLRs; set => numberOfExtendedVLRs = value; } public uint NumberOfExtendedVLRs { get; set; }
/// <summary> /// <summary>
/// <b>New in 1.4</b><br /> /// <b>New in 1.4</b><br />
/// Number of point records (64 bits) /// Number of point records (64 bits)
/// </summary> /// </summary>
public ulong NumberPointRecords { get => numberPointRecords; set => numberPointRecords = value; } public ulong NumberPointRecords { get; set; }
/// <summary> /// <summary>
/// <b>New in 1.4</b><br /> /// <b>New in 1.4</b><br />
/// todo /// todo
/// </summary> /// </summary>
public ulong[] NumberPointsByReturn { get => numberPointsByReturn; set => numberPointsByReturn = value; } public ulong[] NumberPointsByReturn { get; set; }
public byte[] GetAsByteArray() public byte[] GetAsByteArray()
{ {
byte[] endBytes = new byte[headerSize]; byte[] endBytes = new byte[HeaderSize];
DataHelpers.ToByteArray(fileSignature).CopyTo(endBytes,0); //4 DataHelpers.ToByteArray(FileSignature).CopyTo(endBytes,0); //4
BitConverter.GetBytes(fileSourceID).CopyTo(endBytes, 4); // 2 BitConverter.GetBytes(FileSourceID).CopyTo(endBytes, 4); // 2
BitConverter.GetBytes(globalEncoding).CopyTo(endBytes, 6); // 2 BitConverter.GetBytes(GlobalEncoding).CopyTo(endBytes, 6); // 2
BitConverter.GetBytes(GUIDData1).CopyTo(endBytes, 8); // 4 BitConverter.GetBytes(GUIDData11).CopyTo(endBytes, 8); // 4
BitConverter.GetBytes(GUIDData2).CopyTo(endBytes, 12); // 2 BitConverter.GetBytes(GUIDData21).CopyTo(endBytes, 12); // 2
BitConverter.GetBytes(GUIDData3).CopyTo(endBytes, 14); // 2 BitConverter.GetBytes(GUIDData31).CopyTo(endBytes, 14); // 2
GUIDData4.CopyTo(endBytes, 16); // 8 GUIDData41.CopyTo(endBytes, 16); // 8
endBytes[24] = versionMajor; endBytes[24] = VersionMajor;
endBytes[25] = versionMinor; endBytes[25] = VersionMinor;
DataHelpers.ToByteArray(systemIdentifier).CopyTo(endBytes, 26); // 32 Bytes DataHelpers.ToByteArray(SystemIdentifier).CopyTo(endBytes, 26); // 32 Bytes
DataHelpers.ToByteArray(GeneratingSoftware).CopyTo(endBytes, 58); // 32 Bytes DataHelpers.ToByteArray(GeneratingSoftware).CopyTo(endBytes, 58); // 32 Bytes
BitConverter.GetBytes(fileCreationDayOfYear).CopyTo(endBytes, 90); // 2 BitConverter.GetBytes(FileCreationDayOfYear).CopyTo(endBytes, 90); // 2
BitConverter.GetBytes(fileCreationYear).CopyTo(endBytes, 92); // 2 BitConverter.GetBytes(FileCreationYear).CopyTo(endBytes, 92); // 2
BitConverter.GetBytes(headerSize).CopyTo(endBytes, 94); // 2 BitConverter.GetBytes(HeaderSize).CopyTo(endBytes, 94); // 2
BitConverter.GetBytes(dataOffset).CopyTo(endBytes, 96); // 4 BitConverter.GetBytes(DataOffset).CopyTo(endBytes, 96); // 4
BitConverter.GetBytes(numberVLRs).CopyTo(endBytes, 100); // 4 BitConverter.GetBytes(NumberVLRs).CopyTo(endBytes, 100); // 4
endBytes[104] = pointDataRecordFormat; endBytes[104] = PointDataRecordFormat;
BitConverter.GetBytes(pointDataRecordLength).CopyTo(endBytes, 105); // 2 BitConverter.GetBytes(PointDataRecordLength).CopyTo(endBytes, 105); // 2
BitConverter.GetBytes(legacyNumberOfPointRecords).CopyTo(endBytes, 107); // 4 BitConverter.GetBytes(LegacyNumberOfPointRecords).CopyTo(endBytes, 107); // 4
DataHelpers.ToByteArray(LegacyNumberOfPointByReturn).CopyTo(endBytes, 111); // 20 bytes DataHelpers.ToByteArray(LegacyNumberOfPointByReturn).CopyTo(endBytes, 111); // 20 bytes
BitConverter.GetBytes(X_scaleFactor).CopyTo(endBytes, 131); // 8 BitConverter.GetBytes(X_scaleFactor).CopyTo(endBytes, 131); // 8
BitConverter.GetBytes(Y_scaleFactor).CopyTo(endBytes, 139); // 8 BitConverter.GetBytes(Y_scaleFactor).CopyTo(endBytes, 139); // 8
@ -265,7 +226,7 @@ namespace LASRead.LASFormat
BitConverter.GetBytes(Z_min).CopyTo(endBytes, 219); // 8 BitConverter.GetBytes(Z_min).CopyTo(endBytes, 219); // 8
if (VersionMajor >= 1 && VersionMinor >= 4) if (VersionMajor >= 1 && VersionMinor >= 4)
{ {
BitConverter.GetBytes(startOfWaveformDPR).CopyTo(endBytes, 227); // 8 BitConverter.GetBytes(StartOfWaveformDPR).CopyTo(endBytes, 227); // 8
BitConverter.GetBytes(StartOfFirstExtendedVLR).CopyTo(endBytes, 235); // 8 BitConverter.GetBytes(StartOfFirstExtendedVLR).CopyTo(endBytes, 235); // 8
BitConverter.GetBytes(NumberOfExtendedVLRs).CopyTo(endBytes, 243); // 4 BitConverter.GetBytes(NumberOfExtendedVLRs).CopyTo(endBytes, 243); // 4
BitConverter.GetBytes(NumberPointRecords).CopyTo(endBytes, 247); // 8 BitConverter.GetBytes(NumberPointRecords).CopyTo(endBytes, 247); // 8
@ -283,8 +244,8 @@ namespace LASRead.LASFormat
source.Position = 94; source.Position = 94;
byte[] headerSizeBytes = new byte[2]; byte[] headerSizeBytes = new byte[2];
source.Read(headerSizeBytes, 0, 2); source.Read(headerSizeBytes, 0, 2);
headerSize = BitConverter.ToUInt16(headerSizeBytes, 0); HeaderSize = BitConverter.ToUInt16(headerSizeBytes, 0);
if (headerSize < 375) if (HeaderSize < 375)
{ {
// Assert the version is less than 1.4, where records after z-min were added. // Assert the version is less than 1.4, where records after z-min were added.
source.Position = 24; source.Position = 24;
@ -297,7 +258,7 @@ namespace LASRead.LASFormat
} }
source.Position = 0; source.Position = 0;
byte[] inputHeader = new byte[headerSize]; byte[] inputHeader = new byte[HeaderSize];
source.Read(inputHeader, 0, HeaderSize); source.Read(inputHeader, 0, HeaderSize);
FileSignature = new char[] { (char)inputHeader[0], (char)inputHeader[1], (char)inputHeader[2], (char)inputHeader[3] }; FileSignature = new char[] { (char)inputHeader[0], (char)inputHeader[1], (char)inputHeader[2], (char)inputHeader[3] };
@ -325,19 +286,19 @@ namespace LASRead.LASFormat
Z_scaleFactor = BitConverter.ToDouble(inputHeader, 147); Z_scaleFactor = BitConverter.ToDouble(inputHeader, 147);
X_offset = BitConverter.ToDouble(inputHeader, 155); X_offset = BitConverter.ToDouble(inputHeader, 155);
Y_offset = BitConverter.ToDouble(inputHeader, 163); Y_offset = BitConverter.ToDouble(inputHeader, 163);
z_offset = BitConverter.ToDouble(inputHeader, 171); Z_offset = BitConverter.ToDouble(inputHeader, 171);
X_max = BitConverter.ToDouble(inputHeader, 179); X_max = BitConverter.ToDouble(inputHeader, 179);
X_min = BitConverter.ToDouble(inputHeader, 187); X_min = BitConverter.ToDouble(inputHeader, 187);
Y_max = BitConverter.ToDouble(inputHeader, 195); Y_max = BitConverter.ToDouble(inputHeader, 195);
Y_min = BitConverter.ToDouble(inputHeader, 203); Y_min = BitConverter.ToDouble(inputHeader, 203);
Z_max = BitConverter.ToDouble(inputHeader, 211); Z_max = BitConverter.ToDouble(inputHeader, 211);
Z_min = BitConverter.ToDouble(inputHeader, 219); Z_min = BitConverter.ToDouble(inputHeader, 219);
if (versionMinor >= 4) if (VersionMinor >= 4)
{ {
StartOfWaveformDPR = BitConverter.ToUInt64(inputHeader, 227); StartOfWaveformDPR = BitConverter.ToUInt64(inputHeader, 227);
StartOfFirstExtendedVLR = BitConverter.ToUInt64(inputHeader, 235); StartOfFirstExtendedVLR = BitConverter.ToUInt64(inputHeader, 235);
NumberOfExtendedVLRs = BitConverter.ToUInt32(inputHeader, 243); NumberOfExtendedVLRs = BitConverter.ToUInt32(inputHeader, 243);
numberPointRecords = BitConverter.ToUInt64(inputHeader, 247); NumberPointRecords = BitConverter.ToUInt64(inputHeader, 247);
NumberPointsByReturn = DataHelpers.ToULongArray(inputHeader, 255, 120); NumberPointsByReturn = DataHelpers.ToULongArray(inputHeader, 255, 120);
} }
return true; return true;
@ -392,7 +353,7 @@ namespace LASRead.LASFormat
sb.Append("Y Min: " + Y_min.ToString() + Environment.NewLine); sb.Append("Y Min: " + Y_min.ToString() + Environment.NewLine);
sb.Append("Z Max: " + Z_max.ToString() + Environment.NewLine); sb.Append("Z Max: " + Z_max.ToString() + Environment.NewLine);
sb.Append("Z Min: " + Z_min.ToString() + Environment.NewLine); sb.Append("Z Min: " + Z_min.ToString() + Environment.NewLine);
if (versionMinor >= 4) if (VersionMinor >= 4)
{ {
sb.Append("Waveform Start: " + StartOfWaveformDPR.ToString() + Environment.NewLine); sb.Append("Waveform Start: " + StartOfWaveformDPR.ToString() + Environment.NewLine);
sb.Append("Extended VLR Start: " + StartOfFirstExtendedVLR.ToString() + Environment.NewLine); sb.Append("Extended VLR Start: " + StartOfFirstExtendedVLR.ToString() + Environment.NewLine);

23
LASRead/UnitTest1.cs Normal file
View File

@ -0,0 +1,23 @@
using NUnit.Framework;
using LASFormat;
using System.IO;
namespace Tests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
FileStream fs = File.OpenRead("C:\\points.las");
LASFile lasFile = new LASFile(fs);
Assert.Pass();
}
}
}