Ejemplo n.º 1
0
        private static string GetNextEmptyOrOpener(WktStreamTokenizer tokenizer)
        {
            tokenizer.NextToken();
            string nextWord = tokenizer.GetStringValue();

            if (nextWord == "EMPTY" || nextWord == "(")
            {
                return(nextWord);
            }

            throw new Exception("Expected 'EMPTY' or '(' but encountered '" + nextWord + "'");
        }
Ejemplo n.º 2
0
        private static Polygon ReadPolygonText(WktStreamTokenizer tokenizer)
        {
            //var pol = new Polygon(new List<LineString>
            //{
            //	new LineString(new List<GeographicPosition>()
            //	{
            //		new GeographicPosition(0,0),
            //		new GeographicPosition(1,1),
            //		new GeographicPosition(0,0)
            //	})
            //});

            var nextToken = GetNextEmptyOrOpener(tokenizer);

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

            var aneis = new List <LineString>();

            do
            {
                var ring = GetCoordinates(tokenizer);

                var max = ring.Count;

                var vertices = new List <GeographicPosition>();

                for (var i = 0; i < max; i++)
                {
                    var lng = ring[i];
                    var lat = ring[i + 1];

                    var coord = new GeographicPosition(lat, lng);

                    i++;
                    vertices.Add(coord);
                }

                aneis.Add(new LineString(vertices));

                nextToken = GetNextCloserOrComma(tokenizer);
            } while (nextToken == ",");

            var pol = new Polygon(
                new List <LineString>(aneis)
                );

            return(pol);
        }
Ejemplo n.º 3
0
        private static Point ReadPointText(WktStreamTokenizer tokenizer)
        {
            var p = new Point(new GeographicPosition(0, 0));

            var nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken == "EMPTY")
            {
                return(p);
            }

            var longitude = GetNextNumber(tokenizer);
            var latitude  = GetNextNumber(tokenizer);

            p = new Point(new GeographicPosition(latitude, longitude));

            GetNextCloser(tokenizer);

            return(p);
        }
Ejemplo n.º 4
0
        private static GeoJSONObject ReadGeometryTaggedText(WktStreamTokenizer tokenizer)
        {
            tokenizer.NextToken();
            var           type     = tokenizer.GetStringValue().ToUpper();
            GeoJSONObject geometry = null;

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

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

            /*case "MULTIPOINT":
             *      geometry = ReadMultiPointText(tokenizer);
             *      break;*/
            /*case "MULTILINESTRING":
             *      geometry = ReadMultiLineStringText(tokenizer);
             *      break;*/
            case "POLYGON":
                geometry = ReadPolygonText(tokenizer);
                break;

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

            /*case "GEOMETRYCOLLECTION":
             *      geometry = ReadGeometryCollectionText(tokenizer);
             *      break;*/
            default:
                throw new Exception(String.Format(new CultureInfo("en-US", false).NumberFormat, "Geometrytype '{0}' is not supported.",
                                                  type));
            }
            return(geometry);
        }
Ejemplo n.º 5
0
        /*private static CellGeom ReadMultiPointText(WktStreamTokenizer tokenizer)
         * {
         *      CellGeom mp = new CellGeom();
         *      mp.coordinates = new List<List<Coordinate>>();
         *
         *      List<Coordinate> ring = new List<Coordinate>();
         *
         *      string nextToken = GetNextEmptyOrOpener(tokenizer);
         *      if (nextToken == "EMPTY")
         *              return mp;
         *
         *      Coordinate coord = new Coordinate();
         *      coord.lng = Convert.ToString(GetNextNumber(tokenizer));
         *      coord.lat = Convert.ToString(GetNextNumber(tokenizer));
         *      ring.Add(coord);
         *      mp.coordinates.Add(ring);
         *
         *      nextToken = GetNextCloserOrComma(tokenizer);
         *      while (nextToken == ",")
         *      {
         *              List<Coordinate> newRing = new List<Coordinate>();
         *              Coordinate newCoord = new Coordinate();
         *              newCoord.lng = Convert.ToString(GetNextNumber(tokenizer));
         *              newCoord.lat = Convert.ToString(GetNextNumber(tokenizer));
         *              newRing.Add(newCoord);
         *              mp.coordinates.Add(newRing);
         *
         *              nextToken = GetNextCloserOrComma(tokenizer);
         *      }
         *      return mp;
         * }*/

        /*private static CellGeom ReadMultiLineStringText(WktStreamTokenizer tokenizer)
         * {
         *      CellGeom lines = new CellGeom();
         *      string nextToken = GetNextEmptyOrOpener(tokenizer);
         *      if (nextToken == "EMPTY")
         *              return lines;
         *
         *      lines.LineStrings.Add(ReadLineStringText(tokenizer));
         *      nextToken = GetNextCloserOrComma(tokenizer);
         *      while (nextToken == ",")
         *      {
         *              lines.LineStrings.Add(ReadLineStringText(tokenizer));
         *              nextToken = GetNextCloserOrComma(tokenizer);
         *      }
         *      return lines;
         * }*/

        private static GeoJSONObject ReadLineStringText(WktStreamTokenizer tokenizer)
        {
            var ring = GetCoordinates(tokenizer);

            var max = ring.Count;

            var vertices = new List <GeographicPosition>();

            for (var i = 0; i < max; i++)
            {
                var lng = ring[i];
                var lat = ring[i + 1];

                var coord = new GeographicPosition(lat, lng);

                i++;
                vertices.Add(coord);
            }

            var line = new LineString(vertices);

            return(line);
        }
Ejemplo n.º 6
0
 private static double GetNextNumber(WktStreamTokenizer tokenizer)
 {
     tokenizer.NextToken();
     return(tokenizer.GetNumericValue());
 }
Ejemplo n.º 7
0
        public static GeoJSONObject Parse(TextReader reader)
        {
            var tokenizer = new WktStreamTokenizer(reader);

            return(ReadGeometryTaggedText(tokenizer));
        }