Example #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;
            }
        }
Example #2
0
            protected void ReadHeader()
            {
                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(_dbfStream);

                // how many records remain
                _readPosition = _header.HeaderLength;
            }
Example #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);
        }
Example #4
0
 public void Write(DbaseFileHeader header)
 {
     if (header == null)
     {
         throw new ArgumentNullException("header");
     }
     if (recordsWritten)
     {
         throw new InvalidOperationException("Records have already been written. Header file needs to be written first.");
     }
     headerWritten = true;
     header.WriteHeader(_writer);
     _header = header;
 }
Example #5
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);
        }
Example #6
0
        public static void WriteDummyDbf(string filename, int recordCount)
        {
            DbaseFileHeader dbfHeader = new DbaseFileHeader();

            dbfHeader.AddColumn("row", 'N', 11, 0);

            DbaseFileWriter dbfWriter = new DbaseFileWriter(filename);

            dbfWriter.Write(dbfHeader);
            for (int i = 0; i < recordCount; i++)
            {
                ArrayList columnValues = new ArrayList();
                columnValues.Add((double)i);
                dbfWriter.Write(columnValues);
            }
            dbfWriter.Close();
        }
Example #7
0
        //byte[] _wkb;
//		GeometryWKBWriter _wkbWriter;

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the ShapefileDataReader class.
        /// </summary>
        /// <param name="filename">The shapefile to read (minus the .shp extension)</param>
        ///<param name="geometryFactory">The GeometryFactory to use.</param>
        public ShapefileDataReader(string filename, GeometryFactory geometryFactory)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            if (geometryFactory == null)
            {
                throw new ArgumentNullException("geometryFactory");
            }
            _geometryFactory = geometryFactory;
            _open            = true;


            if (filename.ToLower().EndsWith(".shp"))
            {
                filename = filename.ToLower().Replace(".shp", "");
            }

            _dbfReader = new DbaseFileReader(filename + ".dbf");
            _shpReader = new ShapefileReader(filename + ".shp", geometryFactory);

            _dbfHeader   = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

//			_wkbWriter = new GeometryWKBWriter(_geometryFactory);

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields    = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            //_dbaseFields[1] = DbaseFieldDescriptor.IdField();
            for (int i = 0; i < _dbfHeader.Fields.Length; i++)
            {
                _dbaseFields[i + 1] = _dbfHeader.Fields[i];
            }

            _shpHeader = _shpReader.Header;

            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();

            _moreRecords = true;
        }
        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();
            }
        }
