Exemple #1
0
 /// <summary>
 /// Disposes the file
 /// </summary>
 public virtual void Dispose()
 {
     if (_shapePointer != IntPtr.Zero)
     {
         ShapeLib.SHPClose(_shapePointer);
     }
 }
Exemple #2
0
        private void InitializeForReading()
        {
            string currentdir = System.IO.Directory.GetCurrentDirectory();

            _dbfPointer = ShapeLib.DBFOpen(_filename, "rb");

            _data = new DataTable();

            _columns = new Dictionary <string, DBFEntry>();

            int NoOfColumns = ShapeLib.DBFGetFieldCount(_dbfPointer);

            NoOfEntries = ShapeLib.DBFGetRecordCount(_dbfPointer);

            for (int i = 0; i < NoOfColumns; i++)
            {
                DBFEntry      E    = new DBFEntry();
                StringBuilder Name = new StringBuilder();

                E._dbfType = ShapeLib.DBFGetFieldInfo(_dbfPointer, i, Name, ref E._width, ref E._decimals);
                E.name     = Name.ToString();
                E._index   = i;

                //Find the corresponding .NET Type
                switch (E._dbfType)
                {
                case ShapeLib.DBFFieldType.FTDate:
                    E._dotNetType = typeof(DateTime);
                    break;

                case ShapeLib.DBFFieldType.FTDouble:
                    E._dotNetType = typeof(double);
                    break;

                case ShapeLib.DBFFieldType.FTInteger:
                    E._dotNetType = typeof(int);
                    break;

                case ShapeLib.DBFFieldType.FTLogical:
                    E._dotNetType = typeof(bool);
                    break;

                case ShapeLib.DBFFieldType.FTString:
                    E._dotNetType = typeof(string);
                    break;

                case ShapeLib.DBFFieldType.FTInvalid:
                default:
                    E._dotNetType = typeof(object);
                    break;
                }

                _columns.Add(E.name, E);
                _data.Columns.Add(E.name, E._dotNetType);
            }
        }
Exemple #3
0
 public virtual void Dispose()
 {
     if (_data != null)
     {
         _data.Dispose();
     }
     if (_dbfPointer != IntPtr.Zero)
     {
         ShapeLib.DBFClose(_dbfPointer);
     }
 }
Exemple #4
0
        /// <summary>
        /// Writes a point
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        public void WritePointShape(double X, double Y)
        {
            IntPtr obj = ShapeLib.SHPCreateSimpleObject(ShapeLib.ShapeType.Point, 1, new double[] { X }, new double[] { Y }, null);

            // IntPtr obj = ShapeLib.SHPCreateObject(ShapeLib.ShapeType.Point, -1, 0, null, null, 1, new double[] { X }, new double[] { Y }, null, null);

            if (_shapePointer == IntPtr.Zero)
            {
                _shapePointer = ShapeLib.SHPCreate(_fileName, ShapeLib.ShapeType.Point);
            }

            ShapeLib.SHPWriteObject(_shapePointer, -1, obj);
            ShapeLib.SHPDestroyObject(obj);
            NoOfEntries++;
        }
Exemple #5
0
        public ShapeReader(string FileName)
            : base()
        {
            _fileName = FileName;
            if (!File.Exists(FileName))
            {
                throw new FileNotFoundException("Could not find the file: " + Path.GetFullPath(FileName));
            }
            _data = new DBFReader(FileName);


            // Open shapefile
            _shapePointer = ShapeLib.SHPOpen(FileName, "rb");

            int nbentities = 0;

            double[] arr1 = new double[4];
            double[] arr2 = new double[4];

            ShapeLib.SHPGetInfo(_shapePointer, ref nbentities, ref type, arr1, arr2);
            NoOfEntries = nbentities;
        }
