예제 #1
0
        /// <summary>
        /// Creates a Point using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text
        /// format. The next tokens must form a &lt;Point Text&gt;.</param>
        /// <param name="factory">The factory to create the result geometry</param>
        /// <returns>Returns a Point specified by the next token in
        /// the stream.</returns>
        /// <remarks>
        /// ParseException is thrown if an unexpected token is encountered.
        /// </remarks>
        private static IPoint ReadPointText(WktStreamTokenizer tokenizer, IGeometryFactory factory)
        {
            var nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(factory.CreatePoint((Coordinate)null));
            }

            var c = new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer));

            GetNextCloser(tokenizer);

            return(factory.CreatePoint(c));
        }
예제 #2
0
        /// <summary>
        /// Creates a <see cref="GeometryCollection"/> using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer"> Tokenizer over a stream of text in Well-known Text
        /// format. The next tokens must form a GeometryCollection Text.</param>
        /// <param name="factory">The factory to create the result geometry</param>
        /// <returns>
        /// A <see cref="GeometryCollection"/> specified by the next token in the stream.</returns>
        private static IGeometryCollection ReadGeometryCollectionText(WktStreamTokenizer tokenizer, IGeometryFactory factory)
        {
            var nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken.Equals("EMPTY"))
            {
                return(factory.CreateGeometryCollection(null));
            }
            var geometries = new List <IGeometry>();

            geometries.Add(ReadGeometryTaggedText(tokenizer));
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken.Equals(","))
            {
                geometries.Add(ReadGeometryTaggedText(tokenizer));
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return(factory.CreateGeometryCollection(geometries.ToArray()));
        }
예제 #3
0
        /// <summary>
        /// Creates a Geometry using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text
        /// format. The next tokens must form a &lt;Geometry Tagged Text&gt;.</param>
        /// <returns>Returns a Geometry specified by the next token in the stream.</returns>
        /// <remarks>
        /// Exception is thrown if the coordinates used to create a Polygon
        /// shell and holes do not form closed linestrings, or if an unexpected
        /// token is encountered.
        /// </remarks>
        private static IGeometry ReadGeometryTaggedText(WktStreamTokenizer tokenizer)
        {
            tokenizer.NextToken();
            var              type = tokenizer.GetStringValue().ToUpper();
            IGeometry        geometry;
            IGeometryFactory factory = new GeometryFactory();

            switch (type)
            {
            case "POINT":
                geometry = ReadPointText(tokenizer, factory);
                break;

            case "LINESTRING":
                geometry = ReadLineStringText(tokenizer, factory);
                break;

            case "MULTIPOINT":
                geometry = ReadMultiPointText(tokenizer, factory);
                break;

            case "MULTILINESTRING":
                geometry = ReadMultiLineStringText(tokenizer, factory);
                break;

            case "POLYGON":
                geometry = ReadPolygonText(tokenizer, factory);
                break;

            case "MULTIPOLYGON":
                geometry = ReadMultiPolygonText(tokenizer, factory);
                break;

            case "GEOMETRYCOLLECTION":
                geometry = ReadGeometryCollectionText(tokenizer, factory);
                break;

            default:
                throw new Exception(String.Format(Map.NumberFormatEnUs, "Geometrytype '{0}' is not supported.",
                                                  type));
            }
            return(geometry);
        }
예제 #4
0
        /// <summary>
        /// Creates a <see cref="IMultiLineString"/> using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer">tokenizer over a stream of text in Well-known Text format. The next tokens must form a MultiLineString Text</param>
        /// <param name="factory">The factory to create the result geometry</param>
        /// <returns>a <see cref="MultiLineString"/> specified by the next token in the stream</returns>
        private static IMultiLineString ReadMultiLineStringText(WktStreamTokenizer tokenizer, IGeometryFactory factory)
        {
            string nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(factory.CreateMultiLineString(null));
            }

            var lineStrings = new List <ILineString>();

            lineStrings.Add(ReadLineStringText(tokenizer, factory));
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken == ",")
            {
                lineStrings.Add(ReadLineStringText(tokenizer, factory));
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return(factory.CreateMultiLineString(lineStrings.ToArray()));
        }
예제 #5
0
        /// <summary>
        /// Creates a Point using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text
        /// format. The next tokens must form a &lt;Point Text&gt;.</param>
        /// <param name="factory">The factory to create the result geometry</param>
        /// <returns>Returns a Point specified by the next token in
        /// the stream.</returns>
        /// <remarks>
        /// ParseException is thrown if an unexpected token is encountered.
        /// </remarks>
        private static IMultiPoint ReadMultiPointText(WktStreamTokenizer tokenizer, IGeometryFactory factory)
        {
            string nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(factory.CreateMultiPointFromCoords((Coordinate[])null));
            }

            var points = new List <Coordinate>();

            points.Add(new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer)));
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken == ",")
            {
                points.Add(new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer)));
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return(factory.CreateMultiPointFromCoords(points.ToArray()));
        }
