/// <summary> /// Advances the enumerator to the next element of the collection. /// </summary> /// <returns> /// true if the enumerator was successfully advanced to the next element; /// false if the enumerator has passed the end of the collection. /// </returns> /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception> public bool MoveNext() { if (_idxBinaryReader != null) { if (_idxBinaryReader.BaseStream.Position >= _idxBinaryReader.BaseStream.Length) { return(false); } var offset = 2L * _idxBinaryReader.ReadInt32BE(); var contentLength = 2 * _idxBinaryReader.ReadInt32BE(); _shpBinaryReader.BaseStream.Position = offset; } if (_shpBinaryReader.BaseStream.Position < _shpBinaryReader.BaseStream.Length) { // Mark Jacquin: add a try catch when some shapefile have extra char at the end but no record try { int recordNumber = _shpBinaryReader.ReadInt32BE(); int contentLength = _shpBinaryReader.ReadInt32BE(); _geometry = _handler.Read(_shpBinaryReader, contentLength, _parent._geometryFactory); } catch (Exception ex) { return(false); } return(true); } // Reached end of file, so close the reader. //_shpBinaryReader.Close(); return(false); }
/// <summary> /// Read shape at a given offset. /// </summary> /// <param name="shapeOffset"> The offset at which the requested shape metadata begins.</param> /// <param name="geoFactory"></param> /// <returns></returns> public Geometry ReadShapeAtOffset(long shapeOffset, GeometryFactory geoFactory) { Geometry currGeomtry = null; ThrowIfDisposed(); if (shapeOffset < HEADER_LENGTH || shapeOffset >= ShapeReaderStream.BaseStream.Length) { throw new IndexOutOfRangeException("Shape offset cannot be lower than header length (100) or higher than shape file size"); } lock (ShapeReaderStream) { // Skip to shape size location in file. ShapeReaderStream.BaseStream.Seek(shapeOffset + 4, SeekOrigin.Begin); int currShapeLengthInWords = ShapeReaderStream.ReadInt32BE(); currGeomtry = m_ShapeHandler.Read(ShapeReaderStream, currShapeLengthInWords, geoFactory); } return(currGeomtry); }
/// <summary> /// Advances the enumerator to the next element of the collection. /// </summary> /// <returns> /// true if the enumerator was successfully advanced to the next element; /// false if the enumerator has passed the end of the collection. /// </returns> /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception> public bool MoveNext() { if (_shpBinaryReader.PeekChar() != -1) { // Mark Jacquin: add a try catch when some shapefile have extra char at the end but no record try { int recordNumber = _shpBinaryReader.ReadInt32BE(); int contentLength = _shpBinaryReader.ReadInt32BE(); _geometry = _handler.Read(_shpBinaryReader, _parent._geometryFactory); } catch (Exception) { return(false); } return(true); } else { // Reached end of file, so close the reader. //_shpBinaryReader.Close(); return(false); } }
/// <summary> /// Advances the enumerator to the next element of the collection. /// </summary> /// <returns> /// true if the enumerator was successfully advanced to the next element; /// false if the enumerator has passed the end of the collection. /// </returns> /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception> public bool MoveNext() { if (_shpBinaryReader.BaseStream.Position < _shpBinaryReader.BaseStream.Length) { // Mark Jacquin: add a try catch when some shapefile have extra char at the end but no record try { int recordNumber = _shpBinaryReader.ReadInt32BE(); int contentLength = _shpBinaryReader.ReadInt32BE(); _geometry = _handler.Read(_shpBinaryReader, contentLength, _parent._geometryFactory); } catch (Exception ex) { // actually we should remove this crap... Trace.WriteLine(ex.Message); return(false); } return(true); } // Reached end of file, so close the reader. //_shpBinaryReader.Close(); return(false); }
/// <summary> /// Reads a record from the specified stream. /// </summary> /// <param name="stream">A System.IO.Stream instance to read</param> /// <param name="recordOffset">An offset of record</param> /// <param name="bounds">An object representing a bounds of the reading area</param> public ShapeFileRecord ReadRecord(Stream stream, int?recordOffset, BoundingRectangle bounds) { #region old //if (recordOffset != null) // stream.Seek(recordOffset.Value, 0); //ShapeFileRecord record = new ShapeFileRecord(); //record.Offset = stream.Position; //// заголовок записи //record.RecordNumber = ShapeFile.ReadInt32_BE(stream); //record.ContentLength = ShapeFile.ReadInt32_BE(stream); //// тип геометрической фигуры //record.ShapeType = ShapeFile.ReadInt32_LE(stream); //bool wasRead = false; //switch (record.ShapeType) //{ // case (int)ShapeType.NullShape: // break; // case (int)ShapeType.Point: // wasRead = ShapeFile.ReadPoint(stream, bounds, record); // break; // case (int)ShapeType.PolyLine: // wasRead = ShapeFile.ReadPolygon(stream, bounds, record); // break; // case (int)ShapeType.Polygon: // wasRead = ShapeFile.ReadPolygon(stream, bounds, record); // break; // case (int)ShapeType.Multipoint: // wasRead = ShapeFile.ReadMultipoint(stream, bounds, record); // break; // default: // { // string msg = String.Format(System.Globalization.CultureInfo.InvariantCulture, "ShapeType {0} is not supported.", (int)record.ShapeType); // throw new InvalidDataException(msg); // } //} //if (wasRead) //{ // this._records.Add(record); // return record; //} //else return null; #endregion #region New if (recordOffset != null) { stream.Seek(recordOffset.Value, 0); } ShapeFileRecord record = new ShapeFileRecord(); record.Offset = stream.Position; // заголовок записи //BigEndianBinaryReader reader = new BigEndianBinaryReader(stream); //record.RecordNumber = reader.ReadInt32BE();// ShapeFile.ReadInt32_BE(stream); //record.ContentLength = reader.ReadInt32BE();// ShapeFile.ReadInt32_BE(stream); record.RecordNumber = stream.ReadInt32BE(); // ShapeFile.ReadInt32_BE(stream); record.ContentLength = stream.ReadInt32BE(); // ShapeFile.ReadInt32_BE(stream); // тип геометрической фигуры record.ShapeType = stream.ReadInt32(); //.ReadInt32BE();// ShapeFile.ReadInt32_LE(stream); ShapeHandler handler = ShapeFile.GetShapeHandler((ShapeType)record.ShapeType); if (handler.Read(stream, bounds, record)) { this._records.Add(record); return(record); } else { return(null); } #endregion }