Exemple #6
0
        /// <summary>
        /// Reads the next row of data columns corresponding to the format in the datarow
        /// </summary>
        /// <returns></returns>
        public void ReadNext(DataRow dr)
        {
            for (int j = 0; j < dr.Table.Columns.Count; j++)
            {
                DBFEntry E;
                if (_columns.TryGetValue(dr.Table.Columns[j].ColumnName, out E))
                {
                    //Find the corresponding .NET Type
                    switch (E._dbfType)
                    {
                    case ShapeLib.DBFFieldType.FTDate:
                        dr[j] = ShapeLib.DBFReadDateTimeAttribute(_dbfPointer, _recordPointer, E._index);
                        break;

                    case ShapeLib.DBFFieldType.FTDouble:
                        dr[j] = ShapeLib.DBFReadDoubleAttribute(_dbfPointer, _recordPointer, E._index);
                        break;

                    case ShapeLib.DBFFieldType.FTInteger:
                        dr[j] = ShapeLib.DBFReadIntegerAttribute(_dbfPointer, _recordPointer, E._index);
                        break;

                    case ShapeLib.DBFFieldType.FTLogical:
                        dr[j] = ShapeLib.DBFReadLogicalAttribute(_dbfPointer, _recordPointer, E._index);
                        break;

                    case ShapeLib.DBFFieldType.FTString:
                        dr[j] = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ShapeLib.DBFReadStringAttribute(_dbfPointer, _recordPointer, E._index));
                        break;

                    case ShapeLib.DBFFieldType.FTInvalid:
                    default:
                        break;
                    }
                }
            }
            _recordPointer++;
        }
Exemple #7
0
        public void Write(GeoRefData geodata)
        {
            double[]           Xs   = null;
            double[]           Ys   = null;
            ShapeLib.ShapeType type = ShapeLib.ShapeType.NullShape;

            if (geodata.Geometry is IXYPoint)
            {
                IXYPoint p = (IXYPoint)geodata.Geometry;
                type = ShapeLib.ShapeType.Point;
                Xs   = new double[] { p.X };
                Ys   = new double[] { p.Y };
            }
            else if (geodata.Geometry.GetType().Equals(typeof(XYPolyline)))
            {
                XYPolyline p = (XYPolyline)geodata.Geometry;
                type = ShapeLib.ShapeType.PolyLine;
                int npoints = p.Points.Count;

                Xs = new double[npoints];
                Ys = new double[npoints];

                for (int i = 0; i < npoints; i++)
                {
                    Xs[i] = p.Points[i].X;
                    Ys[i] = p.Points[i].Y;
                }
            }
            else if (geodata.Geometry.GetType().Equals(typeof(XYLine)))
            {
                XYLine p = (XYLine)geodata.Geometry;
                type = ShapeLib.ShapeType.PolyLine;
                int npoints = 2;

                Xs = new double[] { p.P1.X, p.P2.X };
                Ys = new double[] { p.P1.Y, p.P2.Y };;
            }

            else if (geodata.Geometry.GetType().Equals(typeof(XYPolygon)))
            {
                XYPolygon p = (XYPolygon)geodata.Geometry;
                type = ShapeLib.ShapeType.Polygon;
                int npoints = p.Points.Count;

                Xs = new double[npoints];
                Ys = new double[npoints];
                p.Points.Reverse();

                for (int i = 0; i < npoints; i++)
                {
                    Xs[i] = p.Points[i].X;
                    Ys[i] = p.Points[i].Y;
                }
                p.Points.Reverse();
            }
            else if (geodata.Geometry.GetType().Equals(typeof(MultiPartPolygon)))
            {
                MultiPartPolygon p = (MultiPartPolygon)geodata.Geometry;
                type = ShapeLib.ShapeType.Polygon;
                int npoints = p.Polygons.Sum(pol => pol.Points.Count);

                foreach (var poly in p.Polygons)
                {
                    poly.Points.Reverse();
                }

                Xs = new double[npoints];
                Ys = new double[npoints];
                int i = 0;
                foreach (var point in p.Polygons.SelectMany(pol => pol.Points))
                {
                    Xs[i] = point.X;
                    Ys[i] = point.Y;
                    i++;
                }
                foreach (var poly in p.Polygons)
                {
                    poly.Points.Reverse();
                }
            }

            if (_shapePointer == IntPtr.Zero)
            {
                _shapePointer = ShapeLib.SHPCreate(_fileName, type);
            }

            if (_shapePointer == IntPtr.Zero)
            {
                throw new Exception("Could not create: " + Path.GetFullPath(_fileName) + "\nMake sure directory exists.");
            }


            IntPtr obj;

            if (geodata.Geometry.GetType().Equals(typeof(MultiPartPolygon)))
            {
                MultiPartPolygon p = (MultiPartPolygon)geodata.Geometry;

                int[] partstarts = new int[p.Polygons.Count];
                partstarts[0] = 0;
                ShapeLib.PartType[] partype = new ShapeLib.PartType[p.Polygons.Count];
                for (int i = 0; i < partype.Count(); i++)
                {
                    partype[i] = ShapeLib.PartType.Ring;
                }

                for (int i = 1; i < partstarts.Count(); i++)
                {
                    partstarts[i] = partstarts[i - 1] + p.Polygons[i - 1].Points.Count;
                }


                obj = ShapeLib.SHPCreateObject(type, -1, partstarts.Count(), partstarts, partype, Xs.Count(), Xs, Ys, null, null);
            }
            else
            {
                obj = ShapeLib.SHPCreateSimpleObject(type, Xs.Count(), Xs, Ys, null);
            }
            ShapeLib.SHPWriteObject(_shapePointer, -1, obj);
            ShapeLib.SHPDestroyObject(obj);
            _dbf.WriteData(geodata.Data);

            NoOfEntries++;
        }
