LAS-Read/LASRead/LASFormat/FileHeader.cs

368 lines
17 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Printing.IndexedProperties;
using System.Text;
using System.Windows.Markup;
namespace LASRead.LASFormat
{
class LASFormatDetail
{
/* Primative Types:
* (LAS = C#)
* char = byte
* uchar = char
* short = int16
* ushort = uint16
* long = int
* ulong = uint
* long long = int64
* ulong long = uint64
* float = float
* double = double
* string = 1-byte, ASCII chars, null terminated unless at max length (therefore no termination)
*/
/* Public header:
* char[4] - signature
* ushort - file id
* ushort encoding
* ulong ?- guid 1
* ushort ?- guid 2
* ushort ?- guid 3
* uchar[8] ?- guid 4
* uchar
*/
}
public class FileHeader
{
/// <summary>
/// Signature of the data. Should always be "LASF"
/// </summary>
public char[] FileSignature { get; set; }
/// <summary>
/// ID Associated with the data. Intended to differentiate between different sources
/// </summary>
public ushort FileSourceID { get; set; }
/// <summary>
/// Flags Indicating the formats of certain data <br />
/// 0 : GPS Time Format (0 - Week time; 1 - standard GPS Time) <br />
/// 1 : Waveforms included (depricated) <br />
/// 2 : Waveforms in associated .wdp file (mutex with 1:) <br />
/// 3 : Point returns are synthetic <br />
/// 4 : Coordinate Reference System is WKT, else GeoTIFF <br />
/// 5-15 : Reserved <br />
/// </summary>
public ushort GlobalEncoding { get; set; }
public uint GUIDData11 { get; set; } = 0;
public ushort GUIDData21 { get; set; } = 0;
public ushort GUIDData31 { get; set; } = 0;
public byte[] GUIDData41 { get; set; } = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
public byte VersionMajor { get; set; }
public byte VersionMinor { get; set; }
/// <summary>
/// Identifies the hardware used to collect the data <br />
/// Can be MERGE, MODIFICATION, EXTRACTION, TRANSFORMATION, OTHER, or a custom expression
/// </summary>
public char[] SystemIdentifier { get; set; }
/// <summary>
/// Identifies the software used to encode the data
/// </summary>
public char[] GeneratingSoftware { get; set; }
/// <summary>
/// The day of the year this file was created
/// </summary>
public ushort FileCreationDayOfYear { get; set; }
/// <summary>
/// The year this data was collected
/// </summary>
public ushort FileCreationYear { get; set; }
/// <summary>
/// The size of this header
/// </summary>
public ushort HeaderSize { get; set; }
/// <summary>
/// The offset of the first Point-Data-Record (PDR)
/// </summary>
public uint DataOffset { get; set; }
/// <summary>
/// The number of Variable Length Records (VLRs) present
/// </summary>
public uint NumberVLRs { get; set; }
/// <summary>
/// An integer representing the format of the PDR data. <br />
/// See <see cref="IPointDataRecord"/>
/// </summary>
public byte PointDataRecordFormat { get; set; }
/// <summary>
/// The length of a PDR record
/// </summary>
public ushort PointDataRecordLength { get; set; }
/// <summary>
/// Number of point records for legacy compatibility (32 bits)
/// </summary>
public uint LegacyNumberOfPointRecords { get; set; }
/// <summary>
/// todo
/// </summary>
public uint[] LegacyNumberOfPointByReturn { get; set; }
/// <summary>
/// Scale factor of the X axis. Multiply by this value to get the true value
/// </summary>
public double X_scaleFactor { get; set; }
/// <summary>
/// Scale factor of the Y axis. Multiply by this value to get the true value
/// </summary>
public double Y_scaleFactor { get; set; }
/// <summary>
/// Scale factor of the Z axis. Multiply by this value to get the true value
/// </summary>
public double Z_scaleFactor { get; set; }
/// <summary>
/// Offset of the X axis. <br />
/// Calculate Coordinates like so: <br />
/// X_final = X * X_scaleFactor + X_offset
/// </summary>
public double X_offset { get; set; }
/// <summary>
/// Offset of the Y axis. <br />
/// Calculate Coordinates like so: <br />
/// Y_final = Y * Y_scaleFactor + Y_offset
/// </summary>
public double Y_offset { get; set; }
/// <summary>
/// Offset of the Z axis. <br />
/// Calculate Coordinates like so: <br />
/// Z_final = Z * Z_scaleFactor + Z_offset
/// </summary>
public double Z_offset { get; set; }
/// <summary>
/// Largest X value in the data
/// </summary>
public double X_max { get; set; }
/// <summary>
/// Smallest X value in the data
/// </summary>
public double X_min { get; set; }
/// <summary>
/// Largest Y value in the data
/// </summary>
public double Y_max { get; set; }
/// <summary>
/// Smallest Y value in the data
/// </summary>
public double Y_min { get; set; }
/// <summary>
/// Largest Z value in the data
/// </summary>
public double Z_max { get; set; }
/// <summary>
/// Smallest Z value in the data
/// </summary>
public double Z_min { get; set; }
/// <summary>
/// <b>New in 1.4</b><br />
/// Start of the Waveform Data Packet Records
/// </summary>
public ulong StartOfWaveformDPR { get; set; }
/// <summary>
/// <b>New in 1.4</b><br />
/// Start of the first Extended Variable Length Record (EVLR)
/// </summary>
public ulong StartOfFirstExtendedVLR { get; set; }
/// <summary>
/// <b>New in 1.4</b><br />
/// Number of Extended Variable Length Records
/// </summary>
public uint NumberOfExtendedVLRs { get; set; }
/// <summary>
/// <b>New in 1.4</b><br />
/// Number of point records (64 bits)
/// </summary>
public ulong NumberPointRecords { get; set; }
/// <summary>
/// <b>New in 1.4</b><br />
/// todo
/// </summary>
public ulong[] NumberPointsByReturn { get; set; }
public byte[] GetAsByteArray()
{
byte[] endBytes = new byte[HeaderSize];
DataHelpers.ToByteArray(FileSignature).CopyTo(endBytes,0); //4
BitConverter.GetBytes(FileSourceID).CopyTo(endBytes, 4); // 2
BitConverter.GetBytes(GlobalEncoding).CopyTo(endBytes, 6); // 2
BitConverter.GetBytes(GUIDData11).CopyTo(endBytes, 8); // 4
BitConverter.GetBytes(GUIDData21).CopyTo(endBytes, 12); // 2
BitConverter.GetBytes(GUIDData31).CopyTo(endBytes, 14); // 2
GUIDData41.CopyTo(endBytes, 16); // 8
endBytes[24] = VersionMajor;
endBytes[25] = VersionMinor;
DataHelpers.ToByteArray(SystemIdentifier).CopyTo(endBytes, 26); // 32 Bytes
DataHelpers.ToByteArray(GeneratingSoftware).CopyTo(endBytes, 58); // 32 Bytes
BitConverter.GetBytes(FileCreationDayOfYear).CopyTo(endBytes, 90); // 2
BitConverter.GetBytes(FileCreationYear).CopyTo(endBytes, 92); // 2
BitConverter.GetBytes(HeaderSize).CopyTo(endBytes, 94); // 2
BitConverter.GetBytes(DataOffset).CopyTo(endBytes, 96); // 4
BitConverter.GetBytes(NumberVLRs).CopyTo(endBytes, 100); // 4
endBytes[104] = PointDataRecordFormat;
BitConverter.GetBytes(PointDataRecordLength).CopyTo(endBytes, 105); // 2
BitConverter.GetBytes(LegacyNumberOfPointRecords).CopyTo(endBytes, 107); // 4
DataHelpers.ToByteArray(LegacyNumberOfPointByReturn).CopyTo(endBytes, 111); // 20 bytes
BitConverter.GetBytes(X_scaleFactor).CopyTo(endBytes, 131); // 8
BitConverter.GetBytes(Y_scaleFactor).CopyTo(endBytes, 139); // 8
BitConverter.GetBytes(Z_scaleFactor).CopyTo(endBytes, 147); // 8
BitConverter.GetBytes(X_offset).CopyTo(endBytes, 155); // 8
BitConverter.GetBytes(Y_offset).CopyTo(endBytes, 163); // 8
BitConverter.GetBytes(Z_offset).CopyTo(endBytes, 171); // 8
BitConverter.GetBytes(X_max).CopyTo(endBytes, 179); // 8
BitConverter.GetBytes(X_min).CopyTo(endBytes, 187); // 8
BitConverter.GetBytes(Y_max).CopyTo(endBytes, 195); // 8
BitConverter.GetBytes(Y_min).CopyTo(endBytes, 203); // 8
BitConverter.GetBytes(Z_max).CopyTo(endBytes, 211); // 8
BitConverter.GetBytes(Z_min).CopyTo(endBytes, 219); // 8
if (VersionMajor >= 1 && VersionMinor >= 4)
{
BitConverter.GetBytes(StartOfWaveformDPR).CopyTo(endBytes, 227); // 8
BitConverter.GetBytes(StartOfFirstExtendedVLR).CopyTo(endBytes, 235); // 8
BitConverter.GetBytes(NumberOfExtendedVLRs).CopyTo(endBytes, 243); // 4
BitConverter.GetBytes(NumberPointRecords).CopyTo(endBytes, 247); // 8
DataHelpers.ToByteArray(NumberPointsByReturn).CopyTo(endBytes, 255); // 120 Bytes
}
return endBytes;
}
/// <summary>
/// Reads the header from the provided source
/// </summary>
/// <param name="source"></param>
/// <returns>The expected size of the header</returns>
public bool ReadHeader(Stream source)
{
source.Position = 94;
byte[] headerSizeBytes = new byte[2];
source.Read(headerSizeBytes, 0, 2);
HeaderSize = BitConverter.ToUInt16(headerSizeBytes, 0);
if (HeaderSize < 375)
{
// Assert the version is less than 1.4, where records after z-min were added.
source.Position = 24;
byte major = (byte)source.ReadByte();
byte minor = (byte)source.ReadByte();
if (major >= 1 && minor > 3)
{
throw new InvalidDataException("Header is too small for data format >= v1.4");
}
}
source.Position = 0;
byte[] inputHeader = new byte[HeaderSize];
source.Read(inputHeader, 0, HeaderSize);
FileSignature = new char[] { (char)inputHeader[0], (char)inputHeader[1], (char)inputHeader[2], (char)inputHeader[3] };
FileSourceID = BitConverter.ToUInt16(inputHeader, 4);
GlobalEncoding = BitConverter.ToUInt16(inputHeader, 6);
GUIDData11 = BitConverter.ToUInt32(inputHeader, 8);
GUIDData21 = BitConverter.ToUInt16(inputHeader, 12);
GUIDData31 = BitConverter.ToUInt16(inputHeader, 14);
GUIDData41 = new byte[] { inputHeader[16], inputHeader[17], inputHeader[18], inputHeader[19], inputHeader[20], inputHeader[21], inputHeader[22], inputHeader[23]};
VersionMajor = inputHeader[24];
VersionMinor = inputHeader[25];
SystemIdentifier = DataHelpers.ToCharArray(inputHeader, 26, 32);
GeneratingSoftware = DataHelpers.ToCharArray(inputHeader, 58, 32);
FileCreationDayOfYear = BitConverter.ToUInt16(inputHeader, 90);
FileCreationYear = BitConverter.ToUInt16(inputHeader, 92);
// 94 is headerSize
DataOffset = BitConverter.ToUInt32(inputHeader, 96);
NumberVLRs = BitConverter.ToUInt32(inputHeader, 100);
PointDataRecordFormat = inputHeader[104];
PointDataRecordLength = BitConverter.ToUInt16(inputHeader, 105);
LegacyNumberOfPointRecords = BitConverter.ToUInt32(inputHeader, 107);
LegacyNumberOfPointByReturn = DataHelpers.ToUintArray(inputHeader, 111, 20);
X_scaleFactor = BitConverter.ToDouble(inputHeader, 131);
Y_scaleFactor = BitConverter.ToDouble(inputHeader, 139);
Z_scaleFactor = BitConverter.ToDouble(inputHeader, 147);
X_offset = BitConverter.ToDouble(inputHeader, 155);
Y_offset = BitConverter.ToDouble(inputHeader, 163);
Z_offset = BitConverter.ToDouble(inputHeader, 171);
X_max = BitConverter.ToDouble(inputHeader, 179);
X_min = BitConverter.ToDouble(inputHeader, 187);
Y_max = BitConverter.ToDouble(inputHeader, 195);
Y_min = BitConverter.ToDouble(inputHeader, 203);
Z_max = BitConverter.ToDouble(inputHeader, 211);
Z_min = BitConverter.ToDouble(inputHeader, 219);
if (VersionMinor >= 4)
{
StartOfWaveformDPR = BitConverter.ToUInt64(inputHeader, 227);
StartOfFirstExtendedVLR = BitConverter.ToUInt64(inputHeader, 235);
NumberOfExtendedVLRs = BitConverter.ToUInt32(inputHeader, 243);
NumberPointRecords = BitConverter.ToUInt64(inputHeader, 247);
NumberPointsByReturn = DataHelpers.ToULongArray(inputHeader, 255, 120);
}
return true;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("Signature: " + new string(FileSignature) + Environment.NewLine);
sb.Append("SourceID: " + FileSourceID.ToString() + Environment.NewLine);
sb.Append("Global Encoding: " + GlobalEncoding.ToString() + Environment.NewLine);
sb.Append("GUIDData1: " + GUIDData11.ToString() + Environment.NewLine);
sb.Append("GUIDData2: " + GUIDData21.ToString() + Environment.NewLine);
sb.Append("GUIDData3: " + GUIDData31.ToString() + Environment.NewLine);
sb.Append("GUIDData4: ");
foreach (byte data in GUIDData41)
{
if (data != 0)
{
sb.Append((char)data);
}
}
sb.Append(Environment.NewLine);
sb.Append("Version: " + VersionMajor.ToString() + "." + VersionMinor.ToString() + Environment.NewLine);
sb.Append("System Identifier: "); sb.Append(SystemIdentifier); sb.Append(Environment.NewLine);
sb.Append("Generating Software: "); sb.Append(GeneratingSoftware); sb.Append(Environment.NewLine);
DateTime creationDate = new DateTime(FileCreationYear, 1, 1);
creationDate = creationDate.AddDays(FileCreationDayOfYear);
sb.Append("Creation Date: " + creationDate.ToShortDateString() + Environment.NewLine);
sb.Append("Header Length: " + HeaderSize.ToString() + Environment.NewLine);
sb.Append("Data Offset: " + DataOffset.ToString() + Environment.NewLine);
sb.Append("Number VRLs: " + NumberVLRs.ToString() + Environment.NewLine);
sb.Append("PointDataFormat: " + PointDataRecordFormat.ToString() + Environment.NewLine);
sb.Append("PointDataLength: " + PointDataRecordLength.ToString() + Environment.NewLine);
sb.Append("Legacy #PDRs: " + LegacyNumberOfPointRecords.ToString() + Environment.NewLine);
sb.Append("Legacy #PDR returns: ");
foreach (uint legacyReturn in LegacyNumberOfPointByReturn)
{
sb.Append(legacyReturn);
sb.Append(" ");
}
sb.Append(Environment.NewLine);
sb.Append("X Scale Factor: " + X_scaleFactor.ToString() + Environment.NewLine);
sb.Append("Y Scale Factor: " + Y_scaleFactor.ToString() + Environment.NewLine);
sb.Append("Z Scale Factor: " + Z_scaleFactor.ToString() + Environment.NewLine);
sb.Append("X Offset: " + X_offset.ToString() + Environment.NewLine);
sb.Append("Y Offset: " + Y_offset.ToString() + Environment.NewLine);
sb.Append("Z Offset: " + Z_offset.ToString() + Environment.NewLine);
sb.Append("X Max: " + X_max.ToString() + Environment.NewLine);
sb.Append("X Min: " + X_min.ToString() + Environment.NewLine);
sb.Append("Y Max: " + Y_max.ToString() + Environment.NewLine);
sb.Append("Y Min: " + Y_min.ToString() + Environment.NewLine);
sb.Append("Z Max: " + Z_max.ToString() + Environment.NewLine);
sb.Append("Z Min: " + Z_min.ToString() + Environment.NewLine);
if (VersionMinor >= 4)
{
sb.Append("Waveform Start: " + StartOfWaveformDPR.ToString() + Environment.NewLine);
sb.Append("Extended VLR Start: " + StartOfFirstExtendedVLR.ToString() + Environment.NewLine);
sb.Append("Number eVLRs: " + NumberOfExtendedVLRs.ToString() + Environment.NewLine);
sb.Append("Number Point Records: " + NumberPointRecords.ToString() + Environment.NewLine);
sb.Append("NPR by Return: " + NumberPointsByReturn.ToString() + Environment.NewLine);
}
return sb.ToString();
}
}
}