Example #1
0
        /// <summary>
        /// Parses the first 100 bytes of a shapefile into the important values
        /// </summary>
        /// <param name="inFilename">The fileName to read</param>
        public void Open(string inFilename)
        {
            _fileName = inFilename;

            //  Position        Field           Value       Type        ByteOrder
            //  --------------------------------------------------------------
            //  Byte 0          File Code       9994        Integer     Big
            //  Byte 4          Unused          0           Integer     Big
            //  Byte 8          Unused          0           Integer     Big
            //  Byte 12         Unused          0           Integer     Big
            //  Byte 16         Unused          0           Integer     Big
            //  Byte 20         Unused          0           Integer     Big
            //  Byte 24         File Length     File Length Integer     Big
            //  Byte 28         Version         1000        Integer     Little
            //  Byte 32         Shape Type      Shape Type  Integer     Little
            //  Byte 36         Bounding Box    Xmin        Double      Little
            //  Byte 44         Bounding Box    Ymin        Double      Little
            //  Byte 52         Bounding Box    Xmax        Double      Little
            //  Byte 60         Bounding Box    Ymax        Double      Little
            //  Byte 68         Bounding Box    Zmin        Double      Little
            //  Byte 76         Bounding Box    Zmax        Double      Little
            //  Byte 84         Bounding Box    Mmin        Double      Little
            //  Byte 92         Bounding Box    Mmax        Double      Little

            // This may throw an IOException if the file is already in use.
            BufferedBinaryReader bbReader = new BufferedBinaryReader(inFilename);

            bbReader.FillBuffer(100); // we only need to read 100 bytes from the header.

            bbReader.Close(); // Close the internal readers connected to the file, but don't close the file itself.

            // Reading BigEndian simply requires us to reverse the byte order.
            _fileCode = bbReader.ReadInt32(false);

            // Skip the next 20 bytes because they are unused
            bbReader.Seek(20, SeekOrigin.Current);

            // Read the file length in reverse sequence
            _fileLength = bbReader.ReadInt32(false);

            // From this point on, all the header values are in little Endean

            // Read the version
            _version = bbReader.ReadInt32();

            // Read in the shape type that should be the shape type for the whole shapefile
            _shapeType = (ShapeType)bbReader.ReadInt32();

            // Get the extents, each of which are double values.
            _xMin = bbReader.ReadDouble();
            _yMin = bbReader.ReadDouble();
            _xMax = bbReader.ReadDouble();
            _yMax = bbReader.ReadDouble();
            _zMin = bbReader.ReadDouble();
            _zMax = bbReader.ReadDouble();
            _mMin = bbReader.ReadDouble();
            _mMax = bbReader.ReadDouble();

            bbReader.Dispose();

            string shxFile = Path.ChangeExtension(Filename, ".shx");
            FileInfo fi = new FileInfo(shxFile);
            if (fi.Exists)
            {
                _shxLength = Convert.ToInt32(fi.Length / 2); //length is in 16 bit words.
            }
        }
Example #2
0
        /// <summary>
        /// Parses the first 100 bytes of a shapefile into the important values
        /// </summary>
        /// <param name="inFilename">The fileName to read</param>
        public void Open(string inFilename)
        {
            Filename = inFilename;

            // Position        Field           Value       Type        ByteOrder
            // --------------------------------------------------------------
            // Byte 0          File Code       9994        Integer     Big
            // Byte 4          Unused          0           Integer     Big
            // Byte 8          Unused          0           Integer     Big
            // Byte 12         Unused          0           Integer     Big
            // Byte 16         Unused          0           Integer     Big
            // Byte 20         Unused          0           Integer     Big
            // Byte 24         File Length     File Length Integer     Big
            // Byte 28         Version         1000        Integer     Little
            // Byte 32         Shape Type      Shape Type  Integer     Little
            // Byte 36         Bounding Box    Xmin        Double      Little
            // Byte 44         Bounding Box    Ymin        Double      Little
            // Byte 52         Bounding Box    Xmax        Double      Little
            // Byte 60         Bounding Box    Ymax        Double      Little
            // Byte 68         Bounding Box    Zmin        Double      Little
            // Byte 76         Bounding Box    Zmax        Double      Little
            // Byte 84         Bounding Box    Mmin        Double      Little
            // Byte 92         Bounding Box    Mmax        Double      Little

            // This may throw an IOException if the file is already in use.
            BufferedBinaryReader bbReader = new BufferedBinaryReader(Filename);

            bbReader.FillBuffer(100); // we only need to read 100 bytes from the header.

            bbReader.Close();         // Close the internal readers connected to the file, but don't close the file itself.

            // Reading BigEndian simply requires us to reverse the byte order.
            FileCode = bbReader.ReadInt32(false);

            // Skip the next 20 bytes because they are unused
            bbReader.Seek(20, SeekOrigin.Current);

            // Read the file length in reverse sequence
            FileLength = bbReader.ReadInt32(false);

            // From this point on, all the header values are in little Endean

            // Read the version
            Version = bbReader.ReadInt32();

            // Read in the shape type that should be the shape type for the whole shapefile
            ShapeType = (ShapeType)bbReader.ReadInt32();

            // Get the extents, each of which are double values.
            Xmin = bbReader.ReadDouble();
            Ymin = bbReader.ReadDouble();
            Xmax = bbReader.ReadDouble();
            Ymax = bbReader.ReadDouble();
            Zmin = bbReader.ReadDouble();
            Zmax = bbReader.ReadDouble();
            Mmin = bbReader.ReadDouble();
            Mmax = bbReader.ReadDouble();

            bbReader.Dispose();

            FileInfo fi = new FileInfo(ShxFilename);

            if (fi.Exists)
            {
                ShxLength = Convert.ToInt32(fi.Length / 2); // length is in 16 bit words.
            }
        }