Exemple #8
0
 public Shape()
 {
     ShapeLib.LoadNativeAssemblies();
 }
Exemple #9
0
        public void Flush()
        {
            if (_rows.Count > 0)
            {
                if ((int)_dbfPointer == 0)
                {
                    CreateBDF();
                }

                //Now fill in the data
                for (int j = 0; j < _rows[0].Table.Columns.Count; j++)
                {
                    //double data
                    if (_rows[0].Table.Columns[j].DataType == typeof(double))
                    {
                        for (int i = 0; i < _rows.Count; i++)
                        {
                            if (_rows[i][j] != DBNull.Value)
                            {
                                double d = (double)_rows[i][j];
                                ShapeLib.DBFWriteDoubleAttribute(_dbfPointer, i, j, d);
                            }
                        }
                    }
                    // int data
                    else if (_rows[0].Table.Columns[j].DataType == typeof(int))
                    {
                        for (int i = 0; i < _rows.Count; i++)
                        {
                            if (_rows[i][j] != DBNull.Value)
                            {
                                ShapeLib.DBFWriteIntegerAttribute(_dbfPointer, i, j, (int)_rows[i][j]);
                            }
                        }
                    }
                    //string data
                    else if (_rows[0].Table.Columns[j].DataType == typeof(string))
                    {
                        for (int i = 0; i < _rows.Count; i++)
                        {
                            int ok = ShapeLib.DBFWriteStringAttribute(_dbfPointer, i, j, _rows[i][j].ToString());
                        }
                    }
                    //DateTime data
                    else if (_rows[0].Table.Columns[j].DataType == typeof(DateTime))
                    {
                        for (int i = 0; i < _rows.Count; i++)
                        {
                            if (_rows[i][j] != DBNull.Value)
                            {
                                ShapeLib.DBFWriteDateAttribute(_dbfPointer, i, j, (DateTime)_rows[i][j]);
                            }
                        }
                    }
                    else if (_rows[0].Table.Columns[j].DataType == typeof(bool))
                    {
                        for (int i = 0; i < _rows.Count; i++)
                        {
                            ShapeLib.DBFWriteLogicalAttribute(_dbfPointer, i, j, (bool)_rows[i][j]);
                        }
                    }
                }
                _rows.Clear();
            }
        }
