Example #1
0
        private void ParsePrefix(string s)
        {
            int startIndex = s.IndexOf('(');
            int endIndex = s.LastIndexOf(')');
            string prefix = startIndex == -1 ? s : s.Substring(0, startIndex);

            HasZ = false;
            HasM = false;
            CoordinateCount = 2;
            prefix = prefix.Trim().ToLower();
            if (prefix.EndsWith(" z"))
            {
                HasZ = true;
                CoordinateCount = 3;
            }
            if (prefix.EndsWith(" m"))
            {
                HasM = true;
                CoordinateCount = 3;
            }
            if (prefix.EndsWith(" zm"))
            {
                HasM = true;
                HasZ = true;
                CoordinateCount = 4;
            }

            Type = WktType.None;
            if (prefix.StartsWith("point"))
                Type = WktType.Point;
            if (prefix.StartsWith("linestring"))
                Type = WktType.LineString;
            if (prefix.StartsWith("polygon"))
                Type = WktType.Polygon;
            if (prefix.StartsWith("polyhedralsurface"))
                Type = WktType.PolyhedralSurface;
            if (prefix.StartsWith("triangle"))
                Type = WktType.Triangle;
            if (prefix.StartsWith("tin"))
                Type = WktType.Tin;
            if (prefix.StartsWith("multipoint"))
                Type = WktType.MultiPoint;
            if (prefix.StartsWith("multilinestring"))
                Type = WktType.MultiLineString;
            if (prefix.StartsWith("multipolygon"))
                Type = WktType.MultiPolygon;
            if (prefix.StartsWith("geometrycollection"))
                Type = WktType.GeometryCollection;

            Token = new WktToken(s, startIndex, endIndex);
        }
        private static IPoint BuildPoint(WktToken token, WktText wkt, ISpatialReference spatialReference)
        {
            var coordinates = token.Coords.ToArray();

            int partCount = coordinates.Length;
            if (!wkt.HasZ && !wkt.HasM && partCount != 2)
                throw new ArgumentException("Mal-formed WKT, wrong number of elements, expecting x and y");
            if (wkt.HasZ && !wkt.HasM && partCount != 3)
                throw new ArgumentException("Mal-formed WKT, wrong number of elements, expecting x y z");
            if (!wkt.HasZ && wkt.HasM && partCount != 3)
                throw new ArgumentException("Mal-formed WKT, wrong number of elements, expecting x y m");
            if (wkt.HasZ && wkt.HasM && partCount != 4)
                throw new ArgumentException("Mal-formed WKT, wrong number of elements, expecting x y z m");

            var point = (IPoint)new PointClass();
            if (spatialReference != null)
                point.SpatialReference = spatialReference;

            point.PutCoords(coordinates[0], coordinates[1]);

            if (wkt.HasZ)
                point.Z = coordinates[2];
            if (wkt.HasM && !wkt.HasZ)
                point.M = coordinates[2];
            if (wkt.HasZ && wkt.HasM)
                point.M = coordinates[3];

            MakeZmAware(point, wkt.HasZ, wkt.HasM);
            return point;
        }
        private static IRing BuildRing(WktToken token, WktText wkt, ISpatialReference spatialReference)
        {
            var ring = (IPointCollection)new RingClass();
            if (spatialReference != null)
                ((IGeometry)ring).SpatialReference = spatialReference;

            foreach (var point in token.Tokens)
            {
                ring.AddPoint(BuildPoint(point, wkt, spatialReference));
            }
            MakeZmAware((IGeometry)ring, wkt.HasZ, wkt.HasM);
            return (IRing)ring;
        }
        private static IPath BuildPath(WktToken token, WktText wkt, ISpatialReference spatialReference)
        {
            var path = (IPointCollection)new PathClass();
            if (spatialReference != null)
                ((IGeometry)path).SpatialReference = spatialReference;

            foreach (var point in token.Tokens)
            {
                path.AddPoint(BuildPoint(point, wkt, spatialReference));
            }
            var geometry = path as IGeometry;
            MakeZmAware(geometry, wkt.HasZ, wkt.HasM);
            return (IPath)path;
        }