LAS-Read/LASRead/LASFormat/RecordCollection.cs

93 lines
3.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LASRead.LASFormat
{
public class RecordCollection : IEnumerable<Record>
{
RecordEnumerator enumerator;
public RecordCollection(ref Stream source, long startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
{
enumerator = new RecordEnumerator(ref source, startPosition, maxItems, firstHeader);
}
public IEnumerator<Record> GetEnumerator()
{
return enumerator;
}
IEnumerator IEnumerable.GetEnumerator()
{
return enumerator;
}
}
public class RecordEnumerator : IEnumerator<Record>
{
Stream dataSource;
long streamStart;
long currentPosition;
uint currentCount;
uint maxCount;
IRecordPayloadHeader evalHeader;
public RecordEnumerator(ref Stream source, long startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
{
// Recieves the stream reference, the start position of the first record header, the number of record headers
dataSource = source; // Stream
streamStart = startPosition; // start of the first header
currentPosition = startPosition; // The current position of the enumerator
currentCount = 0; // Total number of items enumerated
maxCount = maxItems; // Maximum number of items to enumerate
evalHeader = firstHeader;
//Current = new Record(firstHeader, startPosition);
}
object IEnumerator.Current => Current;
public Record Current { get; private set; }
public void Dispose()
{
dataSource = null;
streamStart = 0;
currentPosition = 0;
currentCount = 0;
maxCount = 0;
}
public bool MoveNext()
{
if (currentCount >= maxCount)
{
return false;
}
else
{
long oldPos = dataSource.Position;
// Advance the position in the stream to the beginning of the next record, after the header and data
if (Current != null)
{
currentPosition += Current.header.HeaderLength + Current.header.RecordLengthAfterHeader;
}
dataSource.Position = currentPosition;
evalHeader.ReadRecords(dataSource);
Record nextRecord = new Record(evalHeader, currentPosition);
nextRecord.ReadData(dataSource);
Current = nextRecord;
dataSource.Position = oldPos;
currentCount++;
return true;
}
}
public void Reset()
{
throw new NotImplementedException();
}
}
}