Exemple #1
0
        public static List <geometry> parseWkt(String wkt, String lbl)
        {
            var retval = new List <geometry>();

            var nfi = new NumberFormatInfo();

            nfi.NumberGroupSeparator   = "";
            nfi.NumberDecimalSeparator = ".";
            var ns = NumberStyles.Float;

            wkt = wkt.Replace("\t", " ").Replace("\r\n", " ").Replace("\n\r", " ").Replace("\r", " ").Replace("\n", " ").Replace("\r\n", " ").Replace("\n\r", " ").ToUpper();
            while (wkt.Contains("  "))
            {
                wkt = wkt.Replace("  ", " ");
            }
            wkt = wkt.Trim();

            var i        = wkt.IndexOfAny(new Char[] { '(', ' ' });
            var geomType = wkt.Substring(0, i);

            wkt = wkt.Substring(i).Trim();
            if (!wkt.ToUpper().Contains("EMPTY"))
            {
                if ("POINT".Equals(geomType))
                {
                    wkt = wkt.TrimStart('(').TrimEnd(')').Trim();
                    var    xy = wkt.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    Double x, y;
                    if (xy.Length >= 2 && Double.TryParse(xy[0], ns, nfi, out x) && Double.TryParse(xy[1], ns, nfi, out y))
                    {
                        var g = new geometryPoint();
                        g.label         = lbl;
                        g.coordinate[0] = new coordinate(x, y);
                        retval.Add(g);
                    }
                }
                else if ("MULTIPOINT".Equals(geomType))
                {
                    wkt = wkt.TrimStart('(').TrimEnd(')').Trim();
                    var pts = wkt.Split(',');
                    foreach (var pt in pts)
                    {
                        var    xy = pt.Trim().TrimStart('(').TrimEnd(')').Trim().Split(' ');
                        Double x, y;
                        if (xy.Length >= 2 && Double.TryParse(xy[0], ns, nfi, out x) && Double.TryParse(xy[1], ns, nfi, out y))
                        {
                            var g = new geometryPoint();
                            g.label         = lbl;
                            g.coordinate[0] = new coordinate(x, y);
                            retval.Add(g);
                        }
                    }
                }
                else if ("LINESTRING".Equals(geomType))
                {
                    var g = new geometryPolyline();
                    g.coordinates[0] = new List <coordinate>();
                    wkt = wkt.TrimStart('(').TrimEnd(')').Trim();
                    var pts = wkt.Split(',');
                    foreach (var pt in pts)
                    {
                        var    xy = pt.Trim().Split(' ');
                        Double x, y;
                        if (xy.Length >= 2 && Double.TryParse(xy[0], ns, nfi, out x) && Double.TryParse(xy[1], ns, nfi, out y))
                        {
                            g.coordinates[0].Add(new coordinate(x, y));
                        }
                    }
                    if (g.coordinates.Count > 1)
                    {
                        g.label = lbl;
                        retval.Add(g);
                    }
                }
                else if ("MULTILINESTRING".Equals(geomType))
                {
                    wkt = wkt.TrimStart('(').TrimEnd(')').Trim();
                    var parensOpen  = new List <int>();
                    var parensClose = new List <int>();
                    int tmp         = -1;
                    while ((tmp = wkt.IndexOf('(', tmp + 1)) >= 0)
                    {
                        parensOpen.Add(tmp);
                    }
                    tmp = -1;
                    while ((tmp = wkt.IndexOf(')', tmp + 1)) >= 0)
                    {
                        parensClose.Add(tmp);
                    }
                    tmp = Math.Min(parensOpen.Count, parensClose.Count);
                    for (int j = 0; j < tmp; j++)
                    {
                        if (parensOpen[j] > parensClose[j])
                        {
                            break;
                        }
                        var pts = wkt.Substring(parensOpen[j] + 1, parensClose[j] - parensOpen[j] - 1).Trim().Split(',');
                        var g   = new geometryPolyline();
                        g.coordinates[0] = new List <coordinate>();
                        foreach (var pt in pts)
                        {
                            var    xy = pt.Trim().Split(' ');
                            Double x, y;
                            if (xy.Length >= 2 && Double.TryParse(xy[0], ns, nfi, out x) && Double.TryParse(xy[1], ns, nfi, out y))
                            {
                                g.coordinates[0].Add(new coordinate(x, y));
                            }
                        }
                        if (g.coordinates.Count > 1)
                        {
                            g.label = lbl;
                            retval.Add(g);
                        }
                    }
                }
                else if ("POLYGON".Equals(geomType))
                {
                    var g = new geometryPolygon();
                    g.coordinates[0] = new List <List <coordinate> >();
                    wkt = wkt.Substring(1, wkt.Length - 2).Trim();
                    var parensOpen  = new List <int>();
                    var parensClose = new List <int>();
                    int tmp         = -1;
                    while ((tmp + 1) < wkt.Length && (tmp = wkt.IndexOf('(', tmp + 1)) >= 0)
                    {
                        parensOpen.Add(tmp);
                    }
                    tmp = -1;
                    while ((tmp + 1) < wkt.Length && (tmp = wkt.IndexOf(')', tmp + 1)) >= 0)
                    {
                        parensClose.Add(tmp);
                    }
                    tmp = Math.Min(parensOpen.Count, parensClose.Count);
                    for (int j = 0; j < tmp; j++)
                    {
                        var cc = new List <coordinate>();
                        if (parensOpen[j] > parensClose[j])
                        {
                            break;
                        }
                        var pts = wkt.Substring(parensOpen[j] + 1, parensClose[j] - parensOpen[j] - 1).Trim().Split(',');
                        foreach (var pt in pts)
                        {
                            var    xy = pt.Trim().Split(' ');
                            Double x, y;
                            if (xy.Length >= 2 && Double.TryParse(xy[0], ns, nfi, out x) && Double.TryParse(xy[1], ns, nfi, out y))
                            {
                                cc.Add(new coordinate(x, y));
                            }
                        }
                        if (cc.Count > 2)
                        {
                            g.coordinates[0].Add(cc);
                        }
                    }
                    if (g.coordinates[0].Count > 0)
                    {
                        g.label = lbl;
                        retval.Add(g);
                    }
                }
                else if ("MULTIPOLYGON".Equals(geomType))
                {
                    wkt = wkt.Replace("( (", "((").Replace(") )", "))");
                    wkt = wkt.Substring(1, wkt.Length - 2).Trim();
                    var doubleParensOpen  = new List <int>();
                    var doubleParensClose = new List <int>();
                    int doubleTmp         = -1;
                    while ((doubleTmp + 1) < wkt.Length && (doubleTmp = wkt.IndexOf("((", doubleTmp + 1)) >= 0)
                    {
                        doubleParensOpen.Add(doubleTmp);
                    }
                    doubleTmp = -1;
                    while ((doubleTmp + 1) < wkt.Length && (doubleTmp = wkt.IndexOf("))", doubleTmp + 1)) >= 0)
                    {
                        doubleParensClose.Add(doubleTmp);
                    }
                    doubleTmp = Math.Min(doubleParensOpen.Count, doubleParensClose.Count);
                    for (int doubleJ = 0; doubleJ < doubleTmp; doubleJ++)
                    {
                        var g = new geometryPolygon();
                        g.coordinates[0] = new List <List <coordinate> >();
                        var wkt2        = wkt.Substring(1, wkt.Length - 2).Trim();
                        var parensOpen  = new List <int>();
                        var parensClose = new List <int>();
                        int tmp         = -1;
                        while ((tmp + 1) < wkt2.Length && (tmp = wkt2.IndexOf('(', tmp + 1)) >= 0)
                        {
                            parensOpen.Add(tmp);
                        }
                        tmp = -1;
                        while ((tmp + 1) < wkt2.Length && (tmp = wkt2.IndexOf(')', tmp + 1)) >= 0)
                        {
                            parensClose.Add(tmp);
                        }
                        tmp = Math.Min(parensOpen.Count, parensClose.Count);
                        for (int j = 0; j < tmp; j++)
                        {
                            var cc = new List <coordinate>();
                            if (parensOpen[j] > parensClose[j])
                            {
                                break;
                            }
                            var pts = wkt2.Substring(parensOpen[j] + 1, parensClose[j] - parensOpen[j] - 1).Trim().Split(',');
                            foreach (var pt in pts)
                            {
                                var    xy = pt.Trim().Split(' ');
                                Double x, y;
                                if (xy.Length >= 2 && Double.TryParse(xy[0], ns, nfi, out x) && Double.TryParse(xy[1], ns, nfi, out y))
                                {
                                    cc.Add(new coordinate(x, y));
                                }
                            }
                            if (cc.Count > 2)
                            {
                                g.coordinates[0].Add(cc);
                            }
                        }
                        if (g.coordinates[0].Count > 0)
                        {
                            g.label = lbl;
                            retval.Add(g);
                        }
                    }
                }
            }

            return(retval);
        }
