/// <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.ReadInt32BE(); 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.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); _fileLength = shpBinaryReader.ReadInt32BE(); _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 = (ShapeGeometryTypes) Enum.Parse(typeof(ShapeGeometryTypes), 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> /// /// </summary> /// <returns></returns> 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> /// 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.ReadInt32BE(); 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.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); shpBinaryReader.ReadInt32BE(); _fileLength = shpBinaryReader.ReadInt32BE(); _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 = (ShapeGeometryTypes)Enum.Parse(typeof(ShapeGeometryTypes), 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> /// Reads a generic stream containing geographic data saved as shapefile structure, /// and returns a collection of all the features contained. /// Since NTS Geometry Model not support Z and M data, those informations are ignored if presents in shapefile. /// </summary> /// <param name="stream">Shapefile data stream.</param> /// <returns><c>GeometryCollection</c> containing all geometries in shapefile.</returns> protected IGeometryCollection Read(Stream stream) { // Read big endian values using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream)) { // Verify File Code int fileCode = beReader.ReadInt32BE(); Debug.Assert(fileCode == 9994); stream.Seek(20, SeekOrigin.Current); length = beReader.ReadInt32BE(); // Read little endian values using (BinaryReader leReader = new BinaryReader(stream)) { ArrayList list = null; // Verify Version int version = leReader.ReadInt32(); Debug.Assert(version == 1000); // ShapeTypes int shapeType = leReader.ReadInt32(); switch ((ShapeGeometryTypes) shapeType) { case ShapeGeometryTypes.Point: case ShapeGeometryTypes.PointZ: case ShapeGeometryTypes.PointM: case ShapeGeometryTypes.PointZM: list = new ArrayList(ReadPointData(stream)); break; case ShapeGeometryTypes.LineString: case ShapeGeometryTypes.LineStringZ: case ShapeGeometryTypes.LineStringM: case ShapeGeometryTypes.LineStringZM: list = new ArrayList(ReadLineStringData(stream)); break; case ShapeGeometryTypes.Polygon: case ShapeGeometryTypes.PolygonZ: case ShapeGeometryTypes.PolygonM: case ShapeGeometryTypes.PolygonZM: list = new ArrayList(ReadPolygonData(stream)); break; case ShapeGeometryTypes.MultiPoint: case ShapeGeometryTypes.MultiPointZ: case ShapeGeometryTypes.MultiPointM: case ShapeGeometryTypes.MultiPointZM: list = new ArrayList(ReadMultiPointData(stream)); break; case ShapeGeometryTypes.MultiPatch: throw new NotImplementedException("FeatureType " + shapeType + " not supported."); default: throw new ArgumentOutOfRangeException("FeatureType " + shapeType + " not recognized by the system"); } IGeometryCollection collection = shapeReader.CreateGeometryCollection(list); return collection; } } }
/// <summary> /// /// </summary> /// <param name="beReader"></param> private void ReadFeatureHeader(BigEndianBinaryReader beReader) { int recordNumber = beReader.ReadInt32BE(); int contentLength = beReader.ReadInt32BE(); int shapeType = beReader.ReadInt32(); }
/// <summary> /// Reads a generic stream containing geographic data saved as shapefile structure, /// and returns a collection of all the features contained. /// Since NTS Geometry Model not support Z and M data, those informations are ignored if presents in shapefile. /// </summary> /// <param name="stream">Shapefile data stream.</param> /// <returns><c>GeometryCollection</c> containing all geometries in shapefile.</returns> protected IGeometryCollection Read(Stream stream) { // Read big endian values using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream)) { // Verify File Code int fileCode = beReader.ReadInt32BE(); Debug.Assert(fileCode == 9994); stream.Seek(20, SeekOrigin.Current); length = beReader.ReadInt32BE(); // Read little endian values using (BinaryReader leReader = new BinaryReader(stream)) { ArrayList list = null; // Verify Version int version = leReader.ReadInt32(); Debug.Assert(version == 1000); // ShapeTypes int shapeType = leReader.ReadInt32(); switch ((ShapeGeometryTypes)shapeType) { case ShapeGeometryTypes.Point: case ShapeGeometryTypes.PointZ: case ShapeGeometryTypes.PointM: case ShapeGeometryTypes.PointZM: list = new ArrayList(ReadPointData(stream)); break; case ShapeGeometryTypes.LineString: case ShapeGeometryTypes.LineStringZ: case ShapeGeometryTypes.LineStringM: case ShapeGeometryTypes.LineStringZM: list = new ArrayList(ReadLineStringData(stream)); break; case ShapeGeometryTypes.Polygon: case ShapeGeometryTypes.PolygonZ: case ShapeGeometryTypes.PolygonM: case ShapeGeometryTypes.PolygonZM: list = new ArrayList(ReadPolygonData(stream)); break; case ShapeGeometryTypes.MultiPoint: case ShapeGeometryTypes.MultiPointZ: case ShapeGeometryTypes.MultiPointM: case ShapeGeometryTypes.MultiPointZM: list = new ArrayList(ReadMultiPointData(stream)); break; case ShapeGeometryTypes.MultiPatch: throw new NotImplementedException("FeatureType " + shapeType + " not supported."); default: throw new ArgumentOutOfRangeException("FeatureType " + shapeType + " not recognized by the system"); } IGeometryCollection collection = shapeReader.CreateGeometryCollection(list); return(collection); } } }