Пример #1
0
        /// <summary>
        /// Reads Geometry from the reader.
        /// </summary>
        /// <param name="reader">The reader used to read data from input stream.</param>
        /// <returns>Geometry read from the input.</returns>
        private static Geometry ReadGeometry(BinaryReader reader)
        {
            WkbGeometryType geometryType = (WkbGeometryType)reader.ReadUInt32();

            bool            is3D, isMeasured;
            WkbGeometryType basicType;

            WkbReader.GetGeometryTypeDetails(geometryType, out basicType, out is3D, out isMeasured);

            switch (basicType)
            {
            case WkbGeometryType.Point: return(WkbReader.ReadPoint(reader, is3D, isMeasured));

            case WkbGeometryType.LineString: return(WkbReader.ReadLineString(reader, is3D, isMeasured));

            case WkbGeometryType.Polygon: return(WkbReader.ReadPolygon(reader, is3D, isMeasured));

            case WkbGeometryType.MultiPoint: return(WkbReader.ReadMultiPoint(reader, is3D, isMeasured));

            case WkbGeometryType.MultiLineString: return(WkbReader.ReadMultiLineString(reader, is3D, isMeasured));

            case WkbGeometryType.MultiPolygon: return(WkbReader.ReadMultiPolygon(reader, is3D, isMeasured));

            case WkbGeometryType.GeometryCollection: return(WkbReader.ReadGeometryCollection(reader, is3D, isMeasured));

            default: throw new WkbFormatException("Unknown geometry type.");
            }
        }
