Beispiel #1
0
        /// <summary>
        /// DotSpatial to Ogr at layer level
        /// </summary>
        /// <param name="featureSet">DotSpatial IFeatureSet</param>
        /// <returns>Ogr Layer</returns>
        public static OSGeo.OGR.Layer DS2OrgLayer(DotSpatial.Data.IFeatureSet featureSet)
        {
            using (OSGeo.OGR.Driver driver = OSGeo.OGR.Ogr.GetDriverByName("Memory"))
            {
                OSGeo.OGR.DataSource  dataSource  = driver.CreateDataSource(featureSet.Name, null);
                OSGeo.OGR.Layer       layer       = dataSource.CreateLayer("Result", DS2OgrProjection(featureSet.Projection), DS2OgrGeometryType(featureSet.FeatureType), new string[] { });
                OSGeo.OGR.FeatureDefn featureDefn = DS2OgrDataTable(featureSet.DataTable);

                for (int k = 0; k < featureDefn.GetFieldCount(); k++)
                {
                    layer.CreateField(featureDefn.GetFieldDefn(k), 0);
                }

                for (int i = 0; i < featureSet.NumRows(); i++)
                {
                    DotSpatial.Data.IFeature feature    = featureSet.GetFeature(i);
                    OSGeo.OGR.Feature        ogrFeature = new OSGeo.OGR.Feature(featureDefn);
                    ogrFeature.SetGeometry(DS2OgrGeometry(feature.Geometry, featureSet));
                    for (int j = 0; j < feature.DataRow.ItemArray.Length; j++)
                    {
                        #region Set Value to Feature

                        object value = feature.DataRow.ItemArray.GetValue(j);
                        if (value is int)
                        {
                            ogrFeature.SetField(j, (int)value);
                        }
                        else if (value is double)
                        {
                            ogrFeature.SetField(j, (double)value);
                        }
                        else if (value is string)
                        {
                            ogrFeature.SetField(j, (string)value);
                        }
                        else if (value is DateTime)
                        {
                            DateTime dateTime = (DateTime)value;
                            ogrFeature.SetField(j, dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, (float)dateTime.Second, 0);
                        }

                        #endregion
                    }
                    layer.CreateFeature(ogrFeature);
                }
                dataSource.FlushCache();
                return(layer);
            }
        }
Beispiel #2
0
        public static OSGeo.OGR.Layer DS2OrgLayer(DotSpatial.Data.IFeatureSet featureSet, OSGeo.OGR.Layer layer)
        {
            using (OSGeo.OGR.FeatureDefn featureDefn = DS2OgrDataTable(featureSet.DataTable))
            {
                for (int k = 0; k < featureDefn.GetFieldCount(); k++)
                {
                    layer.CreateField(featureDefn.GetFieldDefn(k), 0);
                }

                for (int i = 0; i < featureSet.NumRows(); i++)
                {
                    DotSpatial.Data.IFeature feature = featureSet.GetFeature(i);
                    using (OSGeo.OGR.Feature ogrFeature = new OSGeo.OGR.Feature(featureDefn))
                    {
                        ogrFeature.SetGeometry(DS2OgrGeometry(feature.Geometry, featureSet));
                        for (int j = 0; j < feature.DataRow.ItemArray.Length; j++)
                        {
                            #region Set Value to Feature

                            object value = feature.DataRow.ItemArray.GetValue(j);
                            if (value is int)
                            {
                                ogrFeature.SetField(j, (int)value);
                            }
                            else if (value is double)
                            {
                                ogrFeature.SetField(j, (double)value);
                            }
                            else if (value is string)
                            {
                                ogrFeature.SetField(j, (string)value);
                            }
                            else if (value is DateTime)
                            {
                                DateTime dateTime = (DateTime)value;
                                ogrFeature.SetField(j, dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, (float)dateTime.Second, 0);
                            }

                            #endregion
                        }
                        layer.CreateFeature(ogrFeature);
                    }
                }
            }
            return(layer);
        }
        private void BUT_shptopoly_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            fd.Filter = "Shape file|*.shp";
            DialogResult result = fd.ShowDialog();
            string       file   = fd.FileName;

            ProjectionInfo pStart    = new ProjectionInfo();
            ProjectionInfo pESRIEnd  = KnownCoordinateSystems.Geographic.World.WGS1984;
            bool           reproject = false;

            if (File.Exists(file))
            {
                string prjfile = Path.GetDirectoryName(file) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + ".prj";
                if (File.Exists(prjfile))
                {
                    using (StreamReader re = File.OpenText(Path.GetDirectoryName(file) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + ".prj"))
                    {
                        pStart.ParseEsriString(re.ReadLine());

                        reproject = true;
                    }
                }

                DotSpatial.Data.IFeatureSet fs = DotSpatial.Data.FeatureSet.Open(file);

                fs.FillAttributes();

                int rows = fs.NumRows();

                DataTable dtOriginal = fs.DataTable;
                for (int row = 0; row < dtOriginal.Rows.Count; row++)
                {
                    object[] original = dtOriginal.Rows[row].ItemArray;
                }

                foreach (DataColumn col in dtOriginal.Columns)
                {
                    Console.WriteLine(col.ColumnName + " " + col.DataType.ToString());
                }

                int a = 1;

                string path = Path.GetDirectoryName(file);

                foreach (var feature in fs.Features)
                {
                    StringBuilder sb = new StringBuilder();

                    sb.Append("#Shap to Poly - Mission Planner\r\n");
                    foreach (var point in feature.Coordinates)
                    {
                        if (reproject)
                        {
                            double[] xyarray = { point.X, point.Y };
                            double[] zarray  = { point.Z };

                            Reproject.ReprojectPoints(xyarray, zarray, pStart, pESRIEnd, 0, 1);

                            point.X = xyarray[0];
                            point.Y = xyarray[1];
                            point.Z = zarray[0];
                        }

                        sb.Append(point.Y.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\t" + point.X.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\r\n");
                    }

                    log.Info("writting poly to " + path + Path.DirectorySeparatorChar + "poly-" + a + ".poly");
                    File.WriteAllText(path + Path.DirectorySeparatorChar + "poly-" + a + ".poly", sb.ToString());

                    a++;
                }
            }
        }