/// <summary> /// /// </summary> /// <param name="geometry"></param> /// <param name="beWriter"></param> protected void WriteFeatureHeader(Geometry geometry, BigEndianBinaryWriter beWriter) { // Write record number (big endian) beWriter.WriteIntBE((int)++recordCounter); // Write content length (big endian) int contentLength = shapeWriter.GetBytesLength(geometry); beWriter.WriteIntBE((int)(contentLength / 2)); // Write length in 16 bit words! }
/// <summary> /// /// </summary> /// <param name="geometries"></param> /// <param name="beWriter">Big Endian Writer</param> /// <param name="leWriter">Little Endian Endian Writer</param> /// <param name="streamLength"></param> protected void WriteHeaderFile(GeometryCollection geometries, BinaryWriter leWriter, BigEndianBinaryWriter beWriter, int streamLength) { // Write stub value for FileCode (big endian) beWriter.WriteIntBE((int)9994); // Write stub values for unused (big endian) beWriter.WriteIntBE((int)0); beWriter.WriteIntBE((int)0); beWriter.WriteIntBE((int)0); beWriter.WriteIntBE((int)0); beWriter.WriteIntBE((int)0); // Write stub value for Length (big endian) beWriter.WriteIntBE((int)streamLength); // Write version leWriter.Write((int)1000); // Write ShapeTipe leWriter.Write((int)GetShapeType((Geometry)geometries.Geometries[0])); // Write values for boundingbox Envelope envelope = (Envelope)geometries.EnvelopeInternal; leWriter.Write((double)envelope.MinX); leWriter.Write((double)envelope.MinY); leWriter.Write((double)envelope.MaxX); leWriter.Write((double)envelope.MaxY); leWriter.Write((double)0); leWriter.Write((double)0); leWriter.Write((double)0); leWriter.Write((double)0); }
/// <summary> /// Writes a shapefile header to the given stream; /// </summary> /// <param name="file">The binary writer to use.</param> public void Write(BigEndianBinaryWriter file) { if (file == null) { throw new ArgumentNullException("file"); } if (_fileLength == -1) { throw new InvalidOperationException("The header properties need to be set before writing the header record."); } int pos = 0; file.WriteIntBE(_fileCode); pos += 4; for (int i = 0; i < 5; i++) { file.WriteIntBE(0); //Skip unused part of header pos += 4; } file.WriteIntBE(_fileLength); pos += 4; file.Write(_version); pos += 4; file.Write(int.Parse(Enum.Format(typeof(ShapeGeometryTypes), _shapeType, "d"))); pos += 4; // Write the bounding box file.Write(_bounds.MinX); file.Write(_bounds.MinY); file.Write(_bounds.MaxX); file.Write(_bounds.MaxY); pos += 8 * 4; // Skip remaining unused bytes for (int i = 0; i < 4; i++) { file.Write(0.0); // Skip unused part of header pos += 8; } }
/// <summary> /// Writes a shapefile to disk. /// </summary> /// <remarks> /// Assumes the type given for the first geometry is the same for all subsequent geometries. /// For example, is, if the first Geometry is a Multi-polygon/ Polygon, the subsequent geometies are /// Muli-polygon/ polygon and not lines or points. /// The dbase file for the corresponding shapefile contains one column called row. It contains /// the row number. /// </remarks> /// <param name="filename">The filename to write to (minus the .shp extension).</param> /// <param name="geometryCollection">The GeometryCollection to write.</param> public void Write(string filename, IGeometryCollection geometryCollection) { System.IO.FileStream shpStream = new System.IO.FileStream(filename + ".shp", System.IO.FileMode.Create); System.IO.FileStream shxStream = new System.IO.FileStream(filename + ".shx", System.IO.FileMode.Create); BigEndianBinaryWriter shpBinaryWriter = new BigEndianBinaryWriter(shpStream); BigEndianBinaryWriter shxBinaryWriter = new BigEndianBinaryWriter(shxStream); // assumes ShapeHandler handler = Shapefile.GetShapeHandler(Shapefile.GetShapeType(geometryCollection.Geometries[0])); IGeometry body; int numShapes = geometryCollection.NumGeometries; // calc the length of the shp file, so it can put in the header. int shpLength = 50; for (int i = 0; i < numShapes; i++) { body = (IGeometry) geometryCollection.Geometries[i]; shpLength += 4; // length of header in WORDS shpLength += handler.GetLength(body); // length of shape in WORDS } int shxLength = 50 + (4*numShapes); // write the .shp header ShapefileHeader shpHeader = new ShapefileHeader(); shpHeader.FileLength = shpLength; // get envelope in external coordinates IEnvelope env = geometryCollection.EnvelopeInternal; IEnvelope bounds = ShapeHandler.GetEnvelopeExternal(geometryFactory.PrecisionModel, env); shpHeader.Bounds = bounds; // assumes Geometry type of the first item will the same for all other items // in the collection. shpHeader.ShapeType = Shapefile.GetShapeType(geometryCollection.Geometries[0]); shpHeader.Write(shpBinaryWriter); // write the .shx header ShapefileHeader shxHeader = new ShapefileHeader(); shxHeader.FileLength = shxLength; shxHeader.Bounds = shpHeader.Bounds; // assumes Geometry type of the first item will the same for all other items in the collection. shxHeader.ShapeType = Shapefile.GetShapeType(geometryCollection.Geometries[0]); shxHeader.Write(shxBinaryWriter); // write the individual records. int _pos = 50; // header length in WORDS for (int i = 0; i < numShapes; i++) { body = geometryCollection.Geometries[i]; int recordLength = handler.GetLength(body); shpBinaryWriter.WriteIntBE(i+1); shpBinaryWriter.WriteIntBE(recordLength); shxBinaryWriter.WriteIntBE(_pos); shxBinaryWriter.WriteIntBE(recordLength); _pos += 4; // length of header in WORDS handler.Write(body, shpBinaryWriter, geometryFactory); _pos += recordLength; // length of shape in WORDS } shxBinaryWriter.Flush(); shxBinaryWriter.Close(); shpBinaryWriter.Flush(); shpBinaryWriter.Close(); // WriteDummyDbf(filename + ".dbf", numShapes); }
/// <summary> /// Writes a shapefile header to the given stream; /// </summary> /// <param name="file">The binary writer to use.</param> public void Write(BigEndianBinaryWriter file) { if (file == null) throw new ArgumentNullException("file"); if (_fileLength==-1) throw new InvalidOperationException("The header properties need to be set before writing the header record."); int pos = 0; file.WriteIntBE(_fileCode); pos += 4; for (int i = 0; i < 5; i++) { file.WriteIntBE(0);//Skip unused part of header pos += 4; } file.WriteIntBE(_fileLength); pos += 4; file.Write(_version); pos += 4; file.Write(int.Parse(Enum.Format(typeof(ShapeGeometryTypes), _shapeType, "d"))); pos += 4; // Write the bounding box file.Write(_bounds.MinX); file.Write(_bounds.MinY); file.Write(_bounds.MaxX); file.Write(_bounds.MaxY); pos += 8 * 4; // Skip remaining unused bytes for (int i = 0; i < 4; i++) { file.Write(0.0); // Skip unused part of header pos += 8; } }
/// <summary> /// /// </summary> /// <param name="geometries"></param> /// <param name="beWriter">Big Endian Writer</param> /// <param name="leWriter">Little Endian Endian Writer</param> /// <param name="streamLength"></param> protected void WriteHeaderFile(GeometryCollection geometries, BinaryWriter leWriter, BigEndianBinaryWriter beWriter, int streamLength) { // Write stub value for FileCode (big endian) beWriter.WriteIntBE((int) 9994); // Write stub values for unused (big endian) beWriter.WriteIntBE((int) 0); beWriter.WriteIntBE((int) 0); beWriter.WriteIntBE((int) 0); beWriter.WriteIntBE((int) 0); beWriter.WriteIntBE((int) 0); // Write stub value for Length (big endian) beWriter.WriteIntBE((int) streamLength); // Write version leWriter.Write((int) 1000); // Write ShapeTipe leWriter.Write((int) GetShapeType((Geometry) geometries.Geometries[0])); // Write values for boundingbox Envelope envelope = (Envelope) geometries.EnvelopeInternal; leWriter.Write((double) envelope.MinX); leWriter.Write((double) envelope.MinY); leWriter.Write((double) envelope.MaxX); leWriter.Write((double) envelope.MaxY); leWriter.Write((double) 0); leWriter.Write((double) 0); leWriter.Write((double) 0); leWriter.Write((double) 0); }
/// <summary> /// /// </summary> /// <param name="geometry"></param> /// <param name="beWriter"></param> protected void WriteFeatureHeader(Geometry geometry, BigEndianBinaryWriter beWriter) { // Write record number (big endian) beWriter.WriteIntBE((int)++recordCounter); // Write content length (big endian) int contentLength = shapeWriter.GetBytesLength(geometry); beWriter.WriteIntBE((int)(contentLength / 2)); // Write length in 16 bit words! }
/// <summary> /// Writes a shapefile to disk. /// </summary> /// <remarks> /// Assumes the type given for the first geometry is the same for all subsequent geometries. /// For example, is, if the first Geometry is a Multi-polygon/ Polygon, the subsequent geometies are /// Muli-polygon/ polygon and not lines or points. /// The dbase file for the corresponding shapefile contains one column called row. It contains /// the row number. /// </remarks> /// <param name="filename">The filename to write to (minus the .shp extension).</param> /// <param name="geometryCollection">The GeometryCollection to write.</param> public void Write(string filename, IGeometryCollection geometryCollection) { System.IO.FileStream shpStream = new System.IO.FileStream(filename + ".shp", System.IO.FileMode.Create); System.IO.FileStream shxStream = new System.IO.FileStream(filename + ".shx", System.IO.FileMode.Create); BigEndianBinaryWriter shpBinaryWriter = new BigEndianBinaryWriter(shpStream); BigEndianBinaryWriter shxBinaryWriter = new BigEndianBinaryWriter(shxStream); // assumes ShapeHandler handler = Shapefile.GetShapeHandler(Shapefile.GetShapeType(geometryCollection.Geometries[0])); IGeometry body; int numShapes = geometryCollection.NumGeometries; // calc the length of the shp file, so it can put in the header. int shpLength = 50; for (int i = 0; i < numShapes; i++) { body = (IGeometry)geometryCollection.Geometries[i]; shpLength += 4; // length of header in WORDS shpLength += handler.GetLength(body); // length of shape in WORDS } int shxLength = 50 + (4 * numShapes); // write the .shp header ShapefileHeader shpHeader = new ShapefileHeader(); shpHeader.FileLength = shpLength; // get envelope in external coordinates IEnvelope env = geometryCollection.EnvelopeInternal; IEnvelope bounds = ShapeHandler.GetEnvelopeExternal(geometryFactory.PrecisionModel, env); shpHeader.Bounds = bounds; // assumes Geometry type of the first item will the same for all other items // in the collection. shpHeader.ShapeType = Shapefile.GetShapeType(geometryCollection.Geometries[0]); shpHeader.Write(shpBinaryWriter); // write the .shx header ShapefileHeader shxHeader = new ShapefileHeader(); shxHeader.FileLength = shxLength; shxHeader.Bounds = shpHeader.Bounds; // assumes Geometry type of the first item will the same for all other items in the collection. shxHeader.ShapeType = Shapefile.GetShapeType(geometryCollection.Geometries[0]); shxHeader.Write(shxBinaryWriter); // write the individual records. int _pos = 50; // header length in WORDS for (int i = 0; i < numShapes; i++) { body = geometryCollection.Geometries[i]; int recordLength = handler.GetLength(body); shpBinaryWriter.WriteIntBE(i + 1); shpBinaryWriter.WriteIntBE(recordLength); shxBinaryWriter.WriteIntBE(_pos); shxBinaryWriter.WriteIntBE(recordLength); _pos += 4; // length of header in WORDS handler.Write(body, shpBinaryWriter, geometryFactory); _pos += recordLength; // length of shape in WORDS } shxBinaryWriter.Flush(); shxBinaryWriter.Close(); shpBinaryWriter.Flush(); shpBinaryWriter.Close(); // WriteDummyDbf(filename + ".dbf", numShapes); }