Esempio n. 1
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 CanReadWrite()
        {
            var sut = _fixture.Create <ShapeRecord>();

            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 = ShapeRecord.Read(reader);

                    Assert.Equal(sut.Header.RecordNumber, result.Header.RecordNumber);
                    Assert.Equal(sut.Header.ContentLength, result.Header.ContentLength);
                    Assert.Equal(sut.Content.ShapeType, result.Content.ShapeType);
                    Assert.Equal(sut.Content.ContentLength, result.Content.ContentLength);
                }
            }
        }
Esempio n. 3
0
        private ShapeRecord[] GenerateManyRecords(ShapeType typeOfShape, ShapeRecordCount count)
        {
            var records = new ShapeRecord[count.ToInt32()];
            var number  = RecordNumber.Initial;

            for (var index = 0; index < records.Length; index++)
            {
                ShapeContent content = null;
                switch (typeOfShape)
                {
                case ShapeType.Point:
                    content = new PointShapeContent(_fixture.Create <PointM>());
                    break;

                case ShapeType.PolyLineM:
                    content = new PolyLineMShapeContent(_fixture.Create <MultiLineString>());
                    break;

                //case ShapeType.NullShape:
                default:
                    content = NullShapeContent.Instance;
                    break;
                }

                records[index] = content.RecordAs(number);
                number         = number.Next();
            }

            return(records);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public void Write(ShapeRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }

            record.Write(Writer);
        }
        public void Write(ShapeRecord record)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(DbaseBinaryWriter));
            }

            record.Write(Writer);
        }
        public void WriteManyHasExpectedResult()
        {
            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)));
                }
                if (current.Content is PolygonShapeContent polygonContent)
                {
                    return(result.ExpandWith(BoundingBox3D.FromGeometry(polygonContent.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)))
                {
                    //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 <ShapeRecord>();
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        actualRecords.Add(ShapeRecord.Read(reader));
                    }

                    Assert.Equal(expectedHeader, actualHeader);
                    Assert.Equal(expectedRecords, actualRecords, new ShapeRecordEqualityComparer());
                }
            }
        }
        public void WriteOneHasExpectedResult()
        {
            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);
            }
            if (expectedRecord.Content is PolygonShapeContent polygonContent)
            {
                boundingBox = BoundingBox3D.FromGeometry(polygonContent.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)))
                {
                    //Act
                    sut.Write(expectedRecord);
                }

                // Assert
                stream.Position = 0;

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

                    Assert.Equal(expectedHeader, actualHeader);
                    Assert.Equal(expectedRecord, actualRecord, new ShapeRecordEqualityComparer());
                }
            }
        }
 public void ReaderCanNotBeNull()
 {
     new GuardClauseAssertion(_fixture)
     .Verify(Methods.Select(() => ShapeRecord.Read(null)));
 }