Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the DbaseFileWriter class.
        /// </summary>
        /// <param name="filename">The filename of the file create/ append.</param>
        /// <param name="append">True to append to a file. False to create a new file.</param>
        public DbaseFileWriter(string filename, bool append)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            _filename = filename;

            if (append == false)
            {
                _writer = new BinaryWriter(File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None));
                _header = new DbaseFileHeader();
            }
            else
            {
                using (BinaryReader reader = new BinaryReader(File.Open(filename, FileMode.Open)))
                {
                    _header = new DbaseFileHeader();
                    _header.ReadHeader(reader);
                    reader.Close();
                }
                _recordCount = _header.NumRecords;
                _writer      = new BinaryWriter(File.Open(filename, FileMode.Open, FileAccess.ReadWrite));
                _writer.BaseStream.Position = _writer.BaseStream.Length;
                _headerWritten = true;
            }
        }
Exemplo n.º 2
0
            protected void ReadHeader()
            {
                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(_dbfStream);

                // how many records remain
                _readPosition = _header.HeaderLength;
            }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the header information for the dbase file.
        /// </summary>
        /// <returns>DbaseFileHeader contain header and field information.</returns>
        public DbaseFileHeader GetHeader()
        {
            if (_header == null)
            {
                using (BinaryReader dbfStream = new BinaryReader(File.OpenRead(_filename)))
                {
                    _header = new DbaseFileHeader();
                    _header.ReadHeader(dbfStream);
                }
            }

            return(_header);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the header information for the dbase file.
        /// </summary>
        /// <returns>DbaseFileHeader contain header and field information.</returns>
        public DbaseFileHeader GetHeader()
        {
            if (_header == null)
            {
                FileStream   stream    = new FileStream(_filename, System.IO.FileMode.Open);
                BinaryReader dbfStream = new BinaryReader(stream);

                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(dbfStream);

                dbfStream.Close();
                stream.Close();
            }
            return(_header);
        }
Exemplo n.º 5
0
        private void Initialize()
        {
            // Cache the .dbf header
            _dbfHeader = new DbaseFileHeader();

            using (BinaryReader dbfReader = new BinaryReader(File.OpenRead(Path.Combine(Path.GetDirectoryName(_path), Path.GetFileNameWithoutExtension(_path) + ".dbf"))))
            {
                _dbfHeader.ReadHeader(dbfReader);
                _dbfHeaderOffset = dbfReader.BaseStream.Position;
            }

            // Need to make one pass over the geometries and pull out the bounding boxes
            _spatialIndex = new com.vividsolutions.jts.index.strtree.STRtree();
            _extents = new Envelope();

            using (BigEndianBinaryReader shpReader = new BigEndianBinaryReader(File.OpenRead(_path)))
            using (ShapefileIndexReader shxReader = new ShapefileIndexReader(Path.Combine(Path.GetDirectoryName(_path), Path.GetFileNameWithoutExtension(_path) + ".shx")))
            {
                // Get the shape type
                _type = new ShapefileHeader(shpReader).ShapeType;

                while (shxReader.Read())
                {
                    int offset = shxReader.GetOffest();
                    int length = shxReader.GetLength();

                    // Move to the start of geometry
                    shpReader.BaseStream.Position = offset * 2;

                    double xMin;
                    double yMin;
                    double xMax;
                    double yMax;

                    int recordNumber = shpReader.ReadIntBE();
                    int contentLength = shpReader.ReadIntBE();

                    // Read shape type
                    int type = shpReader.ReadInt32();

                    if (type != 1)
                    {
                        xMin = shpReader.ReadDouble();
                        yMin = shpReader.ReadDouble();
                        xMax = shpReader.ReadDouble();
                        yMax = shpReader.ReadDouble();
                    }
                    else
                    {
                        // Point - read x and y
                        xMin = shpReader.ReadDouble();
                        yMin = shpReader.ReadDouble();
                        xMax = yMin;
                        yMax = yMin;
                    }

                    // Build the envelope
                    Envelope extents = new Envelope(xMin, xMax, yMin, yMax);

                    // Add to total extents
                    _extents.expandToInclude(extents);

                    // Insert the index of the record into the spatial index
                    _spatialIndex.insert(extents, new ShapefileRecordPointer(recordNumber, offset * 2, contentLength, (int)_dbfHeaderOffset + (_dbfHeader.RecordLength * (recordNumber - 1))));
                }

                // Build the index once
                _spatialIndex.build();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the DbaseFileWriter class.
        /// </summary>
        /// <param name="filename">The filename of the file create/ append.</param>
        /// <param name="append">True to append to a file. False to create a new file.</param>
        public DbaseFileWriter(string filename,bool append)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            _filename = filename;

            if (append == false)
            {
                _writer = new BinaryWriter(File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None));
                _header = new DbaseFileHeader();
            }
            else
            {
                using (BinaryReader reader = new BinaryReader(File.Open(filename, FileMode.Open)))
                {
                    _header = new DbaseFileHeader();
                    _header.ReadHeader(reader);
                    reader.Close();
                }
                _recordCount = _header.NumRecords;
                _writer = new BinaryWriter(File.Open(filename, FileMode.Open, FileAccess.ReadWrite));
                _writer.BaseStream.Position = _writer.BaseStream.Length;
                _headerWritten = true;
            }
        }
Exemplo n.º 7
0
            protected void ReadHeader()
            {
                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(_dbfStream);

                // how many records remain
                _readPosition = _header.HeaderLength;
            }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the header information for the dbase file.
        /// </summary>
        /// <returns>DbaseFileHeader contain header and field information.</returns>
        public DbaseFileHeader GetHeader()
        {
            if (_header == null)
            {
                using (BinaryReader dbfStream = new BinaryReader(File.OpenRead(_filename)))
                {
                    _header = new DbaseFileHeader();
                    _header.ReadHeader(dbfStream);
                }
            }

            return _header;
        }
Exemplo n.º 9
0
        private void Initialize()
        {
            // Cache the .dbf header
            _dbfHeader = new DbaseFileHeader();

            using (BinaryReader dbfReader = new BinaryReader(File.OpenRead(Path.Combine(Path.GetDirectoryName(_path), Path.GetFileNameWithoutExtension(_path) + ".dbf"))))
            {
                _dbfHeader.ReadHeader(dbfReader);
                _dbfHeaderOffset = dbfReader.BaseStream.Position;
            }

            // Need to make one pass over the geometries and pull out the bounding boxes
            _spatialIndex = new com.vividsolutions.jts.index.strtree.STRtree();
            _extents      = new Envelope();

            using (BigEndianBinaryReader shpReader = new BigEndianBinaryReader(File.OpenRead(_path)))
                using (ShapefileIndexReader shxReader = new ShapefileIndexReader(Path.Combine(Path.GetDirectoryName(_path), Path.GetFileNameWithoutExtension(_path) + ".shx")))
                {
                    // Get the shape type
                    _type = new ShapefileHeader(shpReader).ShapeType;

                    while (shxReader.Read())
                    {
                        int offset = shxReader.GetOffest();
                        int length = shxReader.GetLength();

                        // Move to the start of geometry
                        shpReader.BaseStream.Position = offset * 2;

                        double xMin;
                        double yMin;
                        double xMax;
                        double yMax;

                        int recordNumber  = shpReader.ReadIntBE();
                        int contentLength = shpReader.ReadIntBE();

                        // Read shape type
                        int type = shpReader.ReadInt32();

                        if (type != 1)
                        {
                            xMin = shpReader.ReadDouble();
                            yMin = shpReader.ReadDouble();
                            xMax = shpReader.ReadDouble();
                            yMax = shpReader.ReadDouble();
                        }
                        else
                        {
                            // Point - read x and y
                            xMin = shpReader.ReadDouble();
                            yMin = shpReader.ReadDouble();
                            xMax = yMin;
                            yMax = yMin;
                        }

                        // Build the envelope
                        Envelope extents = new Envelope(xMin, xMax, yMin, yMax);

                        // Add to total extents
                        _extents.expandToInclude(extents);

                        // Insert the index of the record into the spatial index
                        _spatialIndex.insert(extents, new ShapefileRecordPointer(recordNumber, offset * 2, contentLength, (int)_dbfHeaderOffset + (_dbfHeader.RecordLength * (recordNumber - 1))));
                    }

                    // Build the index once
                    _spatialIndex.build();
                }
        }