Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
        }