Пример #2
0
        /// <summary>
        /// Parses data from the binnary array.
        /// </summary>
        /// <param name="wkb">The binary array with WKB serialized geometry.</param>
        /// <returns>Parsed geometry.</returns>
        /// <exception cref="WkbFormatException">Throws exception if wkb array does not contrains valid WKB geometry.</exception>
        public static Geometry Parse(byte[] wkb)
        {
            if (wkb == null)
            {
                throw new ArgumentNullException("wkb");
            }

            using (MemoryStream ms = new MemoryStream(wkb)) {
                using (BinaryReader reader = new BinaryReader(ms)) {
                    if (reader.PeekChar() == -1)
                    {
                        return(null);
                    }

                    try {
                        BinaryEncoding encoding = (BinaryEncoding)reader.ReadByte();
                        if (encoding == BinaryEncoding.BigEndian)
                        {
                            throw new NotSupportedException("Big endian encoding is not supprted in the current version of WkbReader.");
                        }

                        Geometry parsed = WkbReader.ReadGeometry(reader);

                        return(parsed);
                    }
                    catch (EndOfStreamException) {
                        throw new WkbFormatException("End of stream reached before end of valid WKB geometry end.");
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Reads GeometryCollection from the reader.
        /// </summary>
        /// <param name="reader">The reader used to read data from input stream.</param>
        /// <param name="is3D">bool value indicating whether GeometryCollection beeing read has Z-dimension.</param>
        /// <param name="isMeasured">bool value indicating whether GeometryCollection beeing read has M-value.</param>
        /// <returns>GeometryCollection read from the input.</returns>
        private static GeometryCollection <Geometry> ReadGeometryCollection(BinaryReader reader, bool is3D, bool isMeasured)
        {
            int pointsCount = (int)reader.ReadUInt32();

            GeometryCollection <Geometry> result = new GeometryCollection <Geometry>();

            for (int i = 0; i < pointsCount; i++)
            {
                result.Geometries.Add(WkbReader.ReadGeometry(reader));
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// Reads MultiPolygon from the reader.
        /// </summary>
        /// <param name="reader">The reader used to read data from input stream.</param>
        /// <param name="is3D">bool value indicating whether MultiPolygon beeing read has Z-dimension.</param>
        /// <param name="isMeasured">bool value indicating whether MultiPolygon beeing read has M-value.</param>
        /// <returns>MultiPolygon read from the input.</returns>
        private static MultiPolygon ReadMultiPolygon(BinaryReader reader, bool is3D, bool isMeasured)
        {
            int pointsCount = (int)reader.ReadUInt32();

            MultiPolygon result = new MultiPolygon();

            for (int i = 0; i < pointsCount; i++)
            {
                result.Geometries.Add(WkbReader.ReadPolygon(reader, is3D, isMeasured));
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// Reads a list of coordinates from the BinaryReader.
        /// </summary>
        /// <param name="reader">The reader to read from.</param>
        /// <param name="is3D">Bool value indicating whether coordinates has Z value.</param>
        /// <param name="isMeasured">Bool value indicating whether coordinates has M value.</param>
        /// <returns>Parsed Coordinate.</returns>
        private static IEnumerable <Coordinate> ReadCoordinates(BinaryReader reader, bool is3D, bool isMeasured)
        {
            int pointCount = (int)reader.ReadUInt32();

            List <Coordinate> result = new List <Coordinate>(pointCount);

            for (int i = 0; i < pointCount; i++)
            {
                result.Add(WkbReader.ReadCoordinate(reader, is3D, isMeasured));
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        /// Parses data from the binary array as given geometry type.
        /// </summary>
        /// <typeparam name="T">The Geometry type to be parsed.</typeparam>
        /// <param name="wkb">The binary array with WKB serialized geometry.</param>
        /// <returns>Parsed geometry.</returns>
        /// <exception cref="WkbFormatException">Throws exception if wkb array does not contrains valid WKB geometry of specific type.</exception>
        public static T Parse <T>(byte[] wkb) where T : Geometry
        {
            Geometry parsed = WkbReader.Parse(wkb);

            if (parsed != null)
            {
                T result = parsed as T;
                if (result == null)
                {
                    throw new WkbFormatException("Input doesn't contain valid WKB representation of the specified geometry type.");
                }

                return(result);
            }
            else
            {
                return(null);
            }
        }
Пример #7
0
        /// <summary>
        /// Reads Polygon from the reader.
        /// </summary>
        /// <param name="reader">The reader used to read data from input stream.</param>
        /// <param name="is3D">bool value indicating whether polygon beeing read has Z-dimension.</param>
        /// <param name="isMeasured">bool value indicating whether polygon beeing read has M-value.</param>
        /// <returns>Polygon read from the input.</returns>
        private static Polygon ReadPolygon(BinaryReader reader, bool is3D, bool isMeasured)
        {
            int ringsCount = (int)reader.ReadUInt32();

            if (ringsCount == 0)
            {
                return(new Polygon());
            }

            IEnumerable <Coordinate> exterior = WkbReader.ReadCoordinates(reader, is3D, isMeasured);
            Polygon result = new Polygon(new CoordinateList(exterior));

            for (int i = 1; i < ringsCount; i++)
            {
                IEnumerable <Coordinate> interior = WkbReader.ReadCoordinates(reader, is3D, isMeasured);
                result.InteriorRings.Add(new CoordinateList(interior));
            }

            return(result);
        }
Пример #8
0
        /// <summary>
        /// Read geometry in WKB format from the input.
        /// </summary>
        /// <returns>Parsed geometry or null if no other geometry is available.</returns>
        public Geometry Read()
        {
            if (_inputReader.PeekChar() == -1)
            {
                return(null);
            }

            try {
                BinaryEncoding encoding = (BinaryEncoding)_inputReader.ReadByte();
                if (encoding == BinaryEncoding.BigEndian)
                {
                    throw new NotSupportedException("Big endian encoding is not supprted in the current version of WkbReader.");
                }

                return(WkbReader.ReadGeometry(_inputReader));
            }
            catch (EndOfStreamException) {
                throw new WkbFormatException("End of stream reached before end of valid WKB geometry end.");
            }
        }
Пример #9
0
        /// <summary>
        /// Reads LineString from the reader.
        /// </summary>
        /// <param name="reader">The reader used to read data from input stream.</param>
        /// <param name="is3D">bool value indicating whether linestring beeing read has Z-dimension.</param>
        /// <param name="isMeasured">bool value indicating whether linestring beeing read has M-value.</param>
        /// <returns>Linestring read from the input.</returns>
        private static LineString ReadLineString(BinaryReader reader, bool is3D, bool isMeasured)
        {
            IEnumerable <Coordinate> coordinates = WkbReader.ReadCoordinates(reader, is3D, isMeasured);

            return(new LineString(coordinates));
        }
Пример #10
0
        /// <summary>
        /// Reads Point from the reader.
        /// </summary>
        /// <param name="reader">The reader used to read data from input stream.</param>
        /// <param name="is3D">bool value indicating whether point beeing read has Z-dimension.</param>
        /// <param name="isMeasured">bool value indicating whether point beeing read has M-value.</param>
        /// <returns>Point read from the input</returns>
        private static Point ReadPoint(BinaryReader reader, bool is3D, bool isMeasured)
        {
            Coordinate position = WkbReader.ReadCoordinate(reader, is3D, isMeasured);

            return(new Point(position));
        }