Example #9
0
        public static ArrayList ReadRecord(BinaryReader reader, DbaseFileHeader header)
        {
            ArrayList attrs = null;

            bool foundRecord = false;

            while (!foundRecord)
            {
                // retrieve the record length
                int tempNumFields = header.Fields.Length;

                // storage for the actual values
                attrs = new ArrayList(tempNumFields);

                // read the deleted flag
                char tempDeleted = (char)reader.ReadChar();

                // read the record length
                int tempRecordLength = 1;                 // for the deleted character just read.

                // read the Fields
                for (int j = 0; j < tempNumFields; j++)
                {
                    // find the length of the field.
                    int tempFieldLength = header.Fields[j].Length;
                    tempRecordLength = tempRecordLength + tempFieldLength;

                    // find the field type
                    char tempFieldType = header.Fields[j].DbaseType;

                    // read the data.
                    object tempObject = null;
                    switch (tempFieldType)
                    {
                    case 'L':                             // logical data type, one character (T,t,F,f,Y,y,N,n)
                        char tempChar = (char)reader.ReadByte();
                        if ((tempChar == 'T') || (tempChar == 't') || (tempChar == 'Y') || (tempChar == 'y'))
                        {
                            tempObject = true;
                        }
                        else
                        {
                            tempObject = false;
                        }
                        break;

                    case 'C':                             // character record.
                        byte[] asciiBytes = reader.ReadBytes(tempFieldLength);

                        // when reading strings - use the encoding stuff otherwise
                        // some characters cause a problem. e.g. Ñ character
                        Encoding asciiEncoding   = Encoding.Default;
                        Encoding unicodeEncoding = Encoding.Unicode;
                        byte[]   unicodeBytes    = Encoding.Convert(asciiEncoding, unicodeEncoding, asciiBytes);
                        char[]   unicodeChars    = new char[unicodeEncoding.GetCharCount(unicodeBytes, 0, unicodeBytes.Length)];
                        unicodeEncoding.GetChars(unicodeBytes, 0, unicodeBytes.Length, unicodeChars, 0);
                        string newString = new string(unicodeChars);
                        // fyrerise: strip \0s from the end if necessary
                        // in addition to our normal whitespace set
                        tempObject = newString.TrimEnd().TrimEnd('\0');
                        break;

                    case 'D':                             // date data type.
                        char[] ebuffer = new char[8];
                        ebuffer = reader.ReadChars(8);
                        string tempString = new string(ebuffer, 0, 4);
                        // fyrerise: some of our input data appears to
                        // have datetimes (read as 8 blank spaces). This
                        // was not handled by the original codebase, so I
                        // added this.
                        if (string.IsNullOrEmpty(tempString.Trim()))
                        {
                            break;
                        }
                        int year = int.Parse(tempString, System.Globalization.CultureInfo.InvariantCulture);
                        tempString = new string(ebuffer, 4, 2);
                        int month = int.Parse(tempString, System.Globalization.CultureInfo.InvariantCulture) - 1;
                        tempString = new string(ebuffer, 6, 2);
                        int day = int.Parse(tempString, System.Globalization.CultureInfo.InvariantCulture);
                        tempObject = new DateTime(year, month, day);
                        break;

                    case 'N':                             // number
                    case 'F':                             // floating point number
                        char[] fbuffer = new char[tempFieldLength];
                        fbuffer    = reader.ReadChars(tempFieldLength);
                        tempString = new string(fbuffer);
                        if (tempString == "**")
                        {
                            tempString = "0";
                            tempObject = null;
                        }
                        else
                        {
                            try
                            {
                                tempObject = Double.Parse(tempString.Trim(), System.Globalization.CultureInfo.InvariantCulture);
                            }
                            catch (FormatException)
                            {
                                // if we can't format the number, just save it as
                                // a string
                                tempObject = tempString;
                            }
                        }
                        break;

                    default:
                        throw new NotSupportedException("Do not know how to parse Field type " + tempFieldType);
                    }

                    attrs.Add(tempObject);
                }

                // ensure that the full record has been read.
                if (tempRecordLength < header.RecordLength)
                {
                    byte[] tempbuff = new byte[header.RecordLength - tempRecordLength];
                    tempbuff = reader.ReadBytes(header.RecordLength - tempRecordLength);
                }

                // add the row if it is not deleted.
                if (tempDeleted != '*')
                {
                    foundRecord = true;
                }
            }
            return(attrs);
        }
Example #10
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;
            }
        }
