Compare commits

..

2 Commits

Author SHA1 Message Date
5af930b4ea Fixed a number of small bugs, refactored 2021-11-17 13:16:14 +13:00
269373faa2 Refactored and cleaned code 2021-11-17 12:29:35 +13:00
7 changed files with 100 additions and 142 deletions

View File

@ -62,6 +62,7 @@ namespace LASFormat
Header = new FileHeader();
Header.ReadHeader(source); // Reads intrinsics from the stream, such as
// Reflective PDR type
PDRType = Type.GetType("LASRead.LASFormat.PDR" + Header.PointDataRecordFormat.ToString());
@ -74,7 +75,7 @@ namespace LASFormat
VLRStart = Header.HeaderSize;
VLRHeader initial = new VLRHeader();
initial.ReadRecords(VLRStream);
vlrCollection = new RecordCollection(VLRStream, VLRStart, Header.NumberVLRs, initial);
vlrCollection = new RecordCollection(ref VLRStream, VLRStart, Header.NumberVLRs, initial);
// Grab a starting PDR
PDRStart = Header.DataOffset;
@ -83,12 +84,16 @@ namespace LASFormat
PDRStream = GetOffsetStream(source, PDRSize);
byte[] pdrInitial = new byte[Header.PointDataRecordLength];
PDRStream.Read(pdrInitial, 0, Header.PointDataRecordLength);
// Convert the PDRF to our PDR types. Uses System.Reflections to find the value and avoid the use of switch-case.
// Use generics to identify the PDR type.
// i.e., Reflections looks for the class 'LASRead.LASFormat.PDR<value>' and uses that as the type of the following methods
// at runtime.
// We use Activator.CreateInstance to instance this unknown type (which we assert must be an instance of IPointDataRecord)
Type t = Type.GetType("LASRead.LASFormat.PDR" + Header.PointDataRecordFormat.ToString());
IPointDataRecord initialPoint = (IPointDataRecord)Activator.CreateInstance(t);
initialPoint.ReadPoint(pdrInitial);
// Using generics to dynamically create a PDRCollection<PDR*> storage
// Using generics to dynamically create a PDRCollection<PDR[type]> storage
points = Activator.CreateInstance(typeof(PDRCollection<>).MakeGenericType(PDRType), new object[] { Header, PDRStream, initialPoint});
PointsType = Type.GetTypeArray(new object[] { points })[0];
// Grab a starting EVLR
@ -98,7 +103,7 @@ namespace LASFormat
EVLRStream = GetOffsetStream(source, EVLRSize);
EVLRHeader evlrInitial = new EVLRHeader();
evlrInitial.ReadRecords(EVLRStream);
evlrCollection = new RecordCollection(EVLRStream, EVLRStart, Header.NumberOfExtendedVLRs, evlrInitial);
evlrCollection = new RecordCollection(ref EVLRStream, EVLRStart, Header.NumberOfExtendedVLRs, evlrInitial);
// Finally, set the stream back to the starting position
source.Position = 0;
// TODO: Grab a starting Waveform
@ -141,6 +146,7 @@ namespace LASFormat
source.Read(buffer, 0, remaining);
newStream.Write(buffer, 0, remaining);
}
newStream.Position = 0;
return newStream;
}

View File

@ -335,10 +335,10 @@ namespace LASRead.LASFormat
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)
for (int i = 0; i < LegacyNumberOfPointByReturn.Length; i++)
{
sb.Append(legacyReturn);
sb.Append(" ");
uint legacyReturn = LegacyNumberOfPointByReturn[i];
sb.Append($"{i+1}: {legacyReturn} ");
}
sb.Append(Environment.NewLine);
sb.Append("X Scale Factor: " + X_scaleFactor.ToString() + Environment.NewLine);

View File

