Exemple #1
0
        public void ReadExpectsHeaderShapeTypeToBeValid()
        {
            var shapeType = _fixture.Create <Generator <int> >().First(_ => !Enum.IsDefined(typeof(ShapeType), _));

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    writer.WriteInt32BigEndian(ShapeFileHeader.ExpectedFileCode);
                    for (var index = 0; index < 20; index++)
                    {
                        writer.Write((byte)0x0);
                    }

                    writer.WriteInt32BigEndian(_fixture.Create <int>());
                    writer.WriteInt32LittleEndian(ShapeFileHeader.ExpectedVersion);
                    writer.WriteInt32LittleEndian(shapeType);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    Assert.Throws <ShapeFileHeaderException>(
                        () => ShapeFileHeader.Read(reader)
                        );
                }
            }
        }
Exemple #2
0
        public ShapeBinaryWriter(ShapeFileHeader header, BinaryWriter writer)
        {
            Header = header ?? throw new ArgumentNullException(nameof(header));
            Writer = writer ?? throw new ArgumentNullException(nameof(writer));

            Header.Write(Writer);
        }
Exemple #3
0
        public ShapeRecordEnumeratorWithShorterStreamTests()
        {
            var fixture = new Fixture();

            fixture.CustomizeShapeRecordCount();
            fixture.CustomizeWordLength();

            var content = new PointShapeContent(new PointM(1.0, 1.0));
            var number  = RecordNumber.Initial;

            _record = content.RecordAs(number);
            var header = new ShapeFileHeader(
                ShapeFileHeader.Length.Plus(_record.Length.Times(2)),
                ShapeType.Point,
                fixture.Create <BoundingBox3D>());
            var stream = new MemoryStream();

            using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                header.Write(writer);
                _record.Write(writer);
                writer.Write(fixture.CreateMany <byte>(_record.Length.ToByteLength().ToInt32() / 2).ToArray());
                writer.Flush();
            }

            stream.Position = 100;

            _reader = new DisposableBinaryReader(stream, Encoding.UTF8, false);
            _sut    = header.CreateShapeRecordEnumerator(_reader);
        }
Exemple #4
0
        public ShapeRecordEnumeratorOverStreamWithExpectedBytesTests()
        {
            var fixture = new Fixture();

            fixture.CustomizeShapeRecordCount();
            fixture.CustomizeWordLength();

            var content = new PointShapeContent(new Point(1.0, 1.0));
            var number  = RecordNumber.Initial;

            _record1 = content.RecordAs(number);
            _record2 = content.RecordAs(number.Next());
            var header = new ShapeFileHeader(
                ShapeFileHeader.Length.Plus(_record1.Length).Plus(_record2.Length),
                ShapeType.Point,
                fixture.Create <BoundingBox3D>());
            var stream = new MemoryStream();

            using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                header.Write(writer);
                _record1.Write(writer);
                _record2.Write(writer);
                writer.Flush();
            }

            stream.Position = 100;

            _reader = new DisposableBinaryReader(stream, Encoding.UTF8, false);
            _sut    = header.CreateShapeRecordEnumerator(_reader);
        }
        public void WriteManyOnDisposedInstanceHasExpectedResult()
        {
            var shapeType       = _fixture.Create <ShapeType>();
            var recordCount     = _fixture.Create <ShapeRecordCount>();
            var expectedRecords = GenerateManyRecords(shapeType, recordCount);
            var length          = expectedRecords.Aggregate(ShapeFileHeader.Length,
                                                            (result, current) => result.Plus(current.Length));
            var boundingBox = expectedRecords.Aggregate(BoundingBox3D.Empty,
                                                        (result, current) =>
            {
                if (current.Content is PointShapeContent pointContent)
                {
                    return(result.ExpandWith(BoundingBox3D.FromGeometry(pointContent.Shape)));
                }
                if (current.Content is PolyLineMShapeContent lineContent)
                {
                    return(result.ExpandWith(BoundingBox3D.FromGeometry(lineContent.Shape)));
                }
                return(result);
            });
            var expectedHeader = new ShapeFileHeader(length, shapeType, boundingBox);

            using (var stream = new MemoryStream())
            {
                using (var sut = new ShapeBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    sut.Dispose();
                    Assert.Throws <ObjectDisposedException>(() => sut.Write(expectedRecords));
                }
            }
        }
        public void WriteOneOnDisposedInstanceHasExpectedResult()
        {
            var shapeType      = _fixture.Create <ShapeType>();
            var expectedRecord = GenerateOneRecord(shapeType);
            var length         = ShapeFileHeader.Length.Plus(expectedRecord.Length);
            var boundingBox    = BoundingBox3D.Empty;

            if (expectedRecord.Content is PointShapeContent pointContent)
            {
                boundingBox = BoundingBox3D.FromGeometry(pointContent.Shape);
            }
            if (expectedRecord.Content is PolyLineMShapeContent lineContent)
            {
                boundingBox = BoundingBox3D.FromGeometry(lineContent.Shape);
            }
            var expectedHeader = new ShapeFileHeader(length, shapeType, boundingBox);

            using (var stream = new MemoryStream())
            {
                using (var sut = new ShapeBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    sut.Dispose();
                    Assert.Throws <ObjectDisposedException>(() => sut.Write(expectedRecord));
                }
            }
        }
