示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShapefileWriter">ShapefileWriter</see> class with the specified filename and factory .
        /// </summary>
        /// <param name="filename">The name of the file to write.</param>
        /// <param name="factory">The <b>GeometryFactory</b> to use.</param>
        /// <param name="append"></param>
        /// <exception cref="ArgumentNullException">The factory is a null reference (Nothing in Visual Basic).</exception>
        public ShapefileWriter(string filename, GeometryFactory factory, bool append)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            _factory = factory;
            string shxFilename = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + ".shx");


            if (append)
            {
                if (File.Exists(filename) == false || File.Exists(shxFilename) == false)
                {
                    throw new ArgumentException("Cannot append to a file that does not exist.");
                }

                ShapefileHeader shpHeader = null;
                ShapefileHeader shxHeader = null;
                using (Stream stream = File.Open(filename, FileMode.Open))
                {
                    using (BigEndianBinaryReader beBinaryReader = new BigEndianBinaryReader(stream))
                    {
                        shpHeader = new ShapefileHeader(beBinaryReader);
                    }
                }
                using (Stream stream = File.Open(shxFilename, FileMode.Open))
                {
                    using (BigEndianBinaryReader beBinaryReader = new BigEndianBinaryReader(stream))
                    {
                        shxHeader = new ShapefileHeader(beBinaryReader);
                    }
                }
                this._type      = shpHeader.ShapeType;
                this._shpLength = shpHeader.FileLength;
                this._bounds    = shpHeader.Bounds;
                this._count     = (shxHeader.FileLength - 50) / 4;


                _shpWriter = new BigEndianBinaryWriter(File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
                _shxWriter = new BigEndianBinaryWriter(File.Open(shxFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));

                // Write dummy headers as place holders
                this.WriteHeader(_shpWriter, 0);
                this.WriteHeader(_shxWriter, 0);
                _shpWriter.BaseStream.Position = _shpWriter.BaseStream.Length;
                _shxWriter.BaseStream.Position = _shxWriter.BaseStream.Length;
            }
            else
            {
                _shpWriter = new BigEndianBinaryWriter(File.Open(filename, FileMode.CreateNew));
                _shxWriter = new BigEndianBinaryWriter(File.Open(shxFilename, FileMode.CreateNew));

                // Write dummy headers as place holders
                this.WriteHeader(_shpWriter, 0);
                this.WriteHeader(_shxWriter, 0);
            }
        }
示例#2
0
        public void WriteHeader(BigEndianBinaryWriter writer, int length)
        {
            ShapefileHeader header = new ShapefileHeader();

            // Go to the start of the stream (if we're not already there)
            writer.BaseStream.Position = 0;

            // Set current header properties
            header.Bounds     = _bounds;
            header.FileLength = length;
            header.ShapeType  = _type;
            header.Write(writer);
        }
示例#3
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.setLittleEndianMode(false);
            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.setLittleEndianMode(true);
            file.Write(_version);
            pos += 4;

            file.Write(int.Parse(Enum.Format(typeof(ShapeType), _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
            //file.setLittleEndianMode(false);//well they may not be unused forever...
            for (int i = 0; i < 4; i++)
            {
                file.Write(0.0);                //Skip unused part of header
                pos += 8;
            }
            Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled, "Header pos:" + pos);
        }
示例#4
0
        /// <summary>
        /// Writes a shapefile header to the given stream;
        /// </summary>
        /// <param name="writer">The binary writer to use.</param>
        public void Write(BigEndianBinaryWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (_fileLength == -1)
            {
                throw new InvalidOperationException("The header properties need to be set before writing the header record.");
            }

            writer.WriteIntBE(_fileCode);

            for (int i = 0; i < 5; i++)
            {
                writer.WriteIntBE(0);                   // Skip unused part of header
            }

            writer.WriteIntBE(_fileLength);
            writer.Write(_version);
            writer.Write((int)_shapeType);

            // Write the bounding box
            writer.Write(_bounds.getMinX());
            writer.Write(_bounds.getMinY());
            writer.Write(_bounds.getMaxX());
            writer.Write(_bounds.getMaxY());

            // Skip remaining unused bytes
            for (int i = 0; i < 4; i++)
            {
                writer.Write(0.0);                      // Skip unused part of header
            }

            Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled, "Header position: " + writer.BaseStream.Position);
        }
		/// <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.setLittleEndianMode(false);
			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.setLittleEndianMode(true);
			file.Write(_version);
			pos += 4;
			
			file.Write(int.Parse(Enum.Format(typeof(ShapeType),_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
			//file.setLittleEndianMode(false);//well they may not be unused forever...
			for (int i = 0; i < 4; i++)
			{
				file.Write(0.0);//Skip unused part of header
				pos += 8;
			}
			Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled,"Header pos:"+pos);
		}
示例#6
0
        /// <summary>
        /// Writes a shapefile to disk.
        /// </summary>
        /// <remarks>
        /// <para>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.</para>
        /// <para>The dbase file for the corresponding shapefile contains one column called row. It contains
        /// the row number.</para>
        /// </remarks>
        /// <param name="filename">The filename to write to (minus the .shp extension).</param>
        /// <param name="geometryCollection">The GeometryCollection to write.</param>
        /// <param name="geometryFactory">The geometry factory to use.</param>
        public static void Write(string filename, GeometryCollection geometryCollection, GeometryFactory geometryFactory)
        {
            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[0]));

            Geometry body;
            int      numShapes = geometryCollection.GetNumGeometries();
            // 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       = geometryCollection[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 envelopse in external coordinates
            Envelope env    = geometryCollection.GetEnvelopeInternal();
            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[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[0]);
            shxHeader.Write(shxBinaryWriter);



            // write the individual records.
            int _pos = 50;             // header length in WORDS

            for (int i = 0; i < numShapes; i++)
            {
                body = geometryCollection[i];
                int recordLength = handler.GetLength(body);
                Debug.Assert(Shapefile.GetShapeType(body) != shpHeader.ShapeType, String.Format("Item {0} in the GeometryCollection is not the same Shapetype as Item 0.", i));
                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();

            Debug.Assert(_pos != shpLength, "File length in header and actual file length do not match.");
            //stream.Close();
            //Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled,"File length pos:"+_pos*2+" bytes");
            //Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled,"File length pos "+_pos+ " words");

            WriteDummyDbf(filename + ".dbf", numShapes);
        }
示例#7
0
        /// <summary>
        /// Writes a shapefile header to the given stream;
        /// </summary>
        /// <param name="writer">The binary writer to use.</param>
        public void Write(BigEndianBinaryWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (_fileLength == -1)
            {
                throw new InvalidOperationException("The header properties need to be set before writing the header record.");
            }

            writer.WriteIntBE(_fileCode);

            for (int i = 0; i < 5; i++)
            {
                writer.WriteIntBE(0);	// Skip unused part of header
            }

            writer.WriteIntBE(_fileLength);
            writer.Write(_version);
            writer.Write((int)_shapeType);

            // Write the bounding box
            writer.Write(_bounds.getMinX());
            writer.Write(_bounds.getMinY());
            writer.Write(_bounds.getMaxX());
            writer.Write(_bounds.getMaxY());

            // Skip remaining unused bytes
            for (int i = 0; i < 4; i++)
            {
                writer.Write(0.0);	// Skip unused part of header
            }

            Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled, "Header position: " + writer.BaseStream.Position);
        }
		/// <summary>
		/// Writes a shapefile to disk.
		/// </summary>
		/// <remarks>
		/// <para>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.</para>
		/// <para>The dbase file for the corresponding shapefile contains one column called row. It contains 
		/// the row number.</para>
		/// </remarks>
		/// <param name="filename">The filename to write to (minus the .shp extension).</param>
		/// <param name="geometryCollection">The GeometryCollection to write.</param>
		/// <param name="geometryFactory">The geometry factory to use.</param>
		public static void Write(string filename, GeometryCollection geometryCollection, GeometryFactory geometryFactory)
		{
			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[0]));

			Geometry body;
			int numShapes = geometryCollection.GetNumGeometries();
			// 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 = geometryCollection[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 envelopse in external coordinates
			Envelope env = geometryCollection.GetEnvelopeInternal();
			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[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[0] );
			shxHeader.Write(shxBinaryWriter);



			// write the individual records.
			int _pos = 50; // header length in WORDS
			for (int i = 0; i < numShapes; i++) 
			{
				body = geometryCollection[i];
				int recordLength = handler.GetLength(body);
				Debug.Assert( Shapefile.GetShapeType(body)!=shpHeader.ShapeType, String.Format("Item {0} in the GeometryCollection is not the same Shapetype as Item 0.",i));
				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();

			Debug.Assert(_pos!=shpLength,"File length in header and actual file length do not match.");
			//stream.Close();
			//Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled,"File length pos:"+_pos*2+" bytes");
			//Trace.WriteLineIf(Shapefile.TraceSwitch.Enabled,"File length pos "+_pos+ " words");

			WriteDummyDbf(filename+".dbf", numShapes);	
		}
