/// <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, IGeometryFactory 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",String.Empty); _dbfReader = new DbaseFileReader(filename + ".dbf"); _shpReader = new ShapefileReader(filename + ".shp", geometryFactory); _dbfHeader = _dbfReader.GetHeader(); _recordCount = _dbfHeader.NumRecords; // 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(); 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; }
/// <summary> /// /// </summary> protected void ReadHeader() { _header = new DbaseFileHeader(); // read the header _header.ReadHeader(_dbfStream); // how many records remain _readPosition = _header.HeaderLength; }
/// <summary> /// /// </summary> /// <param name="header"></param> public void UpdateHeader(DbaseFileHeader header) { headerWritten = true; int pos = (int)_writer.BaseStream.Position; _writer.Seek(0, SeekOrigin.Begin); header.WriteHeader(_writer); _writer.Seek(pos, SeekOrigin.Begin); _header = header; }
/// <summary> /// /// </summary> /// <param name="header"></param> 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; }
/// <summary> /// Gets the header from a dbf file. /// </summary> /// <param name="dbfFile">The DBF file.</param> /// <returns></returns> public static DbaseFileHeader GetHeader(string dbfFile) { if (!File.Exists(dbfFile)) { throw new FileNotFoundException(dbfFile + " not found"); } DbaseFileHeader header = new DbaseFileHeader(); header.ReadHeader(new BinaryReader(new FileStream(dbfFile, FileMode.Open, FileAccess.Read, FileShare.Read))); return(header); }
/// <summary> /// /// </summary> /// <param name="header"></param> 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; }
/// <summary> /// /// </summary> /// <param name="filename"></param> /// <param name="recordCount"></param> public static void WriteDummyDbf(string filename, int recordCount) { DbaseFileHeader dbfHeader = new DbaseFileHeader(); dbfHeader.AddColumn("Description",'C', 20, 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(); }
/// <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, FileAccess.Read, FileShare.Read); BinaryReader dbfStream = new BinaryReader(stream); _header = new DbaseFileHeader(); // read the header _header.ReadHeader(dbfStream); dbfStream.Close(); stream.Close(); } return(_header); }
/// <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, FileAccess.Read, FileShare.Read); BinaryReader dbfStream = new BinaryReader(stream); _header = new DbaseFileHeader(); // read the header _header.ReadHeader(dbfStream); dbfStream.Close(); stream.Close(); } return _header; }
/// <summary> /// /// </summary> /// <param name="filename"></param> /// <param name="recordCount"></param> public static void WriteDummyDbf(string filename, int recordCount) { DbaseFileHeader dbfHeader = new DbaseFileHeader(); dbfHeader.AddColumn("Description", 'C', 20, 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(); }
/// <summary> /// Gets the stub header. /// </summary> /// <param name="feature">The feature.</param> /// <param name="count">The count.</param> /// <returns></returns> public static DbaseFileHeader GetHeader(Feature feature, int count) { IAttributesTable attribs = feature.Attributes; string[] names = attribs.GetNames(); DbaseFileHeader header = new DbaseFileHeader(); header.NumRecords = count; foreach (string name in names) { Type type = attribs.GetType(name); if (type == typeof(double) || type == typeof(float)) { header.AddColumn(name, 'N', DoubleLength, DoubleDecimals); } else if (type == typeof(short) || type == typeof(ushort) || type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong)) { header.AddColumn(name, 'N', IntLength, IntDecimals); } else if (type == typeof(string)) { header.AddColumn(name, 'C', StringLength, StringDecimals); } else if (type == typeof(bool)) { throw new NotImplementedException("Boolean values currently not supported"); } else if (type == typeof(DateTime)) { throw new NotImplementedException("DateTime values currently not supported"); } else { throw new ArgumentException("Type " + type.Name + " not supported"); } } return(header); }
/// <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, IGeometryFactory 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", String.Empty); } _dbfReader = new DbaseFileReader(filename + ".dbf"); _shpReader = new ShapefileReader(filename + ".shp", geometryFactory); _dbfHeader = _dbfReader.GetHeader(); _recordCount = _dbfHeader.NumRecords; // 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(); 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; }
/// <summary> /// Gets the stub header. /// </summary> /// <param name="feature">The feature.</param> /// <param name="count">The count.</param> /// <returns></returns> public static DbaseFileHeader GetHeader(Feature feature, int count) { IAttributesTable attribs = feature.Attributes; string[] names = attribs.GetNames(); DbaseFileHeader header = new DbaseFileHeader(); header.NumRecords = count; foreach (string name in names) { Type type = attribs.GetType(name); if (type == typeof(double) || type == typeof(float)) header.AddColumn(name, 'N', DoubleLength, DoubleDecimals); else if (type == typeof(short) || type == typeof(ushort) || type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong)) header.AddColumn(name, 'N', IntLength, IntDecimals); else if (type == typeof(string)) header.AddColumn(name, 'C', StringLength, StringDecimals); else if (type == typeof(bool)) throw new NotImplementedException("Boolean values currently not supported"); else if (type == typeof(DateTime)) throw new NotImplementedException("DateTime values currently not supported"); else throw new ArgumentException("Type " + type.Name + " not supported"); } return header; }
/// <summary> /// Gets the header from a dbf file. /// </summary> /// <param name="dbfFile">The DBF file.</param> /// <returns></returns> public static DbaseFileHeader GetHeader(string dbfFile) { if (!File.Exists(dbfFile)) throw new FileNotFoundException(dbfFile + " not found"); DbaseFileHeader header = new DbaseFileHeader(); header.ReadHeader(new BinaryReader(new FileStream(dbfFile, FileMode.Open, FileAccess.Read, FileShare.Read))); return header; }