Exemple #7
0
        public void CanReadWrite()
        {
            var sut = new ShapeFileHeader(
                _fixture.Create <WordLength>(),
                _fixture.Create <ShapeType>(),
                _fixture.Create <BoundingBox3D>());

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    sut.Write(writer);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var result = ShapeFileHeader.Read(reader);

                    Assert.Equal(sut.FileLength, result.FileLength);
                    Assert.Equal(sut.ShapeType, result.ShapeType);
                    Assert.Equal(sut.BoundingBox, result.BoundingBox);
                }
            }
        }
 public ShapeRecordEnumerator(ShapeFileHeader header, BinaryReader reader)
 {
     _header  = header ?? throw new ArgumentNullException(nameof(header));
     _reader  = reader ?? throw new ArgumentNullException(nameof(reader));
     _current = null;
     _state   = State.Initial;
     _length  = Length;
 }
Exemple #9
0
        public void CreateShapeRecordEnumeratorThrowsWhenReaderIsNull()
        {
            var sut = new ShapeFileHeader(
                _fixture.Create <WordLength>(),
                _fixture.Create <ShapeType>(),
                _fixture.Create <BoundingBox3D>());

            Assert.Throws <ArgumentNullException>(() => sut.CreateShapeRecordEnumerator(null));
        }
Exemple #10
0
        public void CreateShapeRecordEnumeratorReturnsExpectedResult()
        {
            var sut = new ShapeFileHeader(
                _fixture.Create <WordLength>(),
                _fixture.Create <ShapeType>(),
                _fixture.Create <BoundingBox3D>());

            Assert.IsAssignableFrom <IEnumerator <ShapeRecord> >(sut.CreateShapeRecordEnumerator(_fixture.Create <BinaryReader>()));
        }
Exemple #11
0
        public void WriteManyHasExpectedResult()
        {
            var shapeType    = _fixture.Create <ShapeType>();
            var recordCount  = _fixture.Create <ShapeRecordCount>();
            var shapeRecords = GenerateManyRecords(shapeType, recordCount);
            var length       = shapeRecords.Aggregate(ShapeFileHeader.Length,
                                                      (result, current) => result.Plus(current.Length));
            var boundingBox = shapeRecords.Aggregate(BoundingBox3D.Empty,
                                                     (result, current) =>
            {
                if (current.Content is PointShapeContent pointContent)
                {
                    return(result.ExpandWith(BoundingBox3D.FromGeometry(pointContent.Shape)));
                }
                if (current.Content is PolyLineMShapeContent lineContent)
                {
                    return(result.ExpandWith(BoundingBox3D.FromGeometry(lineContent.Shape)));
                }
                return(result);
            });
            var expectedHeader  = new ShapeFileHeader(length, shapeType, boundingBox);
            var expectedRecords = new ShapeIndexRecord[shapeRecords.Length];
            var offset          = ShapeIndexRecord.InitialOffset;

            for (var index = 0; index < shapeRecords.Length; index++)
            {
                var shapeRecord = shapeRecords[index];
                expectedRecords[index] = shapeRecord.IndexAt(offset);
                offset = offset.Plus(shapeRecord.Length);
            }

            using (var stream = new MemoryStream())
            {
                using (var sut =
                           new ShapeIndexBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    //Act
                    sut.Write(expectedRecords);
                }

                // Assert
                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var actualHeader  = ShapeFileHeader.Read(reader);
                    var actualRecords = new List <ShapeIndexRecord>();
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        actualRecords.Add(ShapeIndexRecord.Read(reader));
                    }

                    Assert.Equal(expectedHeader, actualHeader);
                    Assert.Equal(expectedRecords, actualRecords, new ShapeIndexRecordEqualityComparer());
                }
            }
        }
