/// <summary> /// Creates an instance of this class using the provided <paramref name="headerReader"/>. /// </summary> /// <param name="headerReader">The stream holding the header information</param> public ShapeFileHeader(BinaryReader headerReader) { // Check file header if (headerReader.ReadInt32() != 170328064) { //File Code is actually 9994, but in Little Endian Byte Order this is '170328064' throw (new ApplicationException("Invalid Shapefile (.shp)")); } // Get file length headerReader.BaseStream.Seek(20, SeekOrigin.Current); FileLength = 2 * ShapeFile.SwapByteOrder(headerReader.ReadInt32()); // Check file Version if (headerReader.ReadInt32() != ShapeFileVersion) { throw (new ApplicationException("Invalid Shapefile version")); } // Get the shape type ShapeType = (ShapeType)headerReader.ReadInt32(); // Get the bounding box _envelope = new Envelope(new Coordinate(headerReader.ReadDouble(), headerReader.ReadDouble()), new Coordinate(headerReader.ReadDouble(), headerReader.ReadDouble())); // Get the Z-range ZRange = Interval.Create(headerReader.ReadDouble(), headerReader.ReadDouble()); // Get the Z-range MRange = Interval.Create(ParseNoDataValue(headerReader.ReadDouble()), ParseNoDataValue(headerReader.ReadDouble())); }
public bool MoveNext() { if (_shpStream.Position >= _shpHeader.FileLength) { _current = new ShapeFileIndexEntry(); return(false); } // Get the record offset var recordOffset = (int)_shpStream.Position; // Get the oid var oid = ShapeFile.SwapByteOrder(_shpReader.ReadInt32()); Debug.Assert(oid == _lastOid + 1); _lastOid = oid; // Get the record length var recordLength = 2 * ShapeFile.SwapByteOrder(_shpReader.ReadInt32()); // Set the current ShapeFileIndexEntry _current = new ShapeFileIndexEntry(recordOffset, recordLength); // Adjust the streams position _shpStream.Seek(recordLength, SeekOrigin.Current); return(true); }
/// <summary> /// Method to create a SHX index from a given ShapeFile /// </summary> /// <param name="shpPath">The path to the shapefile</param> public static void Create(string shpPath) { if (string.IsNullOrEmpty(shpPath)) { throw new ArgumentNullException(shpPath); } if (!File.Exists(shpPath)) { throw new FileNotFoundException("The specified path does not lead to a file", shpPath); } var shxPath = Path.ChangeExtension(shpPath, ".shx"); if (File.Exists(shxPath)) { File.Delete(shxPath); } using (var it = new ShapeFileEnumerator(shpPath)) { var fs = new FileStream(shxPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); fs.Seek(100, SeekOrigin.Begin); using (var bw = new BinaryWriter(fs)) { var count = 0; while (it.MoveNext()) { count++; bw.Write(ShapeFile.SwapByteOrder(it.Current.Offset / 2)); bw.Write(ShapeFile.SwapByteOrder(it.Current.Length / 2)); } var length = (int)bw.BaseStream.Position; Debug.Assert(100 + count * 8 == length); bw.BaseStream.Seek(24, SeekOrigin.Begin); bw.Write(ShapeFile.SwapByteOrder(length / 2)); bw.BaseStream.Seek(length, SeekOrigin.Begin); } } }
public ShapeFileIndex(Stream stream) { stream.Seek(24, SeekOrigin.Begin); var buf = new byte[4]; stream.Read(buf, 0, 4); var bufferSize = 2 * ShapeFile.SwapByteOrder(BitConverter.ToInt32(buf, 0)) - 100; _shxBuffer = new byte[bufferSize]; stream.Seek(100, SeekOrigin.Begin); stream.Read(_shxBuffer, 0, bufferSize); for (var i = 0; i < bufferSize; i += 4) { var value = 2 * ShapeFile.SwapByteOrder(BitConverter.ToInt32(_shxBuffer, i)); var tmp = BitConverter.GetBytes(value); Buffer.BlockCopy(tmp, 0, _shxBuffer, i, 4); } FeatureCount = (bufferSize) / 8; }