using LASRead.LASFormat; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Reflection; namespace LASRead.LASFormat { public class PDRCollection : IEnumerable where T : IPointDataRecord { readonly PDREnumerator enumerator; public PDRCollection(FileHeader fileHeader, Stream baseStream, IPointDataRecord initialRecord) { enumerator = new PDREnumerator(fileHeader, baseStream, initialRecord); } public PDREnumerator GetEnumerator() { return enumerator; } public string TEst() { return "sted"; } IEnumerator IEnumerable.GetEnumerator() { return enumerator; } } /// /// Enumerates through the points in a .las file /// public class PDREnumerator: IEnumerator 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 /// /// Gets the type of the payload /// /// 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(); } } }