/// <summary> /// Initializes a new instance of the <see cref="ShapefileHeader">ShapefileHeader</see> class with values read in from the stream. /// </summary> /// <remarks>Reads the header information from the stream.</remarks> /// <param name="reader">BigEndianBinaryReader stream to the shapefile.</param> public ShapefileHeader(BigEndianBinaryReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } _fileCode = reader.ReadIntBE(); if (_fileCode!=Shapefile.ShapefileId) { throw new ShapefileException("The first four bytes of this file indicate this is not a shape file."); } // Skip 5 unsed bytes for (int i = 0; i < 5; i++) { reader.ReadIntBE(); } _fileLength = reader.ReadIntBE(); _version = reader.ReadInt32(); Debug.Assert(_version == 1000, "Shapefile version", String.Format(System.Globalization.CultureInfo.InvariantCulture, "Expecting only one version (1000), but got {0}", _version)); int shapeType = reader.ReadInt32(); if (Enum.IsDefined(typeof(ShapeType), shapeType)) { _shapeType = (ShapeType)shapeType; } else { throw new ShapefileException("Invalid shape type"); } // Read in and store the bounding box double[] coords = new double[4]; for (int i = 0; i < coords.Length; i++) { coords[i] = reader.ReadDouble(); } _bounds = new Envelope(coords[0], coords[2], coords[1], coords[3]); // skip z and m bounding boxes. for (int i = 0; i < 4; i++) { reader.ReadDouble(); } }
/// <summary> /// Initializes a new instance of the <see cref="ShapefileHeader">ShapefileHeader</see> class with values read in from the stream. /// </summary> /// <remarks>Reads the header information from the stream.</remarks> /// <param name="reader">BigEndianBinaryReader stream to the shapefile.</param> public ShapefileHeader(BigEndianBinaryReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } _fileCode = reader.ReadIntBE(); if (_fileCode != Shapefile.ShapefileId) { throw new ShapefileException("The first four bytes of this file indicate this is not a shape file."); } // Skip 5 unsed bytes for (int i = 0; i < 5; i++) { reader.ReadIntBE(); } _fileLength = reader.ReadIntBE(); _version = reader.ReadInt32(); Debug.Assert(_version == 1000, "Shapefile version", String.Format(System.Globalization.CultureInfo.InvariantCulture, "Expecting only one version (1000), but got {0}", _version)); int shapeType = reader.ReadInt32(); if (Enum.IsDefined(typeof(ShapeType), shapeType)) { _shapeType = (ShapeType)shapeType; } else { throw new ShapefileException("Invalid shape type"); } // Read in and store the bounding box double[] coords = new double[4]; for (int i = 0; i < coords.Length; i++) { coords[i] = reader.ReadDouble(); } _bounds = new Envelope(coords[0], coords[2], coords[1], coords[3]); // skip z and m bounding boxes. for (int i = 0; i < 4; i++) { reader.ReadDouble(); } }
public bool Read() { if (_reader.PeekChar() != -1) { _currentOffset = _reader.ReadIntBE(); _currentLength = _reader.ReadIntBE(); return(true); } else { return(false); } }
public bool MoveNext() { if (_shpBinaryReader.PeekChar() != -1) { int recordNumber = _shpBinaryReader.ReadIntBE(); int contentLength = _shpBinaryReader.ReadIntBE(); if (Shapefile.TraceSwitch.Enabled) { Trace.WriteLine("Record number :" + recordNumber); Trace.WriteLine("contentLength :" + contentLength); } _geometry = _handler.Read(_shpBinaryReader, _parent._geometryFactory); return(true); } else { // reached end of file, so close the reader. _shpBinaryReader.Close(); return(false); } }
/// <summary> /// Initializes a new instance of the ShapefileHeader class with values read in from the stream. /// </summary> /// <remarks>Reads the header information from the stream.</remarks> /// <param name="shpBinaryReader">BigEndianBinaryReader stream to the shapefile.</param> public ShapefileHeader(BigEndianBinaryReader shpBinaryReader) { if (shpBinaryReader == null) { throw new ArgumentNullException("shpBinaryReader"); } _fileCode = shpBinaryReader.ReadIntBE(); if (_fileCode != Shapefile.ShapefileId) { throw new ShapefileException("The first four bytes of this file indicate this is not a shape file."); } // skip 5 unsed bytes shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); _fileLength = shpBinaryReader.ReadIntBE(); _version = shpBinaryReader.ReadInt32(); Debug.Assert(_version == 1000, "Shapefile version", String.Format("Expecting only one version (1000), but got {0}", _version)); int shapeType = shpBinaryReader.ReadInt32(); _shapeType = (ShapeType)Enum.Parse(typeof(ShapeType), shapeType.ToString()); //read in and store the bounding box double[] coords = new double[4]; for (int i = 0; i < 4; i++) { coords[i] = shpBinaryReader.ReadDouble(); } _bounds = new Envelope(coords[0], coords[2], coords[1], coords[3]); // skip z and m bounding boxes. for (int i = 0; i < 4; i++) { shpBinaryReader.ReadDouble(); } }
/// <summary> /// Initializes a new instance of the ShapefileHeader class with values read in from the stream. /// </summary> /// <remarks>Reads the header information from the stream.</remarks> /// <param name="shpBinaryReader">BigEndianBinaryReader stream to the shapefile.</param> public ShapefileHeader(BigEndianBinaryReader shpBinaryReader) { if (shpBinaryReader==null) { throw new ArgumentNullException("shpBinaryReader"); } _fileCode = shpBinaryReader.ReadIntBE(); if (_fileCode!=Shapefile.ShapefileId) { throw new ShapefileException("The first four bytes of this file indicate this is not a shape file."); } // skip 5 unsed bytes shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); shpBinaryReader.ReadIntBE(); _fileLength = shpBinaryReader.ReadIntBE(); _version = shpBinaryReader.ReadInt32(); Debug.Assert(_version==1000, "Shapefile version", String.Format("Expecting only one version (1000), but got {0}",_version)); int shapeType = shpBinaryReader.ReadInt32(); _shapeType = (ShapeType)Enum.Parse(typeof(ShapeType),shapeType.ToString()); //read in and store the bounding box double[] coords = new double[4]; for (int i = 0; i < 4; i++) { coords[i]=shpBinaryReader.ReadDouble(); } _bounds = new Envelope(coords[0], coords[2], coords[1], coords[3]); // skip z and m bounding boxes. for (int i=0; i < 4; i++) { shpBinaryReader.ReadDouble(); } }
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(); } }