/// <summary> /// Initializes a new instance of the <see cref="DbaseFileEnumerator"/> class. /// </summary> /// <param name="parent"></param> public DbaseFileEnumerator(DbaseFileReader parent) { _parent = parent; FileStream stream = new FileStream(parent._filename, FileMode.Open, FileAccess.Read, FileShare.Read); _dbfStream = new BinaryReader(stream, parent._header.Encoding); ReadHeader(); }
/// <summary> /// Create a new Shapefile object and open a Shapefile. Note that three files are required - /// the main file (.shp), the index file (.shx) and the dBASE table (.dbf). The three files /// must all have the same filename (i.e. shapes.shp, shapes.shx and shapes.dbf). Set path /// to any one of these three files to open the Shapefile. /// </summary> /// <param name="path">Path to the .shp, .shx or .dbf file for this Shapefile</param> /// <exception cref="ObjectDisposedException">Thrown if the Shapefile has been disposed</exception> /// <exception cref="ArgumentNullException">Thrown if the path parameter is null</exception> /// <exception cref="ArgumentException">Thrown if the path parameter is empty</exception> /// <exception cref="FileNotFoundException">Thrown if one of the three required files is not found</exception> /// <exception cref="InvalidOperationException">Thrown if an error occurs parsing file headers</exception> public void Open(string path) { if (_disposed) { throw new ObjectDisposedException("Shapefile"); } if (path == null) { throw new ArgumentNullException("path"); } if (path.Length <= 0) { throw new ArgumentException("path parameter is empty", "path"); } _shapefileMainPath = Path.ChangeExtension(path, MainPathExtension); _shapefileIndexPath = Path.ChangeExtension(path, IndexPathExtension); _shapefileDbasePath = Path.ChangeExtension(path, DbasePathExtension); _shapefileProjPath = Path.ChangeExtension(path, ProjPathExtension); if (!File.Exists(_shapefileMainPath)) { throw new FileNotFoundException("Shapefile main file not found", _shapefileMainPath); } if (!File.Exists(_shapefileIndexPath)) { throw new FileNotFoundException("Shapefile index file not found", _shapefileIndexPath); } if (!File.Exists(_shapefileDbasePath)) { throw new FileNotFoundException("Shapefile dBase file not found", _shapefileDbasePath); } // read projection if file exist if (File.Exists(_shapefileProjPath)) { this.Projection = File.ReadAllText(_shapefileProjPath); } _mainStream = File.Open(_shapefileMainPath, FileMode.Open, FileAccess.Read, FileShare.Read); _indexStream = File.Open(_shapefileIndexPath, FileMode.Open, FileAccess.Read, FileShare.Read); if (_mainStream.Length < Header.HeaderLength) { throw new InvalidOperationException("Shapefile main file does not contain a valid header"); } if (_indexStream.Length < Header.HeaderLength) { throw new InvalidOperationException("Shapefile index file does not contain a valid header"); } // read in and parse the headers byte[] headerBytes = new byte[Header.HeaderLength]; _mainStream.Read(headerBytes, 0, Header.HeaderLength); _mainHeader = new Header(headerBytes); _indexStream.Read(headerBytes, 0, Header.HeaderLength); _indexHeader = new Header(headerBytes); // set properties from the main header _type = _mainHeader.ShapeType; _boundingBox = new RectangleD(_mainHeader.XMin, _mainHeader.YMin, _mainHeader.XMax, _mainHeader.YMax); // index header length is in 16-bit words, including the header - number of // shapes is the number of records (each 4 workds long) after subtracting the header bytes _count = (_indexHeader.FileLength - (Header.HeaderLength / 2)) / 4; // open the metadata database _dBaseFileReader = new DbaseFileReader(_shapefileDbasePath); _dBaseHeader = _dBaseFileReader.GetHeader(); _dBaseEnumerator = _dBaseFileReader.GetEnumerator(); _opened = true; }