This project has moved and is read-only. For the latest updates, please go here.

Create Tab file with FeatureDataTable

Topics: SharpMap Project, SharpMap v0.9 / v1.x, WinForms Controls
Jan 12, 2015 at 5:22 AM
Hi Team,
Can we create Tab file (for Points) from FeatureDataTable.

Jan 20, 2015 at 8:30 AM
Something along these lines:
#region CreateFromFeatureDataTable
/// <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, OgrGeometryType geometryType, 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 < OgrGeometryType.wkbPoint || geometryType > OgrGeometryType.wkbGeometryCollection)
        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 layer
    var lyr = ds.CreateLayer(table.TableName, null, geometryType, layerOptions);
    foreach (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)

        using (var feature = new OgrFeature(ld))
            var idx = -1;
            foreach (DataColumn dc in table.Columns)
                var fd = ld.GetFieldDefn(idx);
                DateTime dt;
                switch (fd.GetFieldType())
                    case OgrFieldType.OFTBinary:
                    case OgrFieldType.OFTDate:
                        dt = ((DateTime) fdr[dc]).Date;
                        feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                    case OgrFieldType.OFTDateTime:
                        dt = (DateTime) fdr[dc];
                        feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                    case OgrFieldType.OFTTime:
                        var tod = ((DateTime) fdr[dc]).TimeOfDay;
                        feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0);
                    case OgrFieldType.OFTInteger:
                        feature.SetField(idx, Convert.ToInt32(fdr[dc]));
                    case OgrFieldType.OFTIntegerList:
                        var il = GetIntegerList(fdr[dc], dc.DataType);
                        feature.SetFieldIntegerList(idx, il.Length, il);
                    case OgrFieldType.OFTReal:
                        feature.SetField(idx, Convert.ToDouble(fdr[dc]));
                    case OgrFieldType.OFTRealList:
                        var dl = GetDoubleList(fdr[dc], dc.DataType);
                        feature.SetFieldDoubleList(idx, dl.Length, dl);
                    case OgrFieldType.OFTString:
                        feature.SetField(idx, Convert.ToString(fdr[dc]));
                    case OgrFieldType.OFTStringList:
                        var sl = (string[]) fdr[dc];
                        feature.SetFieldStringList(idx, sl);



private static double[] GetDoubleList(object o, System.Type type)
    double[] res;
    if (type == typeof (float[]))
        var fa = (float[]) o;
        res = new double[fa.Length];
        for (var i = 0; i < fa.Length; i++) res[i] = fa[i];
        return res;
    if (type == typeof (double[]))
        res = (double[]) o;
        return res;
    if (type == typeof (long[]))
        var la = (long[])o;
        res = new double[la.Length];
        for (var i = 0; i < la.Length; i++) res[i] = la[i];
        return res;
    throw new InvalidOperationException("Cannot transform {0} to a list of doubles");

private static int[] GetIntegerList(object o, System.Type type)
    int[] res;
    if (type == typeof(Int16[]))
        var sa = (short[])o;
        res = new int[sa.Length];
        for (var i = 0; i < sa.Length; i++) res[i] = sa[i];
        return res;
    if (type == typeof(int[]))
        res = (int[])o;
        return res;
    throw new InvalidOperationException("Cannot transform {0} to a list of integers");

private static OgrFieldDefn GetFieldDefinition(DataColumn dc)
    var tmp = new OgrFieldDefn(dc.ColumnName, GetFieldType(dc.DataType));
    if (dc.MaxLength > 0)
    return tmp;

private static OgrFieldType GetFieldType(Type dataType)
    switch (dataType.Name)
        case "System.String":
            return OgrFieldType.OFTString;

        case "System.DateTime":
            return OgrFieldType.OFTDateTime;
            return OgrFieldType.OFTDate;
            return OgrFieldType.OFTTime;

        case "System.Byte[]":
            return OgrFieldType.OFTBinary;

        case "System.Byte":
        case "System.Int16":
        case "System.Int32":
            return OgrFieldType.OFTInteger;

        case "System.Int16[]":
        case "System.Int32[]":
            return OgrFieldType.OFTIntegerList;

        case "System.Single":
        case "System.Double":
        case "System.Int64":
            return OgrFieldType.OFTReal;

        //This should not happen
        case "System.Single[]":
        case "System.Double[]":
        case "System.Int64[]":
            return OgrFieldType.OFTRealList;
        //don't know when this is supposed to happen
        case "xxx":
            return OgrFieldType.OFTWideString;
            return OgrFieldType.OFTWideStringList;
    throw new NotSupportedException();

Marked as answer by sgoyal on 2/6/2015 at 10:00 AM