Exemple #12
0
        public ShapeRecordEnumeratorOverStreamWithoutBytesTests()
        {
            var fixture = new Fixture();

            fixture.CustomizeShapeRecordCount();
            fixture.CustomizeWordLength();

            var header = new ShapeFileHeader(
                fixture.Create <WordLength>(),
                fixture.Create <ShapeType>(),
                fixture.Create <BoundingBox3D>());

            _reader = new DisposableBinaryReader(new MemoryStream(), Encoding.UTF8, false);
            _sut    = header.CreateShapeRecordEnumerator(_reader);
        }
        public void WriteOneRecordCanNotBeNull()
        {
            var expectedHeader = new ShapeFileHeader(
                ShapeFileHeader.Length,
                _fixture.Create <ShapeType>(),
                BoundingBox3D.Empty
                );

            using (var stream = new MemoryStream())
            {
                using (var sut = new ShapeBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    Assert.Throws <ArgumentNullException>(() => sut.Write((ShapeRecord)null));
                }
            }
        }
        public void WriteOneHasExpectedResult()
        {
            var shapeType   = _fixture.Create <ShapeType>();
            var recordCount = _fixture.Create <ShapeRecordCount>();
            var shapeRecord = GenerateOneRecord(shapeType);
            var length      = ShapeFileHeader.Length.Plus(shapeRecord.Length);
            var boundingBox = BoundingBox3D.Empty;

            if (shapeRecord.Content is PointShapeContent pointContent)
            {
                boundingBox = BoundingBox3D.FromGeometry(pointContent.Shape);
            }
            if (shapeRecord.Content is PolyLineMShapeContent lineContent)
            {
                boundingBox = BoundingBox3D.FromGeometry(lineContent.Shape);
            }
            if (shapeRecord.Content is PolygonShapeContent polygonContent)
            {
                boundingBox = BoundingBox3D.FromGeometry(polygonContent.Shape);
            }
            var expectedHeader = new ShapeFileHeader(length, shapeType, boundingBox).ForIndex(recordCount);
            var expectedRecord = shapeRecord.IndexAt(ShapeIndexRecord.InitialOffset);

            using (var stream = new MemoryStream())
            {
                using (var sut =
                           new ShapeIndexBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    //Act
                    sut.Write(expectedRecord);
                }

                // Assert
                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var actualHeader = ShapeFileHeader.Read(reader);
                    var actualRecord = ShapeIndexRecord.Read(reader);

                    Assert.Equal(expectedHeader, actualHeader);
                    Assert.Equal(expectedRecord, actualRecord, new ShapeIndexRecordEqualityComparer());
                }
            }
        }
        public void DisposeHasExpectedResult()
        {
            var expectedHeader = new ShapeFileHeader(
                ShapeFileHeader.Length,
                _fixture.Create <ShapeType>(),
                BoundingBox3D.Empty
                );

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, false))
                    using (var sut = new ShapeBinaryWriter(expectedHeader, writer))
                    {
                        sut.Dispose();
                        Assert.Throws <ObjectDisposedException>(() => writer.Write(_fixture.Create <byte>()));
                    }
            }
        }
        public void WriteManyRecordsCanNotBeNull()
        {
            var expectedHeader = new ShapeFileHeader(
                ShapeFileHeader.Length,
                _fixture.Create <ShapeType>(),
                BoundingBox3D.Empty
                ).ForIndex(new ShapeRecordCount(0));

            using (var stream = new MemoryStream())
            {
                using (var sut =
                           new ShapeIndexBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    //Act
                    Assert.Throws <ArgumentNullException>(() => sut.Write((IEnumerable <ShapeIndexRecord>)null));
                }
            }
        }
Exemple #17
0
        public void ReadExpectsHeaderToStartWithFileCode9994()
        {
            var start = _fixture.Create <Generator <int> >().First(_ => _ != ShapeFileHeader.ExpectedFileCode);

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    writer.WriteInt32BigEndian(start);

                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    Assert.Throws <ShapeFileHeaderException>(
                        () => ShapeFileHeader.Read(reader)
                        );
                }
            }
        }
Exemple #18
0
 public void ReaderCanNotBeNull()
 {
     new GuardClauseAssertion(_fixture)
     .Verify(Methods.Select(() => ShapeFileHeader.Read(null)));
 }
 public bool Equals(ShapeFileHeader other) =>
 other != null &&
 FileLength.Equals(other.FileLength) &&
 ShapeType.Equals(other.ShapeType) &&
 BoundingBox.Equals(other.BoundingBox);