예제 #1
0
        /// <summary>
        /// Create a new Shapefile object and open a Shapefile. Note that streams of 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).
        /// </summary>
        /// <param name="mainStream">Stream of .shp for this Shapefile</param>
        /// <param name="indexStream">Stream of .shx for this Shapefile</param>
        /// <param name="dbfStream">relative path of .dbf 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="InvalidOperationException">Thrown if an error occurs parsing file headers</exception>
        public void Open(Stream mainStream, Stream indexStream, Stream dbfStream)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Shapefile");
            }
            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  = mainStream;
            _indexStream = indexStream;

            _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;

            dbfReader = new DBFReader(dbfStream, Encoding.ASCII);
            dbfReader.ResetReaderToEndOfHeader();
            // open the metadata database
            //OpenDb(dbfDirectory);

            _opened = true;
        }