@ -6,26 +6,26 @@ namespace LASRead.LASFormat
{
/// <summary>
/// LAS Data Payloads are in the Point Data Record (PDR) format <br />
/// PDR 0-5 share the same basic substructure (which is inherited from this interface, <br />
/// PDR formats 0-5 share the same basic substructure (which is ultimately inherited from this interface) <br />
/// PDR 6-10 share a slightly different substructure (more flags)
/// </summary>
public interface IPointDataRecord
{
int X { get; set; }
int Y { get; set; }
int Z { get; set; }
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
Nullable<ushort> Intensity { get; set; }
byte ReturnNumberFlag_value { get; set; }
byte NumberOfReturnsFlag_value { get; set; }
byte ScanDirectionFlag_value { get; set; }
byte EdgeOfFlightLineFlag_value { get; set; }
byte Classification { get; set; }
sbyte ScanAngleRank { get; set; }
public ushort? Intensity { get; set; }
public byte ReturnNumberFlag_value { get; set; }
public byte NumberOfReturnsFlag_value { get; set; }
public byte ScanDirectionFlag_value { get; set; }
public byte EdgeOfFlightLineFlag_value { get; set; }
public byte Classification { get; set; }
public sbyte ScanAngleRank { get; set; }
Nullable<byte> UserData { get; set; }
ushort PointSourceID { get; set; }
bool ReadPoint(byte[] data);
public byte? UserData { get; set; }
public ushort PointSourceID { get; set; }
public bool ReadPoint(byte[] data);
/// <summary>
/// Reads the flags from the supplied byte
@ -47,33 +47,23 @@ namespace LASRead.LASFormat
class PDR0 : IPointDataRecord
{
int x;
int y;
int z;
ushort? intensity;
byte returnNumberFlag_value;
byte numberOfReturnsFlag_value;
byte scanDirectionFlag_value;
byte edgeOfFlightLineFlag_value;
byte classification;
sbyte scanAngleRank;
byte? userData;
ushort pointSourceID;
public static readonly int headerSize = 20;
public int X { get => x; set => x = value; }
public int Y { get => y; set => y = value; }
public int Z { get => z; set => z = value; }
public ushort? Intensity { get => intensity; set => intensity = value; }
public byte ReturnNumberFlag_value { get => returnNumberFlag_value; set => returnNumberFlag_value = value; }
public byte NumberOfReturnsFlag_value { get => numberOfReturnsFlag_value; set => numberOfReturnsFlag_value = value; }
public byte ScanDirectionFlag_value { get => scanDirectionFlag_value; set => scanDirectionFlag_value = value; }
public byte EdgeOfFlightLineFlag_value { get => edgeOfFlightLineFlag_value; set => edgeOfFlightLineFlag_value = value; }
public byte Classification { get => classification; set => classification = value; }
public sbyte ScanAngleRank { get => scanAngleRank; set => scanAngleRank = value; }
public byte? UserData { get => userData; set => userData = value; }
public ushort PointSourceID { get => pointSourceID; set => pointSourceID = value; }
const int headerSize = 20;
/// <summary>
/// PDRs for the X,Y,Z are 4-byte integers. They are multiplied by the scale defined in the
/// header to determine the true (potentially non-integral) coordinate of the point
/// </summary>
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public ushort? Intensity { get; set; }
public byte ReturnNumberFlag_value { get; set; }
public byte NumberOfReturnsFlag_value { get; set; }
public byte ScanDirectionFlag_value { get; set; }
public byte EdgeOfFlightLineFlag_value { get; set; }
public byte Classification { get; set; }
public sbyte ScanAngleRank { get; set; }
public byte? UserData { get; set; }
public ushort PointSourceID { get; set; }
public virtual bool ReadFlag(Tuple<byte, byte> source)
{
@ -100,15 +90,15 @@ namespace LASRead.LASFormat
{
if (DataHelpers.VerifySize(data, headerSize))
{
x = BitConverter.ToInt32(data, 0);
y = BitConverter.ToInt32(data, 4);
z = BitConverter.ToInt32(data, 8);
intensity = BitConverter.ToUInt16(data, 12);
X = BitConverter.ToInt32(data, 0);
Y = BitConverter.ToInt32(data, 4);
Z = BitConverter.ToInt32(data, 8);
Intensity = BitConverter.ToUInt16(data, 12);
ReadFlag(Tuple.Create(data[14], (byte)0));
classification = data[15];
scanAngleRank = (sbyte)data[16];
userData = data[17];
pointSourceID = BitConverter.ToUInt16(data, 18);
Classification = data[15];
ScanAngleRank = (sbyte)data[16];
UserData = data[17];
PointSourceID = BitConverter.ToUInt16(data, 18);
return true;
}
else return false;
@ -138,11 +128,11 @@ namespace LASRead.LASFormat
sb.Append("Intensity: " + Intensity.ToString() + Environment.NewLine);
sb.Append("Return Number: " + ReturnNumberFlag_value.ToString() + Environment.NewLine);
sb.Append("Number of Returns: " + NumberOfReturnsFlag_value.ToString() + Environment.NewLine);
sb.Append("Scan Direction: " + (returnNumberFlag_value == 0 ? "+" : "-") + Environment.NewLine);
sb.Append("Edge of Flight Line: " + (returnNumberFlag_value == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("Scan Direction: " + (ReturnNumberFlag_value == 0 ? "+" : "-") + Environment.NewLine);
sb.Append("Edge of Flight Line: " + (ReturnNumberFlag_value == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("Classification: " + ((Classifications)Classification) + Environment.NewLine);
sb.Append("Scan Angle Rank: " + ScanAngleRank.ToString() + Environment.NewLine);
sb.Append("User Data: " + (userData == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("User Data: " + (UserData == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("Point Data Source: " + PointSourceID.ToString() + Environment.NewLine);
return sb.ToString();
@ -167,8 +157,8 @@ namespace LASRead.LASFormat
BitConverter.GetBytes(Intensity ?? 0).CopyTo(result, 12);
MergeFlags().CopyTo(result, 14);
result[15] = Classification;
result[16] = (byte)scanAngleRank;
result[17] = userData ?? 0;
result[16] = (byte)ScanAngleRank;
result[17] = UserData ?? 0;
BitConverter.GetBytes(PointSourceID).CopyTo(result, 18);
return result;
}
@ -176,7 +166,7 @@ namespace LASRead.LASFormat
class PDR1 : PDR0
{
double GPSTime;
new public static readonly int headerSize = 28;
const int headerSize = 28;
public override bool ReadPoint(byte[] data)
{
if (DataHelpers.VerifySize(data, headerSize))
@ -216,7 +206,7 @@ namespace LASRead.LASFormat
ushort red;
ushort green;
ushort blue;
new public static readonly int headerSize = 26;
const int headerSize = 26;
public override bool ReadPoint(byte[] data)
{
if (DataHelpers.VerifySize(data, headerSize))
@ -259,7 +249,7 @@ namespace LASRead.LASFormat
ushort red;
ushort green;
ushort blue;
new public static readonly int headerSize = 34;
const int headerSize = 34;
public override bool ReadPoint(byte[] data)
{
if (DataHelpers.VerifySize(data, headerSize))
@ -308,7 +298,7 @@ namespace LASRead.LASFormat
float dy;
float dz;
new public static readonly int headerSize = 57;
const int headerSize = 57;
public override bool ReadPoint(byte[] data)
{
@ -370,7 +360,7 @@ namespace LASRead.LASFormat
float dy;
float dz;
new public static readonly int headerSize = 63;
const int headerSize = 63;
public override bool ReadPoint(byte[] data)
{
@ -423,40 +413,24 @@ namespace LASRead.LASFormat
class PDR6 : IPointDataRecord
{
int x;
int y;
int z;
ushort? intensity;
byte returnNumberFlag_value; // NB: 4 bits here
byte numberOfReturnsFlag_value; // 4
byte classificationFlag_value; // 4
byte scannerChannelFlag_value; // 2
byte scanDirectionFlag_value; // 1
byte edgeOfFlightLineFlag_value; // 1
byte classification;
short scanAngleRank;
byte? userData;
ushort pointSourceID;
double GPSTime;
public static readonly int headerSize = 30;
const int headerSize = 30;
// Inherited members
public int X { get => x; set => x = value; }
public int Y { get => y; set => y = value; }
public int Z { get => z; set => z = value; }
public ushort? Intensity { get => intensity; set => intensity = value; }
public byte ReturnNumberFlag_value { get => returnNumberFlag_value; set => returnNumberFlag_value = value; }
public byte NumberOfReturnsFlag_value { get => numberOfReturnsFlag_value; set => numberOfReturnsFlag_value = value; }
public byte ScanDirectionFlag_value { get => scanDirectionFlag_value; set => scanDirectionFlag_value = value; }
public byte EdgeOfFlightLineFlag_value { get => edgeOfFlightLineFlag_value; set => edgeOfFlightLineFlag_value = value; }
public byte Classification { get => classification; set => classification = value; }
public short ScanAngleRank { get => scanAngleRank; set => scanAngleRank = value; }
public byte? UserData { get => userData; set => userData = value; }
public ushort PointSourceID { get => pointSourceID; set => pointSourceID = value; }
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public ushort? Intensity { get; set; }
public byte ReturnNumberFlag_value { get; set; }
public byte NumberOfReturnsFlag_value { get; set; }
public byte ScanDirectionFlag_value { get; set; }
public byte EdgeOfFlightLineFlag_value { get; set; }
public byte Classification { get; set; }
public short ScanAngleRank { get; set; }
public byte? UserData { get; set; }
public ushort PointSourceID { get; set; }
// Local members
public byte ClassificationFlag_value { get => classificationFlag_value; set => classificationFlag_value = value; }
public byte ScannerChannelFlag_value { get => scannerChannelFlag_value; set => scannerChannelFlag_value = value; }
public double GPSTime1 { get => GPSTime; set => GPSTime = value; }
public byte ClassificationFlag_value { get; set; }
public byte ScannerChannelFlag_value { get; set; }
public double GPSTime1 { get; set; }
sbyte IPointDataRecord.ScanAngleRank { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
@ -477,15 +451,15 @@ namespace LASRead.LASFormat
{
if (DataHelpers.VerifySize(data, headerSize))
{
x = BitConverter.ToInt32(data, 0);
y = BitConverter.ToInt32(data, 4);
z = BitConverter.ToInt32(data, 8);
intensity = BitConverter.ToUInt16(data, 12);
X = BitConverter.ToInt32(data, 0);
Y = BitConverter.ToInt32(data, 4);
Z = BitConverter.ToInt32(data, 8);
Intensity = BitConverter.ToUInt16(data, 12);
ReadFlag(Tuple.Create(data[14], data[15]));
classification = data[16];
userData = data[17];
scanAngleRank = BitConverter.ToInt16(data, 18);
pointSourceID = BitConverter.ToUInt16(data, 20);
Classification = data[16];
UserData = data[17];
ScanAngleRank = BitConverter.ToInt16(data, 18);
PointSourceID = BitConverter.ToUInt16(data, 20);
return true;
}
else return false;
@ -506,11 +480,11 @@ namespace LASRead.LASFormat
sb.Append("Number of Returns: " + NumberOfReturnsFlag_value.ToString() + Environment.NewLine);
sb.Append("Classification Value: " + ClassificationFlag_value + Environment.NewLine);
sb.Append("Scanner Channel Value: " + ScannerChannelFlag_value + Environment.NewLine);
sb.Append("Scan Direction: " + (returnNumberFlag_value == 0 ? "+" : "-") + Environment.NewLine);
sb.Append("Edge of Flight Line: " + (returnNumberFlag_value == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("Scan Direction: " + (ReturnNumberFlag_value == 0 ? "+" : "-") + Environment.NewLine);
sb.Append("Edge of Flight Line: " + (ReturnNumberFlag_value == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("Classification: " + ((Classifications)Classification) + Environment.NewLine);
sb.Append("Scan Angle Rank: " + ScanAngleRank.ToString() + Environment.NewLine);
sb.Append("User Data: " + (userData == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("User Data: " + (UserData == 0 ? "no" : "yes") + Environment.NewLine);
sb.Append("Point Data Source: " + PointSourceID.ToString() + Environment.NewLine);
sb.Append("GPS Time: " + GPSTime1.ToString() + Environment.NewLine);
return sb.ToString();
@ -536,8 +510,8 @@ namespace LASRead.LASFormat
BitConverter.GetBytes(Intensity ?? 0).CopyTo(result, 12);
MergeFlags().CopyTo(result, 14);
result[15] = Classification;
result[16] = (byte)scanAngleRank;
result[17] = userData ?? 0;
result[16] = (byte)ScanAngleRank;
result[17] = UserData ?? 0;
BitConverter.GetBytes(PointSourceID).CopyTo(result, 18);
return result;
}
@ -549,7 +523,7 @@ namespace LASRead.LASFormat
ushort green;
ushort blue;
new public static readonly int headerSize = 36;
const int headerSize = 36;
public override bool ReadPoint(byte[] data)
{
@ -584,7 +558,7 @@ namespace LASRead.LASFormat
{
ushort nIR;
new public static readonly int headerSize = 38;
const int headerSize = 38;
public override bool ReadPoint(byte[] data)
{
@ -624,7 +598,7 @@ namespace LASRead.LASFormat
float dy;
float dz;
new public static readonly int headerSize = 59;
const int headerSize = 59;
public override bool ReadPoint(byte[] data)
{
@ -673,7 +647,7 @@ namespace LASRead.LASFormat
float dy;
float dz;
new public static readonly int headerSize = 67;
const int headerSize = 67;
public override bool ReadPoint(byte[] data)
{

View File

@ -39,6 +39,7 @@ namespace LASRead.LASFormat
StringBuilder sb = new StringBuilder();
sb.Append(header.ToString() + Environment.NewLine);
sb.Append(string.Format("Binary Data @{0}, of length {1}{2}", position + header.HeaderLength, header.HeaderLength, Environment.NewLine));
sb.Append(header.ToString());
return sb.ToString();
}
}
@ -140,7 +141,7 @@ namespace LASRead.LASFormat
sb.Append("Record ID: " + RecordID.ToString() + Environment.NewLine);
sb.Append("Record Length After Header: " + RecordID.ToString() + Environment.NewLine);
sb.Append("Description: " + new string(DataHelpers.ToCharArray(Description)) + Environment.NewLine);
return base.ToString();
return sb.ToString();
}

View File

@ -9,9 +9,9 @@ namespace LASRead.LASFormat
public class RecordCollection : IEnumerable<Record>
{
RecordEnumerator enumerator;
public RecordCollection(Stream source, ulong startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
public RecordCollection(ref Stream source, ulong startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
{
enumerator = new RecordEnumerator(source, startPosition, maxItems, firstHeader);
enumerator = new RecordEnumerator(ref source, startPosition, maxItems, firstHeader);
}
public IEnumerator<Record> GetEnumerator()
{
@ -32,7 +32,7 @@ namespace LASRead.LASFormat
uint currentCount;
uint maxCount;
public RecordEnumerator(Stream source, ulong startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
public RecordEnumerator(ref Stream source, ulong startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
{
dataSource = source;
streamStart = startPosition;

View File

@ -1,23 +0,0 @@
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();
}
}
}

View File

@ -49,7 +49,7 @@ namespace LasInteractor
private static void ReadFile(string source, bool printAll)
{
Console.WriteLine("Reading {0}", source);
FileStream fs = File.OpenRead(source);
FileStream fs = File.OpenRead(source.Trim('\"'));
Console.WriteLine("File is {0} bytes", fs.Length);
LASFile lasFile = new LASFile(fs);
/*FileStream os = File.OpenWrite("tdata.dat");