Example #1
0
        private void CreateBDF()
        {
            _dbfPointer = ShapeLib.DBFCreate(_filename);

            if (_dbfPointer == IntPtr.Zero)
            {
                string file = System.IO.Path.ChangeExtension((System.IO.Path.GetFullPath(_filename)), ".dbf");
                throw new Exception("Could not create " + file + ". Is the file opened in another program?");
            }

            int DigitsBeforePoint, DigitsAfterPoint;

            int[] Precision = new int[2];

            //Make the dbf file one attribute at the time
            for (int j = 0; j < _rows[0].Table.Columns.Count; j++)
            {
                //double -attribute
                if (_rows[0].Table.Columns[j].DataType == typeof(double))
                {
                    DigitsBeforePoint = 0;
                    DigitsAfterPoint  = 0;

                    bool AllNulls = true;
                    //Loop to find precision
                    for (int i = 0; i < _rows.Count; i++)
                    {
                        //Don't try if no data
                        if (_rows[i][j] != DBNull.Value)
                        {
                            AllNulls          = false;
                            Precision         = GetPrecision((float)(double)_rows[i][j]);
                            DigitsBeforePoint = Math.Max(Precision[0], DigitsBeforePoint);
                            DigitsAfterPoint  = Math.Max(Precision[1], DigitsAfterPoint);
                        }
                    }
                    //Set default precision
                    if (AllNulls)
                    {
                        DigitsAfterPoint  = 5;
                        DigitsBeforePoint = 7;
                    }
                    ShapeLib.DBFAddField(_dbfPointer, _rows[0].Table.Columns[j].Caption, ShapeLib.DBFFieldType.FTDouble, DigitsBeforePoint + DigitsAfterPoint + 1, DigitsAfterPoint);
                }

                //String attribute
                else if (_rows[0].Table.Columns[j].DataType == typeof(string))
                {
                    int width = 10;
                    //Loop to find cell width.
                    for (int i = 0; i < _rows.Count; i++)
                    {
                        width = Math.Max(width, (_rows[i][j]).ToString().Length);
                    }

                    ShapeLib.DBFAddField(_dbfPointer, _rows[0].Table.Columns[j].Caption, ShapeLib.DBFFieldType.FTString, width, 0);
                }

                //int attribute
                else if (_rows[0].Table.Columns[j].DataType == typeof(int))
                {
                    int width = 5;
                    for (int i = 0; i < _rows.Count; i++)
                    {
                        //Loop to find precision
                        //Don't try if no data
                        if (_rows[i][j] != DBNull.Value)
                        {
                            width = Math.Max(width, GetPrecision((int)_rows[i][j]));
                        }
                    }
                    ShapeLib.DBFAddField(_dbfPointer, _rows[0].Table.Columns[j].Caption, ShapeLib.DBFFieldType.FTInteger, width, 0);
                }
                else if (_rows[0].Table.Columns[j].DataType == typeof(bool))
                {
                    ShapeLib.DBFAddField(_dbfPointer, _rows[0].Table.Columns[j].Caption, ShapeLib.DBFFieldType.FTLogical, 1, 0);
                }
                else if (_rows[0].Table.Columns[j].DataType == typeof(DateTime))
                {
                    ShapeLib.DBFAddField(_dbfPointer, _rows[0].Table.Columns[j].Caption, ShapeLib.DBFFieldType.FTDate, 8, 0);
                }
            }
        }