/// <summary> /// Creates an OGR data source from a FeatureDataTable /// </summary> /// <param name="table">The name of the table</param> /// <param name="geometryType">The geometry type</param> /// <param name="driver">The driver</param> /// <param name="connection">The connection string</param> /// <param name="driverOptions">The options for the driver</param> /// <param name="layerOptions">The options for the layer</param> public static void CreateFromFeatureDataTable(FeatureDataTable table, OgcGeometryType geometryType, int srid, string driver, string connection, string[] driverOptions = null, string[] layerOptions = null) { if (table == null) throw new ArgumentNullException("table"); if (table.Rows.Count == 0) throw new ArgumentException("The table contains no rows", "table"); if (geometryType < OgcGeometryType.Point || geometryType > OgcGeometryType.MultiPolygon) throw new ArgumentException("Invalid geometry type", "geometryType"); if (string.IsNullOrWhiteSpace(driver)) throw new ArgumentException("No driver specified", "driver"); var dr = OSGeo.OGR.Ogr.GetDriverByName(driver); if (dr == null) throw new Exception(string.Format("Cannot load driver '{0}'!", driver)); //if (!dr.TestCapability("ODrCCreateDataSource")) // throw new Exception(string.Format("Driver '{0}' cannot create a data source!", driver)); // Create the data source var ds = dr.CreateDataSource(connection, driverOptions); //if (!ds.TestCapability("ODsCCreateLayer")) // throw new Exception(string.Format("Driver '{0}' cannot create a layer!", driver)); // Create the spatial reference var sr = new OSGeo.OSR.SpatialReference(string.Empty); sr.ImportFromEPSG(srid); // Create the layer var lyr = ds.CreateLayer(table.TableName, sr, (OgrGeometryType)geometryType, layerOptions); sr.Dispose(); //lyr.GetSpatialRef(); foreach (System.Data.DataColumn dc in table.Columns) { using (var fldDef = GetFieldDefinition(dc)) lyr.CreateField(fldDef, 0); } using (var ld = lyr.GetLayerDefn()) { foreach (FeatureDataRow fdr in table.Rows) { if ((int)fdr.Geometry.OgcGeometryType != (int)geometryType) continue; using (var feature = new OgrFeature(ld)) { feature.SetGeometry(OgrGeometry.CreateFromWkb(fdr.Geometry.AsBinary())); var idx = -1; foreach (System.Data.DataColumn dc in table.Columns) { idx++; var fd = ld.GetFieldDefn(idx); DateTime dt; switch (fd.GetFieldType()) { case OgrFieldType.OFTBinary: //Nothing break; case OgrFieldType.OFTDate: dt = ((DateTime)fdr[dc]).Date; feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0); break; case OgrFieldType.OFTDateTime: dt = (DateTime)fdr[dc]; feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0); break; case OgrFieldType.OFTTime: var tod = ((DateTime)fdr[dc]).TimeOfDay; feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0); break; case OgrFieldType.OFTInteger: feature.SetField(idx, Convert.ToInt32(fdr[dc])); break; case OgrFieldType.OFTIntegerList: var il = GetIntegerList(fdr[dc], dc.DataType); feature.SetFieldIntegerList(idx, il.Length, il); break; case OgrFieldType.OFTReal: feature.SetField(idx, Convert.ToDouble(fdr[dc])); break; case OgrFieldType.OFTRealList: var dl = GetDoubleList(fdr[dc], dc.DataType); feature.SetFieldDoubleList(idx, dl.Length, dl); break; case OgrFieldType.OFTString: feature.SetField(idx, Convert.ToString(fdr[dc])); break; case OgrFieldType.OFTStringList: var sl = (string[])fdr[dc]; feature.SetFieldStringList(idx, sl); break; } fd.Dispose(); } lyr.CreateFeature(feature); feature.Dispose(); } //ld.Dispose(); } } lyr.Dispose(); ds.Dispose(); dr.Dispose(); }
/// <summary> /// Creates an OGR data source from a FeatureDataTable /// </summary> /// <param name="table">The name of the table</param> /// <param name="geometryType">The geometry type</param> /// <param name="driver">The driver</param> /// <param name="connection">The connection string</param> /// <param name="driverOptions">The options for the driver</param> /// <param name="layerOptions">The options for the layer</param> public static void CreateFromFeatureDataTable(FeatureDataTable table, OgcGeometryType geometryType, int srid, string driver, string connection, string[] driverOptions = null, string[] layerOptions = null) { if (table == null) { throw new ArgumentNullException("table"); } if (table.Rows.Count == 0) { throw new ArgumentException("The table contains no rows", "table"); } if (geometryType < OgcGeometryType.Point || geometryType > OgcGeometryType.MultiPolygon) { throw new ArgumentException("Invalid geometry type", "geometryType"); } if (string.IsNullOrWhiteSpace(driver)) { throw new ArgumentException("No driver specified", "driver"); } var dr = OSGeo.OGR.Ogr.GetDriverByName(driver); if (dr == null) { throw new Exception(string.Format("Cannot load driver '{0}'!", driver)); } //if (!dr.TestCapability("ODrCCreateDataSource")) // throw new Exception(string.Format("Driver '{0}' cannot create a data source!", driver)); // Create the data source var ds = dr.CreateDataSource(connection, driverOptions); //if (!ds.TestCapability("ODsCCreateLayer")) // throw new Exception(string.Format("Driver '{0}' cannot create a layer!", driver)); // Create the spatial reference var sr = new OSGeo.OSR.SpatialReference(string.Empty); sr.ImportFromEPSG(srid); // Create the layer var lyr = ds.CreateLayer(table.TableName, sr, (OgrGeometryType)geometryType, layerOptions); sr.Dispose(); //lyr.GetSpatialRef(); foreach (System.Data.DataColumn dc in table.Columns) { using (var fldDef = GetFieldDefinition(dc)) lyr.CreateField(fldDef, 0); } using (var ld = lyr.GetLayerDefn()) { foreach (FeatureDataRow fdr in table.Rows) { if ((int)fdr.Geometry.OgcGeometryType != (int)geometryType) { continue; } using (var feature = new OgrFeature(ld)) { feature.SetGeometry(OgrGeometry.CreateFromWkb(fdr.Geometry.AsBinary())); var idx = -1; foreach (System.Data.DataColumn dc in table.Columns) { idx++; var fd = ld.GetFieldDefn(idx); DateTime dt; switch (fd.GetFieldType()) { case OgrFieldType.OFTBinary: //Nothing break; case OgrFieldType.OFTDate: dt = ((DateTime)fdr[dc]).Date; feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0); break; case OgrFieldType.OFTDateTime: dt = (DateTime)fdr[dc]; feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0); break; case OgrFieldType.OFTTime: var tod = ((DateTime)fdr[dc]).TimeOfDay; feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0); break; case OgrFieldType.OFTInteger: feature.SetField(idx, Convert.ToInt32(fdr[dc])); break; case OgrFieldType.OFTIntegerList: var il = GetIntegerList(fdr[dc], dc.DataType); feature.SetFieldIntegerList(idx, il.Length, il); break; case OgrFieldType.OFTReal: feature.SetField(idx, Convert.ToDouble(fdr[dc])); break; case OgrFieldType.OFTRealList: var dl = GetDoubleList(fdr[dc], dc.DataType); feature.SetFieldDoubleList(idx, dl.Length, dl); break; case OgrFieldType.OFTString: feature.SetField(idx, Convert.ToString(fdr[dc])); break; case OgrFieldType.OFTStringList: var sl = (string[])fdr[dc]; feature.SetFieldStringList(idx, sl); break; } fd.Dispose(); } lyr.CreateFeature(feature); feature.Dispose(); } //ld.Dispose(); } } lyr.Dispose(); ds.Dispose(); dr.Dispose(); }