internal static ShapeContent ReadFromRecord(BinaryReader reader, ShapeRecordHeader header) { var shapeType = reader.ReadInt32LittleEndian(); if (!Enum.IsDefined(typeof(ShapeType), shapeType)) { throw new ShapeRecordContentException("The Shape Type field does not contain a known type of shape."); } var content = NullShapeContent.Instance; switch ((ShapeType)shapeType) { case ShapeType.NullShape: break; case ShapeType.Point: content = PointShapeContent.ReadPointFromRecord(reader, header); break; case ShapeType.PolyLineM: content = PolyLineMShapeContent.ReadPolyLineMFromRecord(reader, header); break; default: throw new ShapeRecordContentException($"The Shape Type {shapeType} is currently not suppported."); } return(content); }
internal static ShapeContent ReadPolygonFromRecord(BinaryReader reader, ShapeRecordHeader header) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var box = new BoundingBox2D( reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian()); var numberOfParts = reader.ReadInt32LittleEndian(); var numberOfPoints = reader.ReadInt32LittleEndian(); var parts = new int[numberOfParts]; for (var partIndex = 0; partIndex < numberOfParts; partIndex++) { parts[partIndex] = reader.ReadInt32LittleEndian(); } var points = new Point[numberOfPoints]; for (var pointIndex = 0; pointIndex < numberOfPoints; pointIndex++) { points[pointIndex] = new Point( reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian()); } return(new PolygonShapeContent(new Polygon(box, parts, points))); }
internal static ShapeContent ReadPolyLineMFromRecord(BinaryReader reader, ShapeRecordHeader header) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } reader.ReadBytes(BoundingBoxByteLength.ToInt32()); // skip BoundingBox var numberOfParts = reader.ReadInt32LittleEndian(); var numberOfPoints = reader.ReadInt32LittleEndian(); var parts = new int[numberOfParts]; for (var partIndex = 0; partIndex < numberOfParts; partIndex++) { parts[partIndex] = reader.ReadInt32LittleEndian(); } var points = new PointM[numberOfPoints]; for (var pointIndex = 0; pointIndex < numberOfPoints; pointIndex++) { points[pointIndex] = new PointM( reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian()); } var contentLengthWithoutMeasures = ContentHeaderLength .Plus(ByteLength.Int32.Times(numberOfParts)) // Parts .Plus(ByteLength.Double.Times(numberOfPoints * 2)); // Points(X,Y) if (header.ContentLength > contentLengthWithoutMeasures) { reader.ReadBytes(MeasureRangeByteLength.ToInt32()); // skip MeasureRange for (var measureIndex = 0; measureIndex < numberOfPoints; measureIndex++) { points[measureIndex].ChangeMeasurement(reader.ReadDoubleLittleEndian()); // Points[i].M } } var lines = new ILineString[numberOfParts]; var toPointIndex = points.Length; for (var partIndex = numberOfParts - 1; partIndex >= 0; partIndex--) { var fromPointIndex = parts[partIndex]; lines[partIndex] = new LineString( new PointSequence(new ArraySegment <PointM>(points, fromPointIndex, toPointIndex - fromPointIndex)), GeometryConfiguration.GeometryFactory); toPointIndex = fromPointIndex; } return(new PolyLineMShapeContent(new MultiLineString(lines))); }
public static ShapeRecord Read(BinaryReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var header = ShapeRecordHeader.Read(reader); var content = ShapeContent.ReadFromRecord(reader, header); return(new ShapeRecord(header, content)); }
internal static ShapeContent ReadPointFromRecord(BinaryReader reader, ShapeRecordHeader header) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var x = reader.ReadDoubleLittleEndian(); var y = reader.ReadDoubleLittleEndian(); return(new PointShapeContent(new PointM(x, y))); }
internal static ShapeContent ReadPolyLineMFromRecord(BinaryReader reader, ShapeRecordHeader header) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var box = new BoundingBox2D( reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian()); var numberOfParts = reader.ReadInt32LittleEndian(); var numberOfPoints = reader.ReadInt32LittleEndian(); var parts = new int[numberOfParts]; for (var partIndex = 0; partIndex < numberOfParts; partIndex++) { parts[partIndex] = reader.ReadInt32LittleEndian(); } var points = new Point[numberOfPoints]; for (var pointIndex = 0; pointIndex < numberOfPoints; pointIndex++) { points[pointIndex] = new Point( reader.ReadDoubleLittleEndian(), reader.ReadDoubleLittleEndian()); } var contentLengthWithoutMeasures = ContentHeaderLength .Plus(ByteLength.Int32.Times(numberOfParts)) // Parts .Plus(ByteLength.Double.Times(numberOfPoints * 2)); // Points(X,Y) double[] measures; if (header.ContentLength > contentLengthWithoutMeasures) { measures = new double[numberOfPoints]; reader.ReadBytes(MeasureRangeByteLength.ToInt32()); // skip MeasureRange for (var measureIndex = 0; measureIndex < numberOfPoints; measureIndex++) { measures[measureIndex] = reader.ReadDoubleLittleEndian(); } } else { measures = new double[0]; } return(new PolyLineMShapeContent(new PolyLineM(box, parts, points, measures))); }
public void CanReadWrite() { var sut = new ShapeRecordHeader(_fixture.Create <RecordNumber>(), _fixture.Create <WordLength>()); 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 = ShapeRecordHeader.Read(reader); Assert.Equal(sut.RecordNumber, result.RecordNumber); Assert.Equal(sut.ContentLength, result.ContentLength); } } }
public void ReaderCanNotBeNull() { new GuardClauseAssertion(_fixture) .Verify(Methods.Select(() => ShapeRecordHeader.Read(null))); }
public ShapeRecord(ShapeRecordHeader header, ShapeContent content) { Header = header ?? throw new ArgumentNullException(nameof(header)); Content = content ?? throw new ArgumentNullException(nameof(content)); }
public bool Equals(ShapeRecordHeader other) => other != null && RecordNumber.Equals(other.RecordNumber) && ContentLength.Equals(other.ContentLength);