public ShapefileDataReader(IStreamProviderRegistry streamProviderRegistry, IGeometryFactory geometryFactory)
        {
            if (streamProviderRegistry==null)
                throw new ArgumentNullException("streamProviderRegistry");
            if (geometryFactory == null)
                throw new ArgumentNullException("geometryFactory");
            _open = true;

            _dbfReader = new DbaseFileReader(streamProviderRegistry);
            _shpReader = new ShapefileReader(streamProviderRegistry, geometryFactory);

            _dbfHeader = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            for (int i = 0; i < _dbfHeader.Fields.Length; i++)
                _dbaseFields[i + 1] = _dbfHeader.Fields[i];

            _shpHeader = _shpReader.Header;
            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();
            _moreRecords = true;
        }
        /// <summary>
        /// Initializes a new instance of the ShapefileDataReader class.
        /// </summary>
        /// <param name="filename">The shapefile to read (minus the .shp extension)</param>
        ///<param name="geometryFactory">The GeometryFactory to use.</param>
        public ShapefileDataReader(string filename, IGeometryFactory geometryFactory)
        {
            if (String.IsNullOrEmpty(filename))
                throw new ArgumentNullException("filename");
            if (geometryFactory == null)
                throw new ArgumentNullException("geometryFactory");
            _open = true;

            string dbfFile = Path.ChangeExtension(filename, "dbf");
            _dbfReader = new DbaseFileReader(dbfFile);
            string shpFile = Path.ChangeExtension(filename, "shp");
            _shpReader = new ShapefileReader(shpFile, geometryFactory);

            _dbfHeader = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            for (int i = 0; i < _dbfHeader.Fields.Length; i++)
                _dbaseFields[i + 1] = _dbfHeader.Fields[i];

            _shpHeader = _shpReader.Header;
            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();
            _moreRecords = true;
        }
        /// <summary>
        /// Initializes a new instance of the Shapefile class with the given parameters.
        /// </summary>
        /// <param name="filename">The filename of the shape file to read (with .shp).</param>
        /// <param name="geometryFactory">The GeometryFactory to use when creating Geometry objects.</param>
        public ShapefileReader(string filename, IGeometryFactory geometryFactory)
        {
            Guard.IsNotNull(filename, "filename");
            Guard.IsNotNull(geometryFactory, "geometryFactory");

            _filename = filename;
            _geometryFactory = geometryFactory;

            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // read header information. note, we open the file, read the header information and then
                // close the file. This means the file is not opened again until GetEnumerator() is requested.
                // For each call to GetEnumerator() a new BinaryReader is created.
                using (
                    var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read,
                                                               isf))
                {
                    using (var shpBinaryReader = new BigEndianBinaryReader(stream))
                    {
                        _mainHeader = new ShapefileHeader(shpBinaryReader);
                        shpBinaryReader.Close();
                    }
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the Shapefile class with the given parameters.
        /// </summary>
        /// <param name="filename">The filename of the shape file to read (with .shp).</param>
        /// <param name="geometryFactory">The GeometryFactory to use when creating Geometry objects.</param>
        public ShapefileReader(string filename, IGeometryFactory geometryFactory)
        {
            if (filename == null)
                throw new ArgumentNullException("filename");
            if (geometryFactory == null)
                throw new ArgumentNullException("geometryFactory");

            _filename = filename;
            _geometryFactory = geometryFactory;

            // read header information. note, we open the file, read the header information and then
            // close the file. This means the file is not opened again until GetEnumerator() is requested.
            // For each call to GetEnumerator() a new BinaryReader is created.
            var stream = IoManager.File.CreateFileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            var shpBinaryReader = new BigEndianBinaryReader(stream);
            _mainHeader = new ShapefileHeader(shpBinaryReader);
            shpBinaryReader.Close();
        }
        public ShapefileReader(IStreamProviderRegistry shapeStreamProviderRegistry, IGeometryFactory geometryFactory)
        {
            if (shapeStreamProviderRegistry == null)
                throw new ArgumentNullException("shapeStreamProviderRegistry");
            if (geometryFactory == null)
                throw new ArgumentNullException("geometryFactory");

            _shapeStreamProviderRegistry = shapeStreamProviderRegistry;
            _geometryFactory = geometryFactory;

            // read header information. note, we open the file, read the header information and then
            // close the file. This means the file is not opened again until GetEnumerator() is requested.
            // For each call to GetEnumerator() a new BinaryReader is created.
            using (var stream = shapeStreamProviderRegistry[StreamTypes.Shape].OpenRead())
            {
                using (var shpBinaryReader = new BigEndianBinaryReader(stream))
                {
                    _mainHeader = new ShapefileHeader(shpBinaryReader);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the ShapefileDataReader class.
        /// </summary>
        /// <param name="filename">The shapefile to read (minus the .shp extension)</param>
        /// <param name="geometryFactory">The GeometryFactory to use.</param>
        /// <param name="encoding">The encoding to use for reading the attribute data</param>
        public ShapefileDataReader(string filename, IGeometryFactory geometryFactory, Encoding encoding)
        {
            if (String.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }
            if (geometryFactory == null)
            {
                throw new ArgumentNullException("geometryFactory");
            }

            _open = true;

            string dbfFile = Path.ChangeExtension(filename, "dbf");

            _dbfReader = encoding != null
                ? new DbaseFileReader(dbfFile, encoding)
                : new DbaseFileReader(dbfFile);

            string shpFile = Path.ChangeExtension(filename, "shp");

            _shpReader = new ShapefileReader(shpFile, geometryFactory);

            _dbfHeader   = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields    = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            for (int i = 0; i < _dbfHeader.Fields.Length; i++)
            {
                _dbaseFields[i + 1] = _dbfHeader.Fields[i];
            }

            _shpHeader     = _shpReader.Header;
            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();
            _moreRecords   = true;
        }
        /// <summary>
        /// Initializes a new instance of the Shapefile class with the given parameters.
        /// </summary>
        /// <param name="filename">The filename of the shape file to read (with .shp).</param>
        /// <param name="geometryFactory">The GeometryFactory to use when creating Geometry objects.</param>
        public ShapefileReader(string filename, IGeometryFactory geometryFactory)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            if (geometryFactory == null)
            {
                throw new ArgumentNullException("geometryFactory");
            }

            _filename        = filename;
            _geometryFactory = geometryFactory;

            // read header information. note, we open the file, read the header information and then
            // close the file. This means the file is not opened again until GetEnumerator() is requested.
            // For each call to GetEnumerator() a new BinaryReader is created.
            var stream          = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            var shpBinaryReader = new BigEndianBinaryReader(stream);

            _mainHeader = new ShapefileHeader(shpBinaryReader);
            shpBinaryReader.Close();
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the ShapefileDataReader class.
        /// </summary>
        /// <param name="filename">The shapefile to read (minus the .shp extension)</param>
        ///<param name="geometryFactory">The GeometryFactory to use.</param>
        public ShapefileDataReader(string filename, IGeometryFactory geometryFactory)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            if (geometryFactory == null)
            {
                throw new ArgumentNullException("geometryFactory");
            }
            _open = true;

            if (filename.ToLower().EndsWith(".shp"))
            {
                filename = filename.ToLower().Replace(".shp", String.Empty);
            }

            _dbfReader = new DbaseFileReader(filename + ".dbf");
            _shpReader = new ShapefileReader(filename + ".shp", geometryFactory);

            _dbfHeader   = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields    = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            for (int i = 0; i < _dbfHeader.Fields.Length; i++)
            {
                _dbaseFields[i + 1] = _dbfHeader.Fields[i];
            }

            _shpHeader     = _shpReader.Header;
            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();
            _moreRecords   = true;
        }
        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);
        }       
 public virtual void VisitMainFileHeader(ref ShapefileHeader header)
 {
 }
        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);
        }
Example #12
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(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);
        }