LAS-Read/LASRead/LASFormat/RecordCollection.cs

93 lines
3.0 KiB
C#
Raw Permalink Normal View History

2021-03-13 21:31:51 +13:00
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;
2021-11-17 16:34:33 +13:00
public RecordCollection(ref Stream source, long startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
2021-03-13 21:31:51 +13:00
{
enumerator = new RecordEnumerator(ref source, startPosition, maxItems, firstHeader);
2021-03-13 21:31:51 +13:00
}
public IEnumerator<Record> GetEnumerator()
{
return enumerator;
}
IEnumerator IEnumerable.GetEnumerator()
{
return enumerator;
}
}
public class RecordEnumerator : IEnumerator<Record>
{
Stream dataSource;
2021-11-17 16:34:33 +13:00
long streamStart;
long currentPosition;
2021-03-13 21:31:51 +13:00
uint currentCount;
uint maxCount;
2021-11-17 16:34:33 +13:00
IRecordPayloadHeader evalHeader;
2021-03-13 21:31:51 +13:00
2021-11-17 16:34:33 +13:00
public RecordEnumerator(ref Stream source, long startPosition, uint maxItems, IRecordPayloadHeader firstHeader)
2021-03-13 21:31:51 +13:00
{
2021-11-17 16:34:33 +13:00
// 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);
2021-03-13 21:31:51 +13:00
}
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;
2021-11-17 16:34:33 +13:00
// 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);
2021-03-13 21:31:51 +13:00
Current = nextRecord;
dataSource.Position = oldPos;
currentCount++;
return true;
}
}
public void Reset()
{
throw new NotImplementedException();
}
}
}