LAS-Read/LASRead/LASFormat/PDRCollection.cs

115 lines
3.2 KiB
C#
Raw Permalink Normal View History

2021-03-13 21:31:51 +13:00
using LASRead.LASFormat;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace LASRead.LASFormat
{
public class PDRCollection<T> : IEnumerable where T : IPointDataRecord
{
readonly PDREnumerator<T> enumerator;
public PDRCollection(FileHeader fileHeader, Stream baseStream, IPointDataRecord initialRecord)
{
enumerator = new PDREnumerator<T>(fileHeader, baseStream, initialRecord);
}
public PDREnumerator<T> GetEnumerator()
{
return enumerator;
}
public string TEst()
{
return "sted";
}
IEnumerator IEnumerable.GetEnumerator()
{
return enumerator;
}
}
/// <summary>
/// Enumerates through the points in a .las file
/// </summary>
public class PDREnumerator<T>: IEnumerator<T> where T : IPointDataRecord
{
readonly FileHeader baseHeader;
readonly Stream baseStream;
// readonly int headerSize;
//ulong endOfData;
ushort dataLength;
public PDREnumerator(FileHeader fileHeader, Stream baseStream, IPointDataRecord initialRecord)
{
Position = 0;
baseHeader = fileHeader;
this.baseStream = baseStream;
//endOfData = (fileHeader.StartOfFirstExtendedVLR == 0 ? (ulong)baseStream.Length : fileHeader.StartOfFirstExtendedVLR);
Current = (T)initialRecord;
dataLength = fileHeader.PointDataRecordLength;
}
// Stream which the data is sourced from
// Current payload
/// <summary>
/// Gets the type of the payload
/// </summary>
/// <returns></returns>
public Type GetPDRFormat()
{
return Current.GetType();
}
public int EstimateRemainder()
{
return (int)((baseStream.Length - Position) / baseHeader.PointDataRecordLength);
}
object IEnumerator.Current => Current;
public T Current { get; private set; }
public long Position { get; private set; }
public bool MoveNext()
{
if (Position + dataLength >= baseStream.Length)
{
return false;
}
else
{
long lastPos = baseStream.Position;
if (baseStream.Position != Position)
{
baseStream.Position = Position;
}
Position += dataLength;
IPointDataRecord newRecord = (IPointDataRecord)Activator.CreateInstance(Current.GetType());
byte[] nextData = new byte[dataLength];
baseStream.Position = Position;
baseStream.Read(nextData, 0, dataLength);
bool result = newRecord.ReadPoint(nextData);
Current = (T)newRecord;
baseStream.Position = lastPos;
return result;
}
}
public void Reset()
{
throw new NotImplementedException();
}
public void Dispose()
{
//throw new NotImplementedException();
}
}
}