Esempio n. 1
0
    public static void exportSDFtoShapeFile(string inputsdfFile, string outputShapefile)
    {
        using (TextReader reader = new StreamReader(inputsdfFile))
        {
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.Trim() == "BEGIN BOUNDS:")
                {
                    FeatureSet fs = new FeatureSet(FeatureType.Polygon);
                    fs.DataTable.Columns.AddRange(new DataColumn[]
                    {
                        new DataColumn("ProfileName", typeof(string)),
                        new DataColumn("ProfileDate", typeof(string)),
                    });

                    fs.AddFid();

                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line.Trim() == "END BOUNDS:")
                        {
                            fs.SaveAs(outputShapefile, true);

                            break;
                        }
                        else if (line.Trim() == "PROFILE LIMITS:")
                        {
                            string             profileName = "";
                            bool               isDate      = false;
                            DateTime           dateTime    = DateTime.Now;
                            IList <Coordinate> coordinates = new List <Coordinate>();


                            while ((line = reader.ReadLine()) != null)
                            {
                                string[] cols;
                                double   x, y, z;

                                if (line.Trim() == "END:")
                                {
                                    Polygon p = new Polygon(coordinates);

                                    IFeature f = fs.AddFeature(p);

                                    f.DataRow.BeginEdit();

                                    f.DataRow["ProfileName"] = profileName;

                                    if (isDate)
                                    {
                                        f.DataRow["ProfileDate"] = dateTime.ToString("yyyy/MM/dd HH:mm:ss");
                                    }

                                    f.DataRow.EndEdit();

                                    break;
                                }
                                else if (line.Trim() == "POLYGON:")
                                {
                                }
                                else if ((cols = line.Trim().Split(new string[] { ":", "," }, StringSplitOptions.RemoveEmptyEntries)).Length > 1)
                                {
                                    if (cols[0].Trim() == "PROFILE ID")
                                    {
                                        profileName = cols[1].Trim();

                                        cols = profileName.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                                        int day, year;

                                        if (cols.Length == 2 && int.TryParse(cols[0].Substring(0, 2), out day) && int.TryParse(cols[0].Substring(5, 4), out year) && months.ContainsKey(cols[0].Substring(2, 3)))
                                        {
                                            isDate   = true;
                                            dateTime = new DateTime(year, months[cols[0].Substring(2, 3)], day);
                                            dateTime = dateTime.AddHours(double.Parse(cols[1].Substring(0, 2)));
                                            dateTime = dateTime.AddMinutes(double.Parse(cols[1].Substring(2, 2)));
                                        }
                                    }
                                    else if (
                                        double.TryParse(cols[0], out x) &&
                                        double.TryParse(cols[1], out y) &&
                                        double.TryParse(cols[2], out z)
                                        )
                                    {
                                        coordinates.Add(new Coordinate(x, y, z)
                                        {
                                            Z = z
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }