예제 #1
0
        /// <summary>
        /// Writes a Geometry to the given binary wirter.
        /// </summary>
        /// <param name="geometry">The geometry to write.</param>
        /// <param name="file">The file stream to write to.</param>
        /// <param name="geometryFactory">The geometry factory to use.</param>
        public override void Write(IGeometry geometry, System.IO.BinaryWriter file, IGeometryFactory geometryFactory)
        {
            if (!(geometry is IMultiPoint))
            {
                throw new ArgumentException("Geometry Type error: MultiPoint expected, but the type retrieved is " + geometry.GetType().Name);
            }

            // Slow and maybe not useful...
            // if (!geometry.IsValid)
            //  Trace.WriteLine("Invalid multipoint being written.");

            IMultiPoint mpoint = geometry as IMultiPoint;

            file.Write(int.Parse(Enum.Format(typeof(ShapeGeometryTypes), this.ShapeType, "d")));

            IEnvelope box    = geometry.EnvelopeInternal;
            IEnvelope bounds = ShapeHandler.GetEnvelopeExternal(geometryFactory.PrecisionModel, box);

            file.Write(bounds.MinX);
            file.Write(bounds.MinY);
            file.Write(bounds.MaxX);
            file.Write(bounds.MaxY);

            int numPoints = mpoint.NumPoints;

            file.Write(numPoints);

            // write the points
            for (int i = 0; i < numPoints; i++)
            {
                IPoint point = (IPoint)mpoint.Geometries[i];
                file.Write(point.X);
                file.Write(point.Y);
            }
        }
예제 #2
0
            /// <summary>
            /// Initializes a new instance of the ShapefileEnumerator class.
            /// </summary>
            /// <param name="shapefile"></param>
            public ShapefileEnumerator(ShapefileReader shapefile)
            {
                _parent = shapefile;

                // create a file stream for each enumerator that is given out. This allows the same file
                // to have one or more enumerator. If we used the parents stream - than only one IEnumerator
                // could be given out.
                FileStream stream = new FileStream(_parent._filename, System.IO.FileMode.Open, FileAccess.Read, FileShare.Read);

                _shpBinaryReader = new BigEndianBinaryReader(stream);

                // skip header - since parent has already read this.
                _shpBinaryReader.ReadBytes(100);
                ShapeGeometryTypes type = _parent._mainHeader.ShapeType;

                _handler = Shapefile.GetShapeHandler(type);
                if (_handler == null)
                {
                    throw new NotSupportedException("Unsuported shape type:" + type);
                }
            }
예제 #3
0
        /// <summary>
        /// Reads the shapefile and returns a GeometryCollection representing all the records in the shapefile.
        /// </summary>
        /// <returns>GeometryCollection representing every record in the shapefile.</returns>
        public IGeometryCollection ReadAll()
        {
            ArrayList          list    = new ArrayList();
            ShapeGeometryTypes type    = _mainHeader.ShapeType;
            ShapeHandler       handler = Shapefile.GetShapeHandler(type);

            if (handler == null)
            {
                throw new NotSupportedException("Unsupported shape type:" + type);
            }

            int i = 0;

            foreach (IGeometry geometry in this)
            {
                list.Add(geometry);
                i++;
            }

            IGeometry[] geomArray = GeometryFactory.ToGeometryArray(list);
            return(_geometryFactory.CreateGeometryCollection(geomArray));
        }
예제 #4
0
        /// <summary>
        /// Writes a Geometry to the given binary wirter.
        /// </summary>
        /// <param name="geometry">The geometry to write.</param>
        /// <param name="file">The file stream to write to.</param>
        /// <param name="geometryFactory">The geometry factory to use.</param>
        public override void Write(IGeometry geometry, System.IO.BinaryWriter file, IGeometryFactory geometryFactory)
        {
            // This check seems to be not useful and slow the operations...
            // if (!geometry.IsValid)
            // Trace.WriteLine("Invalid polygon being written.");

            IGeometryCollection multi = null;

            if (geometry is IGeometryCollection)
            {
                multi = (IGeometryCollection)geometry;
            }
            else
            {
                GeometryFactory gf = new GeometryFactory(geometry.PrecisionModel);
                multi = gf.CreateMultiPolygon(new IPolygon[] { (IPolygon)geometry, });
            }

            file.Write(int.Parse(Enum.Format(typeof(ShapeGeometryTypes), this.ShapeType, "d")));

            IEnvelope box    = multi.EnvelopeInternal;
            IEnvelope bounds = ShapeHandler.GetEnvelopeExternal(geometryFactory.PrecisionModel, box);

            file.Write(bounds.MinX);
            file.Write(bounds.MinY);
            file.Write(bounds.MaxX);
            file.Write(bounds.MaxY);

            int numParts  = GetNumParts(multi);
            int numPoints = multi.NumPoints;

            file.Write(numParts);
            file.Write(numPoints);

            // write the offsets to the points
            int offset = 0;

            for (int part = 0; part < multi.NumGeometries; part++)
            {
                // offset to the shell points
                IPolygon polygon = (IPolygon)multi.Geometries[part];
                file.Write(offset);
                offset = offset + polygon.ExteriorRing.NumPoints;

                // offstes to the holes
                foreach (ILinearRing ring in polygon.InteriorRings)
                {
                    file.Write(offset);
                    offset = offset + ring.NumPoints;
                }
            }

            // write the points
            for (int part = 0; part < multi.NumGeometries; part++)
            {
                IPolygon      poly   = (IPolygon)multi.Geometries[part];
                ICoordinate[] points = poly.ExteriorRing.Coordinates;
                WriteCoords(new CoordinateList(points), file, geometryFactory);
                foreach (ILinearRing ring in poly.InteriorRings)
                {
                    ICoordinate[] points2 = ring.Coordinates;
                    WriteCoords(new CoordinateList(points2), file, geometryFactory);
                }
            }
        }
예제 #5
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 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);
        }
예제 #6
0
            /// <summary>
            /// Initializes a new instance of the ShapefileEnumerator class.
            /// </summary>
            /// <param name="shapefile"></param>
            public ShapefileEnumerator(ShapefileReader shapefile)
            {
                _parent = shapefile;

                // create a file stream for each enumerator that is given out. This allows the same file
                // to have one or more enumerator. If we used the parents stream - than only one IEnumerator
                // could be given out.
                FileStream stream = new FileStream(_parent._filename, System.IO.FileMode.Open, FileAccess.Read, FileShare.Read);
                _shpBinaryReader = new BigEndianBinaryReader(stream);

                // skip header - since parent has already read this.
                _shpBinaryReader.ReadBytes(100);
                ShapeGeometryTypes type = _parent._mainHeader.ShapeType;
                _handler = Shapefile.GetShapeHandler(type);
                if (_handler == null)
                    throw new NotSupportedException("Unsuported shape type:" + type);
            }