Spatial4n.Core.Io.NtsShapeReadWriter.ReadShapeFromBytes C# (CSharp) Method

ReadShapeFromBytes() public method

public ReadShapeFromBytes ( byte array, int offset, int length ) : Shape
array byte
offset int
length int
return Shape
        public Shape ReadShapeFromBytes(byte[] array, int offset, int length)
        {
            using (var stream = new MemoryStream(array, offset, length, false))
            using (var bytes = new BinaryReader(stream))
            {

                var type = bytes.ReadByte();
                if (type == TYPE_POINT)
                {
                    return new NtsPoint(((NtsSpatialContext)Ctx).GetGeometryFactory().CreatePoint(new Coordinate(bytes.ReadDouble(), bytes.ReadDouble())), Ctx);
                }

                if (type == TYPE_BBOX)
                {
                    return new RectangleImpl(
                        bytes.ReadDouble(), bytes.ReadDouble(),
                        bytes.ReadDouble(), bytes.ReadDouble(), Ctx);
                }

                if (type == TYPE_GEOM)
                {
                    var reader = new WKBReader(((NtsSpatialContext)Ctx).GetGeometryFactory());
                    try
                    {
                        IGeometry geom = reader.Read(stream);

                        CheckCoordinates(geom);
                        return new NtsGeometry(geom, (NtsSpatialContext)Ctx, true);
                    }
                    catch (ParseException ex)
                    {
                        throw new InvalidShapeException("error reading WKT", ex);
                    }
                    catch (IOException ex)
                    {
                        throw new InvalidShapeException("error reading WKT", ex);
                    }
                }

                throw new InvalidShapeException("shape not handled: " + type);
            }
        }