예제 #6
0
        /// <summary>
        /// Creates a Polygon using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text
        ///  format. The next tokens must form a &lt;Polygon Text&gt;.</param>
        /// <param name="factory">The factory to create the result geometry</param>
        /// <returns>Returns a Polygon specified by the next token
        ///  in the stream</returns>
        ///  <remarks>
        ///  ParseException is thrown if the coordinates used to create the Polygon
        ///  shell and holes do not form closed linestrings, or if an unexpected
        ///  token is encountered.
        ///  </remarks>
        private static IPolygon ReadPolygonText(WktStreamTokenizer tokenizer, IGeometryFactory factory)
        {
            string nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(factory.CreatePolygon(null, null));
            }

            var exteriorRing = factory.CreateLinearRing(GetCoordinates(tokenizer));

            nextToken = GetNextCloserOrComma(tokenizer);
            var interiorRings = new List <ILinearRing>();

            while (nextToken == ",")
            {
                //Add holes
                interiorRings.Add(factory.CreateLinearRing(GetCoordinates(tokenizer)));
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return(factory.CreatePolygon(exteriorRing, interiorRings.ToArray()));
        }
예제 #7
0
        /// <summary>
        /// Creates a <see cref="IMultiPolygon"/> using the next token in the stream.
        /// </summary>
        /// <param name="tokenizer">tokenizer over a stream of text in Well-known Text
        /// format. The next tokens must form a MultiPolygon.</param>
        /// <param name="factory">The factory to create the result geometry</param>
        /// <returns>a <code>MultiPolygon</code> specified by the next token in the
        /// stream, or if if the coordinates used to create the <see cref="IPolygon"/>
        /// shells and holes do not form closed linestrings.</returns>
        private static IMultiPolygon ReadMultiPolygonText(WktStreamTokenizer tokenizer, IGeometryFactory factory)
        {
            var    polygons  = new List <IPolygon>();
            string nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(factory.CreateMultiPolygon(polygons.ToArray()));
            }

            var polygon = ReadPolygonText(tokenizer, factory);

            polygons.Add(polygon);
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken == ",")
            {
                polygon = ReadPolygonText(tokenizer, factory);
                polygons.Add(polygon);
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return(factory.CreateMultiPolygon(polygons.ToArray()));
        }
예제 #8
0
        /// <summary>
        /// Returns the next array of Coordinates in the stream.
        /// </summary>
        /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text format.  The
        /// next element returned by the stream should be "(" (the beginning of "(x1 y1, x2 y2, ..., xn yn)" or
        /// "EMPTY".</param>
        /// <returns>The next array of Coordinates in the stream, or an empty array of "EMPTY" is the
        /// next element returned by the stream.</returns>
        private static Coordinate[] GetCoordinates(WktStreamTokenizer tokenizer)
        {
            var    coordinates = new List <Coordinate>();
            string nextToken   = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(coordinates.ToArray());
            }

            var externalCoordinate = new Coordinate();

            externalCoordinate.X = GetNextNumber(tokenizer);
            externalCoordinate.Y = GetNextNumber(tokenizer);
            coordinates.Add(externalCoordinate);
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken == ",")
            {
                var internalCoordinate = new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer));
                coordinates.Add(internalCoordinate);
                nextToken = GetNextCloserOrComma(tokenizer);
            }
            return(coordinates.ToArray());
        }
예제 #9
0
 /// <summary>
 /// Creates a LineString using the next token in the stream.
 /// </summary>
 /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text format.  The next
 ///   tokens must form a LineString Text.</param>
 /// <param name="factory">The factory to create the result geometry</param>
 /// <returns>Returns a LineString specified by the next token in the stream.</returns>
 /// <remarks>
 /// ParseException is thrown if an unexpected token is encountered.
 /// </remarks>
 private static ILineString ReadLineStringText(WktStreamTokenizer tokenizer, IGeometryFactory factory)
 {
     return(factory.CreateLineString(GetCoordinates(tokenizer)));
 }
예제 #10
0
 /// <summary>
 /// Returns the next number in the stream.
 /// </summary>
 /// <param name="tokenizer">Tokenizer over a stream of text in Well-known text format.  The next token
 /// must be a number.</param>
 /// <returns>Returns the next number in the stream.</returns>
 /// <remarks>
 /// ParseException is thrown if the next token is not a number.
 /// </remarks>
 private static double GetNextNumber(WktStreamTokenizer tokenizer)
 {
     tokenizer.NextToken();
     return(tokenizer.GetNumericValue());
 }