示例#9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShapefileWriter">ShapefileWriter</see> class with the specified filename and factory .
        /// </summary>
        /// <param name="filename">The name of the file to write.</param>
        /// <param name="factory">The <b>GeometryFactory</b> to use.</param>
        /// <param name="append"></param>
        /// <exception cref="ArgumentNullException">The factory is a null reference (Nothing in Visual Basic).</exception>
        public ShapefileWriter(string filename, GeometryFactory factory, bool append)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

            _factory = factory;
            string shxFilename = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + ".shx");

            if (append)
            {
                if (File.Exists(filename)==false || File.Exists(shxFilename)==false)
                {
                    throw new ArgumentException("Cannot append to a file that does not exist.");
                }

                ShapefileHeader shpHeader = null;
                ShapefileHeader shxHeader = null;
                using (Stream stream = File.Open(filename, FileMode.Open))
                {
                    using (BigEndianBinaryReader beBinaryReader = new BigEndianBinaryReader(stream))
                    {
                        shpHeader = new ShapefileHeader(beBinaryReader);
                    }
                }
                using (Stream stream = File.Open(shxFilename, FileMode.Open))
                {
                    using (BigEndianBinaryReader beBinaryReader = new BigEndianBinaryReader(stream))
                    {
                        shxHeader = new ShapefileHeader(beBinaryReader);
                    }
                }
                this._type = shpHeader.ShapeType;
                this._shpLength = shpHeader.FileLength;
                this._bounds = shpHeader.Bounds;
                this._count = (shxHeader.FileLength-50)/4;

                _shpWriter = new BigEndianBinaryWriter(File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
                _shxWriter = new BigEndianBinaryWriter(File.Open(shxFilename, FileMode.Open,FileAccess.ReadWrite, FileShare.ReadWrite));

                // Write dummy headers as place holders
                this.WriteHeader(_shpWriter,0);
                this.WriteHeader(_shxWriter,0);
                _shpWriter.BaseStream.Position = _shpWriter.BaseStream.Length;
                _shxWriter.BaseStream.Position = _shxWriter.BaseStream.Length;

            }
            else
            {
                _shpWriter = new BigEndianBinaryWriter(File.Open(filename, FileMode.CreateNew));
                _shxWriter = new BigEndianBinaryWriter(File.Open(shxFilename, FileMode.CreateNew));

                // Write dummy headers as place holders
                this.WriteHeader(_shpWriter,0);
                this.WriteHeader(_shxWriter,0);
            }
        }
示例#10
0
        public void WriteHeader(BigEndianBinaryWriter writer, int length)
        {
            ShapefileHeader header = new ShapefileHeader();

            // Go to the start of the stream (if we're not already there)
            writer.BaseStream.Position = 0;

            // Set current header properties
            header.Bounds = _bounds;
            header.FileLength = length;
            header.ShapeType = _type;
            header.Write(writer);
        }