/// <summary>
        ///
        /// </summary>
        /// <param name="geometry"></param>
        /// <param name="leWriter"></param>
        /// <param name="beWriter"></param>
        protected virtual void Write(Geometry geometry, BinaryWriter leWriter, BigEndianBinaryWriter beWriter)
        {
            WriteFeatureHeader(geometry, beWriter);

            if (geometry is Point)
            {
                shapeWriter.Write(geometry as Point, leWriter);
            }
            else if (geometry is LineString)
            {
                shapeWriter.Write(geometry as LineString, leWriter);
            }
            else if (geometry is Polygon)
            {
                shapeWriter.Write(geometry as Polygon, leWriter);
            }
            else if (geometry is MultiPoint)
            {
                shapeWriter.Write(geometry as MultiPoint, leWriter);
            }
            else if (geometry is MultiLineString)
            {
                shapeWriter.Write(geometry as MultiLineString, leWriter);
            }
            else if (geometry is MultiPolygon)
            {
                shapeWriter.Write(geometry as MultiPolygon, leWriter);
            }
            else
            {
                throw new NotSupportedException("Unsupported Geometry implementation:" + geometry.GetType());
            }
        }
        /// <summary>
        /// Writes an homogeneus <c>GometryCollection</c> into a binary stream.
        /// </summary>
        /// <param name="geometries"></param>
        /// <param name="stream"></param>
        protected virtual void Write(GeometryCollection geometries, Stream stream)
        {
            if (!geometries.IsHomogeneous)
            {
                throw new ArgumentException("Shapefile does contain only geometries of th esame type!");
            }

            // Init counter
            recordCounter = 0;

            using (BigEndianBinaryWriter beWriter = new BigEndianBinaryWriter(stream))
            {
                using (BinaryWriter leWriter = new BinaryWriter(stream))
                {
                    // Evaluate stream length
                    int fileLength = 0;
                    foreach (Geometry geometry in geometries.Geometries)
                    {
                        fileLength += (shapeWriter.GetBytesLength(geometry) + 8);           // 12 is the length of record header
                    }
                    fileLength += 100;                                                      // Add main header
                    WriteHeaderFile(geometries, leWriter, beWriter, (int)(fileLength / 2)); // Write length in 16 bit words!

                    foreach (Geometry geometry in geometries.Geometries)
                    {
                        Write(geometry, leWriter, beWriter);
                    }
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="geometry"></param>
        /// <param name="beWriter"></param>
        protected virtual 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!
        }
예제 #4
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>
        ///
        /// </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 virtual 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(geometries.Geometries[0]));
            // Write values for boundingbox
            IEnvelope 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 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 virtual void Write(string filename, GeometryCollection 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       = 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(new PrecisionModel(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);
        }