Exemple #10
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);
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// Reads the next shape
        /// </summary>
        /// <returns></returns>
        public IGeometry ReadNext(int RecordNumber)
        {
            IntPtr pShape = ShapeLib.SHPReadObject(_shapePointer, RecordNumber);

            ShapeLib.SHPObject shpObject = new ShapeLib.SHPObject();
            Marshal.PtrToStructure(pShape, shpObject);
            double[] x = new double[shpObject.nVertices];
            Marshal.Copy(shpObject.padfX, x, 0, x.Length);
            double[] y = new double[shpObject.nVertices];
            Marshal.Copy(shpObject.padfY, y, 0, y.Length);
            double[] z = new double[shpObject.nVertices];
            Marshal.Copy(shpObject.padfZ, z, 0, z.Length);

            int[] partstarts = null;
            if (shpObject.nParts > 0)
            {
                partstarts = new int[shpObject.nParts];
                Marshal.Copy(shpObject.paPartStart, partstarts, 0, partstarts.Length);
            }

            ShapeLib.SHPDestroyObject(pShape);

            IGeometry geom = null;

            switch (type)
            {
            case ShapeLib.ShapeType.NullShape:
                break;

            case ShapeLib.ShapeType.MultiPoint:
            case ShapeLib.ShapeType.MultiPointZ:
            case ShapeLib.ShapeType.MultiPointM:
            case ShapeLib.ShapeType.PointM:
            case ShapeLib.ShapeType.PointZ:
            case ShapeLib.ShapeType.Point:
                geom = new XYPoint(x[0], y[0]);
                break;

            case ShapeLib.ShapeType.PolyLineM:
            case ShapeLib.ShapeType.PolyLineZ:
            case ShapeLib.ShapeType.PolyLine:
                geom = new XYPolyline();
                for (int i = 0; i < x.Length; i++)
                {
                    ((XYPolyline)geom).Points.Add(new XYPoint(x[i], y[i]));
                }
                break;

            case ShapeLib.ShapeType.PolygonM:
            case ShapeLib.ShapeType.PolygonZ:
            case ShapeLib.ShapeType.Polygon:

                if (partstarts.Count() == 1)
                {
                    geom = new XYPolygon();

                    for (int i = 0; i < x.Length; i++)
                    {
                        ((XYPolygon)geom).Points.Add(new XYPoint(x[i], y[i]));
                    }
                    ((XYPolygon)geom).Points.Reverse();
                }
                else
                {
                    geom = new MultiPartPolygon();

                    //foreach (var partstart in partstarts.Reverse())
                    //{
                    //  var poly = new XYPolygon();
                    //  for (int i = end; i > partstart; i--)
                    //    poly.Points.Add(new XYPoint(x[i], y[i]));
                    //  end = partstart;
                    //  ((MultiPartPolygon)geom).Polygons.Add(poly);
                    //}
                    for (int j = 0; j < partstarts.Count(); j++)
                    {
                        int end;
                        if (j < partstarts.Count() - 1)
                        {
                            end = partstarts[j + 1];
                        }
                        else
                        {
                            end = x.Length;
                        }

                        var poly = new XYPolygon();
                        for (int i = partstarts[j]; i < end; i++)
                        {
                            poly.Points.Add(new XYPoint(x[i], y[i]));
                        }
                        poly.Points.Reverse();
                        ((MultiPartPolygon)geom).Polygons.Add(poly);
                    }
                }
                break;

            case ShapeLib.ShapeType.MultiPatch:
                break;

            default:
                break;
            }
            return(geom);
        }
Exemple #12
0
 public DateTime ReadDate(int record, string ColumnName)
 {
     return(ShapeLib.DBFReadDateTimeAttribute(_dbfPointer, record, _columns[ColumnName]._index));
 }
Exemple #13
0
 public int ReadInt(int record, string ColumnName)
 {
     return(ShapeLib.DBFReadIntegerAttribute(_dbfPointer, record, _columns[ColumnName]._index));
 }
Exemple #14
0
 public string ReadString(int record, string ColumnName)
 {
     return(System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ShapeLib.DBFReadStringAttribute(_dbfPointer, record, _columns[ColumnName]._index)));
 }
Exemple #15
0
 public DBF(string FileName)
 {
     _filename = FileName;
     ShapeLib.LoadNativeAssemblies();
 }