Example #11
0
        public static ArrayList ReadRecord(BinaryReader reader, DbaseFileHeader header)
        {
            ArrayList attrs = null;

            bool foundRecord = false;
            while (!foundRecord)
            {
                // retrieve the record length
                int tempNumFields = header.Fields.Length;

                // storage for the actual values
                attrs = new ArrayList(tempNumFields);

                // read the deleted flag
                char tempDeleted = (char)reader.ReadChar();

                // read the record length
                int tempRecordLength = 1; // for the deleted character just read.

                // read the Fields
                for (int j=0; j<tempNumFields; j++)
                {

                    // find the length of the field.
                    int tempFieldLength = header.Fields[j].Length;
                    tempRecordLength = tempRecordLength + tempFieldLength;

                    // find the field type
                    char tempFieldType = header.Fields[j].DbaseType;

                    // read the data.
                    object tempObject = null;
                    switch (tempFieldType)
                    {
                        case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
                            char tempChar = (char) reader.ReadByte();
                            if ((tempChar == 'T') || (tempChar == 't') || (tempChar == 'Y') || (tempChar == 'y'))
                            {
                                tempObject = true;
                            }
                            else
                            {
                                tempObject = false;
                            }
                            break;
                        case 'C': // character record.
                            byte[] asciiBytes = reader.ReadBytes(tempFieldLength);

                            // when reading strings - use the encoding stuff otherwise
                            // some characters cause a problem. e.g. Ñ character
                            Encoding asciiEncoding = Encoding.Default;
                            Encoding unicodeEncoding = Encoding.Unicode;
                            byte[] unicodeBytes = Encoding.Convert(asciiEncoding,unicodeEncoding,asciiBytes);
                            char[] unicodeChars = new char[unicodeEncoding.GetCharCount(unicodeBytes,0,unicodeBytes.Length)];
                            unicodeEncoding.GetChars(unicodeBytes,0,unicodeBytes.Length,unicodeChars,0);
                            string newString = new string(unicodeChars);
                            // fyrerise: strip \0s from the end if necessary
                            // in addition to our normal whitespace set
                            tempObject = newString.TrimEnd().TrimEnd('\0');
                            break;

                        case 'D': // date data type.
                            char[] ebuffer = new char[8];
                            ebuffer  = reader.ReadChars(8);
                            string tempString = new string(ebuffer, 0, 4);
                            // fyrerise: some of our input data appears to
                            // have datetimes (read as 8 blank spaces). This
                            // was not handled by the original codebase, so I
                            // added this.
                            if (string.IsNullOrEmpty(tempString.Trim()))
                                break;
                            int year = int.Parse(tempString, System.Globalization.CultureInfo.InvariantCulture);
                            tempString = new string(ebuffer, 4, 2);
                            int month = int.Parse(tempString, System.Globalization.CultureInfo.InvariantCulture) - 1;
                            tempString = new string(ebuffer, 6, 2);
                            int day = int.Parse(tempString, System.Globalization.CultureInfo.InvariantCulture);
                            tempObject = new DateTime(year, month, day);
                            break;

                        case 'N': // number
                        case 'F': // floating point number
                            char[] fbuffer = new char[tempFieldLength];
                            fbuffer = reader.ReadChars(tempFieldLength);
                            tempString = new string(fbuffer);
                            if (tempString=="**")
                            {
                                tempString="0";
                                tempObject=null;
                            }
                            else
                            {
                                try
                                {
                                    tempObject = Double.Parse(tempString.Trim(), System.Globalization.CultureInfo.InvariantCulture);
                                }
                                catch (FormatException)
                                {
                                    // if we can't format the number, just save it as
                                    // a string
                                    tempObject = tempString;
                                }
                            }
                            break;

                        default:
                            throw new NotSupportedException("Do not know how to parse Field type "+tempFieldType);
                    }

                    attrs.Add(tempObject);
                }

                // ensure that the full record has been read.
                if (tempRecordLength < header.RecordLength)
                {
                    byte[] tempbuff = new byte[header.RecordLength-tempRecordLength];
                    tempbuff = reader.ReadBytes(header.RecordLength-tempRecordLength);
                }

                // add the row if it is not deleted.
                if (tempDeleted != '*')
                {
                    foundRecord = true;
                }
            }
            return attrs;
        }
Example #12
0
            protected void ReadHeader()
            {
                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(_dbfStream);

                // how many records remain
                _readPosition = _header.HeaderLength;
            }
Example #13
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;
        }
Example #14
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();
                }
        }
Example #15
0
        /// <summary>
        /// Initializes a new instance of the ShapefileDataReader class.
        /// </summary>
        /// <param name="filename">The shapefile to read (minus the .shp extension)</param>
        ///<param name="geometryFactory">The GeometryFactory to use.</param>
        public ShapefileDataReader(string filename, GeometryFactory geometryFactory)
        {
            if (filename==null)
            {
                throw new ArgumentNullException("filename");
            }
            if (geometryFactory==null)
            {
                throw new ArgumentNullException("geometryFactory");
            }
            _geometryFactory = geometryFactory;
            _open=true;

            if (filename.ToLower().EndsWith(".shp"))
            {
                filename = filename.ToLower().Replace(".shp","");
            }

             _dbfReader = new DbaseFileReader(filename+".dbf");
             _shpReader = new ShapefileReader(filename+".shp", geometryFactory);

            _dbfHeader =  _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            //			_wkbWriter = new GeometryWKBWriter(_geometryFactory);

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            //_dbaseFields[1] = DbaseFieldDescriptor.IdField();
            for(int i=0; i < _dbfHeader.Fields.Length; i++)
            {
                _dbaseFields[i+1] = _dbfHeader.Fields[i];
            }

            _shpHeader = _shpReader.Header;

            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();

            _moreRecords = true;
        }
		public static void WriteDummyDbf(string filename, int recordCount)
		{
			DbaseFileHeader dbfHeader = new DbaseFileHeader();

			dbfHeader.AddColumn("row",'N',11,0);
			
			DbaseFileWriter dbfWriter = new DbaseFileWriter(filename);
			dbfWriter.Write(dbfHeader);
			for (int i=0; i < recordCount; i++)
			{
				ArrayList columnValues = new ArrayList();
				columnValues.Add((double)i);
				dbfWriter.Write(columnValues);
			}
			dbfWriter.Close();
		}