Exemple #2
0
        public static List <geometry> parseGeometryBlock(List <String> text, String label)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberGroupSeparator   = "";
            nfi.NumberDecimalSeparator = ".";
            var ns = NumberStyles.Float;

            var retval  = new List <geometry>();
            var words   = text[0].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            var keyword = words[0].ToUpper();

            if ("NONE".Equals(keyword))
            {
                ;
            }
            else if ("POINT".Equals(keyword))
            {
                Double x, y;
                if (words.Length == 1)
                {
                    words = text[1].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    if (words.Length >= 2 && Double.TryParse(words[0], ns, nfi, out x) && Double.TryParse(words[1], ns, nfi, out y))
                    {
                        var g = new geometryPoint();
                        g.label         = label;
                        g.coordinate[0] = new coordinate(x, y);
                        retval.Add(g);
                    }
                }
                else if (words.Length >= 3 && Double.TryParse(words[1], ns, nfi, out x) && Double.TryParse(words[2], ns, nfi, out y))
                {
                    var g = new geometryPoint();
                    g.coordinate[0] = new coordinate(x, y);
                    retval.Add(g);
                }
            }
            else if ("LINE".Equals(keyword))
            {
                Double x1, y1, x2, y2;
                if (words.Length == 1)
                {
                    words = text[1].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    if (words.Length >= 4 && Double.TryParse(words[0], ns, nfi, out x1) && Double.TryParse(words[1], ns, nfi, out y1) && Double.TryParse(words[2], ns, nfi, out x2) && Double.TryParse(words[3], ns, nfi, out y2))
                    {
                        var g = new geometryPolyline();
                        g.label          = label;
                        g.coordinates[0] = new List <coordinate>();
                        g.coordinates[0].Add(new coordinate(x1, y1));
                        g.coordinates[0].Add(new coordinate(x2, y2));
                        retval.Add(g);
                    }
                }
                else if (words.Length >= 5 && Double.TryParse(words[1], ns, nfi, out x1) && Double.TryParse(words[2], ns, nfi, out y1) && Double.TryParse(words[3], ns, nfi, out x2) && Double.TryParse(words[4], ns, nfi, out y2))
                {
                    var g = new geometryPolyline();
                    g.label          = label;
                    g.coordinates[0] = new List <coordinate>();
                    g.coordinates[0].Add(new coordinate(x1, y1));
                    g.coordinates[0].Add(new coordinate(x2, y2));
                    retval.Add(g);
                }
            }
            else if ("PLINE".Equals(keyword))
            {
                Boolean hasMULTIPLE = false;
                UInt32  mSections   = 1;
                if (words.Length < 3 || !words[1].ToUpper().Equals("MULTIPLE") || !UInt32.TryParse(words[2], out mSections))
                {
                    mSections = 1;
                }
                else
                {
                    hasMULTIPLE = true;
                }
                int row = 1;
                for (UInt32 i = 0; i < mSections; i++)
                {
                    var g = new geometryPolyline();
                    g.label = label;
                    UInt32 numPts;
                    if (!hasMULTIPLE && words.Length >= 2 && UInt32.TryParse(words[1], out numPts))
                    {
                        ;
                    }
                    else if ((words = text[row].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)).Length >= 1 && UInt32.TryParse(words[0], out numPts))
                    {
                        row++;
                    }
                    else
                    {
                        numPts = 0;
                        row++;
                    }
                    if (numPts > 0)
                    {
                        g.coordinates[0] = new List <coordinate>();
                        for (UInt32 j = 0; j < numPts; j++)
                        {
                            Double x, y;
                            words = text[row].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                            if (words.Length >= 2 && Double.TryParse(words[0], ns, nfi, out x) && Double.TryParse(words[1], ns, nfi, out y))
                            {
                                g.coordinates[0].Add(new coordinate(x, y));
                            }
                            row++;
                        }
                    }
                    if (g.coordinates[0].Count > 1)
                    {
                        retval.Add(g);
                    }
                }
            }
            else if ("REGION".Equals(keyword))
            {
                UInt32 nPolygons;
                if (words.Length >= 2 && UInt32.TryParse(words[1], out nPolygons))
                {
                    int row = 1;
                    var g   = new geometryPolygon();
                    g.label = label;
                    retval.Add(g);
                    for (UInt32 i = 0; i < nPolygons; i++)
                    {
                        UInt32 numPts;
                        g.coordinates[0] = new List <List <coordinate> >();
                        words            = text[row].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        if (words.Length >= 1 && UInt32.TryParse(words[0], out numPts))
                        {
                            g.coordinates[0].Add(new List <coordinate>());
                            row++;
                            for (UInt32 j = 0; j < numPts - 1; j++)
                            {
                                Double x, y;
                                words = text[row].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                                if (words.Length >= 2 && Double.TryParse(words[0], ns, nfi, out x) && Double.TryParse(words[1], ns, nfi, out y))
                                {
                                    g.coordinates[0][g.coordinates[0].Count - 1].Add(new coordinate(x, y));
                                }
                                row++;
                            }
                        }
                        else
                        {
                            row++;
                        }
                    }
                }
            }
            else if ("TEXT".Equals(keyword))
            {
                var lbl = "";
                if (words.Length == 1)
                {
                    lbl   = text[1].Trim().Trim('\"');
                    words = text[2].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                }
                else if (words.Length >= 2)
                {
                    lbl   = text[0].Substring(4).Trim().Trim('\"');
                    words = text[1].Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                }
                Double x1, y1, x2, y2;
                if (words.Length >= 4 && Double.TryParse(words[0], ns, nfi, out x1) && Double.TryParse(words[1], ns, nfi, out y1) && Double.TryParse(words[2], ns, nfi, out x2) && Double.TryParse(words[3], ns, nfi, out y2))
                {
                    var g = new geometryPoint();
                    g.coordinate[0] = new coordinate((x2 - x1) / 2, (y2 - y1) / 2);
                    g.label         = lbl + (label == null ? "" : " / " + label);
                    retval.Add(g);
                }
            }
            return(retval);
        }