private static /*int*/ void WriteRecordToFile(BigEndianBinaryWriter shpBinaryWriter, BigEndianBinaryWriter shxBinaryWriter, ShapeHandler handler, IGeometry body, int oid) { if (body == null || body.IsEmpty) { WriteNullShapeRecord(shpBinaryWriter, shxBinaryWriter, oid); return; } // Get the length of each record (in bytes) var recordLength = handler.ComputeRequiredLengthInWords(body); // Get the position in the stream var pos = shpBinaryWriter.BaseStream.Position; shpBinaryWriter.WriteIntBE(oid); shpBinaryWriter.WriteIntBE(recordLength); // update shapefile index (position in words, 1 word = 2 bytes) var posWords = pos / 2; if (shxBinaryWriter != null) { shxBinaryWriter.WriteIntBE((int)posWords); shxBinaryWriter.WriteIntBE(recordLength); } handler.Write(body, shpBinaryWriter, body.Factory); /*return recordLength;*/ }
private void WriteShxHeader(BigEndianBinaryWriter shxBinaryWriter, int shxLength, Envelope bounds) { // write the .shx header var shxHeader = new ShapefileHeader { FileLength = shxLength, Bounds = NotNull(bounds), ShapeType = _geometryType }; // assumes Geometry type of the first item will the same for all other items in the collection. shxHeader.Write(shxBinaryWriter); }
private static void WriteShpHeader(BigEndianBinaryWriter shpBinaryWriter, int shpLength, Envelope bounds, ShapeGeometryType shapeType) { var shpHeader = new ShapefileHeader { FileLength = shpLength, Bounds = bounds, ShapeType = shapeType }; // assumes Geometry type of the first item will the same for all other items // in the collection. shpHeader.Write(shpBinaryWriter); }
private static void WriteNullShapeRecord(BigEndianBinaryWriter shpBinaryWriter, BigEndianBinaryWriter shxBinaryWriter, int oid) { const int recordLength = 12; // Add shape shpBinaryWriter.WriteIntBE(oid); shpBinaryWriter.WriteIntBE(recordLength); shpBinaryWriter.Write((int)ShapeGeometryType.NullShape); // Update shapefile index (position in words, 1 word = 2 bytes) var posWords = shpBinaryWriter.BaseStream.Position / 2; shxBinaryWriter.WriteIntBE((int)posWords); shxBinaryWriter.WriteIntBE(recordLength); }
public ShapefileWriter(IGeometryFactory geometryFactory, IStreamProviderRegistry streamProviderRegistry, ShapeGeometryType geomType) : this(geometryFactory) { _shpStream = streamProviderRegistry[StreamTypes.Shape].OpenWrite(true); _shxStream = streamProviderRegistry[StreamTypes.Index].OpenWrite(true); _geometryType = geomType; _shpBinaryWriter = new BigEndianBinaryWriter(_shpStream); _shxBinaryWriter = new BigEndianBinaryWriter(_shxStream); WriteShpHeader(_shpBinaryWriter, 0, new Envelope(0, 0, 0, 0)); WriteShxHeader(_shxBinaryWriter, 0, new Envelope(0, 0, 0, 0)); _shapeHandler = Shapefile.GetShapeHandler(geomType); }
public ShapefileWriter(IGeometryFactory geometryFactory, IStreamProviderRegistry streamProviderRegistry, ShapeGeometryType geomType) : this(geometryFactory) { _shpStream = streamProviderRegistry[StreamTypes.Shape].OpenWrite(true); _shxStream = streamProviderRegistry[StreamTypes.Index].OpenWrite(true); _geometryType = geomType; _shpBinaryWriter = new BigEndianBinaryWriter(_shpStream); _shxBinaryWriter = new BigEndianBinaryWriter(_shxStream); WriteShpHeader(_shpBinaryWriter, 0, new Envelope(0, 0, 0, 0)); WriteShxHeader(_shxBinaryWriter, 0, new Envelope(0, 0, 0, 0)); _shapeHandler = Shapefile.GetShapeHandler(geomType); }
public void Close() { if (_shpBinaryWriter != null) { /*Update header to reflect the data written*/ _shpStream.Seek(0, SeekOrigin.Begin); int shpLenWords = (int)_shpBinaryWriter.BaseStream.Length / 2; // Write the SHP header at the beginning of the file to update the dummy/stale header WriteShpHeader(_shpBinaryWriter, shpLenWords, _totalEnvelope); _shpStream.Seek(0, SeekOrigin.End); if (_shxStream != null) { _shxStream.Seek(0, SeekOrigin.Begin); int shxLenWords = (int)_shxBinaryWriter.BaseStream.Length / 2; WriteShxHeader(_shxBinaryWriter, shxLenWords, _totalEnvelope); _shxStream.Seek(0, SeekOrigin.End); } } if (_shpBinaryWriter != null) { _shpBinaryWriter.Close(); _shpBinaryWriter = null; } if (_shxBinaryWriter != null) { _shxBinaryWriter.Close(); _shxBinaryWriter = null; } if (_shpStream != null) { _shpStream.Close(); _shpStream = null; } if (_shxStream != null) { _shxStream.Close(); _shxStream = null; } }
/// <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."); } var pos = 0; file.WriteIntBE(_fileCode); pos += 4; for (var 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; var format = EnumUtility.Format(typeof(ShapeGeometryType), _shapeType, "d"); file.Write(int.Parse(format)); 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; } }
public void Close() { if (_shpBinaryWriter != null) { /*Update header to reflect the data written*/ _shpStream.Seek(0, SeekOrigin.Begin); var shpLenWords = (int)_shpBinaryWriter.BaseStream.Length / 2; _shpStream.Seek(0, SeekOrigin.End); WriteShpHeader(_shpBinaryWriter, shpLenWords, _totalEnvelope); if (_shxStream != null) { _shxStream.Seek(0, SeekOrigin.Begin); var shxLenWords = (int)_shxBinaryWriter.BaseStream.Length / 2; WriteShxHeader(_shxBinaryWriter, shxLenWords, _totalEnvelope); _shxStream.Seek(0, SeekOrigin.End); } } if (_shpBinaryWriter != null) { _shpBinaryWriter.Close(); _shpBinaryWriter = null; } if (_shxBinaryWriter != null) { _shxBinaryWriter.Close(); _shxBinaryWriter = null; } if (_shpStream != null) { _shpStream.Close(); _shpStream = null; } if (_shxStream != null) { _shxStream.Close(); _shxStream = null; } }
public ShapefileWriter(IGeometryFactory geometryFactory, string filename, ShapeGeometryType geomType) : this(geometryFactory) { var folder = Path.GetDirectoryName(filename) ?? "."; var file = Path.GetFileNameWithoutExtension(filename); if (string.IsNullOrEmpty(file)) throw new ArgumentException(string.Format("Filename '{0}' is not valid", filename), "filename"); filename = Path.Combine(folder, file); _shpStream = new FileStream(filename + ".shp", FileMode.Create); _shxStream = new FileStream(filename + ".shx", FileMode.Create); _geometryType = geomType; _shpBinaryWriter = new BigEndianBinaryWriter(_shpStream); _shxBinaryWriter = new BigEndianBinaryWriter(_shxStream); WriteShpHeader(_shpBinaryWriter, 0, new Envelope(0, 0, 0, 0), geomType); WriteShxHeader(_shxBinaryWriter, 0, new Envelope(0, 0, 0, 0), geomType); _shapeHandler = Shapefile.GetShapeHandler(geomType); }
public ShapefileWriter(IGeometryFactory geometryFactory, string filename, ShapeGeometryType geomType) : this(geometryFactory) { var folder = Path.GetDirectoryName(filename) ?? "."; var file = Path.GetFileNameWithoutExtension(filename); if (string.IsNullOrEmpty(file)) { throw new ArgumentException(string.Format("Filename '{0}' is not valid", filename), "filename"); } filename = Path.Combine(folder, file); _shpStream = new FileStream(filename + ".shp", FileMode.Create); _shxStream = new FileStream(filename + ".shx", FileMode.Create); _geometryType = geomType; _shpBinaryWriter = new BigEndianBinaryWriter(_shpStream); _shxBinaryWriter = new BigEndianBinaryWriter(_shxStream); WriteShpHeader(_shpBinaryWriter, 0, new Envelope(0, 0, 0, 0), geomType); WriteShxHeader(_shxBinaryWriter, 0, new Envelope(0, 0, 0, 0), geomType); _shapeHandler = Shapefile.GetShapeHandler(geomType); }
private static void WriteShpHeader(BigEndianBinaryWriter shpBinaryWriter, int shpLength, Envelope bounds, ShapeGeometryType shapeType) { var shpHeader = new ShapefileHeader {FileLength = shpLength, Bounds = bounds, ShapeType = shapeType}; // assumes Geometry type of the first item will the same for all other items // in the collection. shpHeader.Write(shpBinaryWriter); }
private static /*int*/ void WriteRecordToFile(BigEndianBinaryWriter shpBinaryWriter, BigEndianBinaryWriter shxBinaryWriter, ShapeHandler handler, IGeometry body, int oid) { if (body == null || body.IsEmpty) { WriteNullShapeRecord(shpBinaryWriter, shxBinaryWriter, oid); return; } // Get the length of each record (in bytes) var recordLength = handler.ComputeRequiredLengthInWords(body); // Get the position in the stream var pos = shpBinaryWriter.BaseStream.Position; shpBinaryWriter.WriteIntBE(oid); shpBinaryWriter.WriteIntBE(recordLength); // update shapefile index (position in words, 1 word = 2 bytes) var posWords = pos / 2; shxBinaryWriter.WriteIntBE((int)posWords); shxBinaryWriter.WriteIntBE(recordLength); handler.Write(body, shpBinaryWriter, body.Factory); /*return recordLength;*/ }
private static void WriteNullShapeRecord(BigEndianBinaryWriter shpBinaryWriter, BigEndianBinaryWriter shxBinaryWriter, int oid) { const int recordLength = 12; // Add shape shpBinaryWriter.WriteIntBE(oid); shpBinaryWriter.WriteIntBE(recordLength); shpBinaryWriter.Write((int)ShapeGeometryType.NullShape); // Update shapefile index (position in words, 1 word = 2 bytes) var posWords = shpBinaryWriter.BaseStream.Position / 2; shxBinaryWriter.WriteIntBE((int)posWords); shxBinaryWriter.WriteIntBE(recordLength); }
private void WriteShxHeader(BigEndianBinaryWriter shxBinaryWriter, int shxLength, Envelope bounds) { // write the .shx header var shxHeader = new ShapefileHeader { FileLength = shxLength, Bounds = NotNull(bounds), ShapeType = _geometryType }; // assumes Geometry type of the first item will the same for all other items in the collection. shxHeader.Write(shxBinaryWriter); }
/// <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."); var pos = 0; file.WriteIntBE(_fileCode); pos += 4; for (var 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(EnumUtility.Format(typeof(ShapeGeometryType), _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; } }
public void Close() { if (_shpBinaryWriter != null) { /*Update header to reflect the data written*/ _shpStream.Seek(0, SeekOrigin.Begin); _shxStream.Seek(0, SeekOrigin.Begin); var shpLenWords = (int)_shpBinaryWriter.BaseStream.Length / 2; var shxLenWords = (int)_shxBinaryWriter.BaseStream.Length / 2; WriteShpHeader(_shpBinaryWriter, shpLenWords, _totalEnvelope, _geometryType); WriteShxHeader(_shxBinaryWriter, shxLenWords, _totalEnvelope, _geometryType); _shpStream.Seek(0, SeekOrigin.End); _shxStream.Seek(0, SeekOrigin.End); } if (_shpBinaryWriter != null) { _shpBinaryWriter.Close(); _shpBinaryWriter = null; } if (_shxBinaryWriter != null) { _shxBinaryWriter.Close(); _shxBinaryWriter = null; } if (_shpStream != null) { _shpStream.Close(); _shpStream = null; } if (_shxStream != null) { _shxStream.Close(); _shxStream = null; } }
/// <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(IGeometryCollection geometryCollection) { //FileStream shpStream = new FileStream(filename + ".shp", FileMode.Create); //FileStream shxStream = new FileStream(filename + ".shx", FileMode.Create); shpStream = new MemoryStream(); shxStream = new MemoryStream(); 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 Envelope env = geometryCollection.EnvelopeInternal as Envelope; Envelope 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(); //shxStream.Seek(0, SeekOrigin.Begin); ////shxBinaryWriter.Close(); shpBinaryWriter.Flush(); //shpStream.Seek(0, SeekOrigin.Begin); //shpBinaryWriter.Close(); // WriteDummyDbf(filename + ".dbf", numShapes); }