Exemplo n.º 1
0
        private static List <GeoCoordinate> ParseCoordinates(WellKnownTextTokenizer tokenizer)
        {
            var coordinates = new List <GeoCoordinate>();

            if (IsNumberNext(tokenizer) || tokenizer.NextToken() == TokenType.LParen)
            {
                coordinates.Add(ParseCoordinate(tokenizer));
            }

            while (NextCloserOrComma(tokenizer) == TokenType.Comma)
            {
                var isOpenParen = false;

                if (IsNumberNext(tokenizer) || (isOpenParen = tokenizer.NextToken() == TokenType.LParen))
                {
                    coordinates.Add(ParseCoordinate(tokenizer));
                }

                if (isOpenParen)
                {
                    NextCloser(tokenizer);
                }
            }

            return(coordinates);
        }
Exemplo n.º 2
0
 private static void NextCloser(WellKnownTextTokenizer tokenizer)
 {
     if (tokenizer.NextToken() != TokenType.RParen)
     {
         throw new GeoWKTException(
                   $"Expected {(char)WellKnownTextTokenizer.RParen} " +
                   $"but found: {tokenizer.TokenString()}", tokenizer.LineNumber, tokenizer.Position);
     }
 }
Exemplo n.º 3
0
        private static TokenType NextCloserOrComma(WellKnownTextTokenizer tokenizer)
        {
            var token = tokenizer.NextToken();

            if (token == TokenType.Comma || token == TokenType.RParen)
            {
                return(token);
            }

            throw new GeoWKTException(
                      $"Expected {(char)WellKnownTextTokenizer.Comma} or {(char)WellKnownTextTokenizer.RParen} " +
                      $"but found: {tokenizer.TokenString()}", tokenizer.LineNumber, tokenizer.Position);
        }
Exemplo n.º 4
0
        private static TokenType NextEmptyOrOpen(WellKnownTextTokenizer tokenizer)
        {
            var token = tokenizer.NextToken();

            if (token == TokenType.LParen ||
                token == TokenType.Word && tokenizer.TokenValue.Equals(WellKnownTextTokenizer.Empty, StringComparison.OrdinalIgnoreCase))
            {
                return(token);
            }

            throw new GeoWKTException(
                      $"Expected {WellKnownTextTokenizer.Empty} or {(char)WellKnownTextTokenizer.LParen} " +
                      $"but found: {tokenizer.TokenString()}", tokenizer.LineNumber, tokenizer.Position);
        }
Exemplo n.º 5
0
        private static double NextNumber(WellKnownTextTokenizer tokenizer)
        {
            if (tokenizer.NextToken() == TokenType.Number)
            {
                if (string.Equals(tokenizer.TokenValue, WellKnownTextTokenizer.NaN, StringComparison.OrdinalIgnoreCase))
                {
                    return(double.NaN);
                }

                if (double.TryParse(
                        tokenizer.TokenValue,
                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
                        CultureInfo.InvariantCulture, out var d))
                {
                    return(d);
                }
            }

            throw new GeoWKTException(
                      $"Expected number but found: {tokenizer.TokenString()}", tokenizer.LineNumber, tokenizer.Position);
        }
Exemplo n.º 6
0
        private static IGeoShape Read(WellKnownTextTokenizer tokenizer, string shapeType)
        {
            var token = tokenizer.NextToken();

            if (token != TokenType.Word)
            {
                throw new GeoWKTException(
                          $"Expected word but found {tokenizer.TokenString()}", tokenizer.LineNumber, tokenizer.Position);
            }

            var type = tokenizer.TokenValue.ToUpperInvariant();

            if (shapeType != null && shapeType != GeoShapeType.GeometryCollection && type != shapeType)
            {
                throw new GeoWKTException($"Expected geometry type {shapeType} but found {type}");
            }

            switch (type)
            {
            case GeoShapeType.Point:
                var point = ParsePoint(tokenizer);
                point.Format = GeoShapeFormat.WellKnownText;
                return(point);

            case GeoShapeType.MultiPoint:
                var multiPoint = ParseMultiPoint(tokenizer);
                multiPoint.Format = GeoShapeFormat.WellKnownText;
                return(multiPoint);

            case GeoShapeType.LineString:
                var lineString = ParseLineString(tokenizer);
                lineString.Format = GeoShapeFormat.WellKnownText;
                return(lineString);

            case GeoShapeType.MultiLineString:
                var multiLineString = ParseMultiLineString(tokenizer);
                multiLineString.Format = GeoShapeFormat.WellKnownText;
                return(multiLineString);

            case GeoShapeType.Polygon:
                var polygon = ParsePolygon(tokenizer);
                polygon.Format = GeoShapeFormat.WellKnownText;
                return(polygon);

            case GeoShapeType.MultiPolygon:
                var multiPolygon = ParseMultiPolygon(tokenizer);
                multiPolygon.Format = GeoShapeFormat.WellKnownText;
                return(multiPolygon);

            case GeoShapeType.BoundingBox:
                var envelope = ParseBoundingBox(tokenizer);
                envelope.Format = GeoShapeFormat.WellKnownText;
                return(envelope);

            case GeoShapeType.GeometryCollection:
                var geometryCollection = ParseGeometryCollection(tokenizer);
                geometryCollection.Format = GeoShapeFormat.WellKnownText;
                return(geometryCollection);

            default:
                throw new GeoWKTException($"Unknown geometry type: {type}");
            }
        }