/// <summary>
        /// Method to write a dummy dbase file
        /// </summary>
        /// <param name="dbfWriter">The dbase file writer</param>
        /// <param name="recordCount">The number of records</param>
        public static void WriteDummyDbf(DbaseFileWriter dbfWriter, int recordCount)
        {
            // Create the dummy header
            var dbfHeader = new DbaseFileHeader {
                NumRecords = recordCount
            };

            // add some dummy column
            dbfHeader.AddColumn("Description", 'C', 20, 0);

            // Write the header
            dbfWriter.Write(dbfHeader);
            // Write the features
            for (int i = 0; i < recordCount; i++)
            {
                var columnValues = new List <double> {
                    i
                };
                dbfWriter.Write(columnValues);
            }

            // End of file flag (0x1A)
            dbfWriter.WriteEndOfDbf();

            dbfWriter.Close();
        }
        /// <summary>
        /// Writes the specified feature collection.
        /// </summary>
        /// <param name="featureCollection">The feature collection.</param>
        public void Write(IEnumerable <IFeature> featureCollection)
        {
            // Test if the Header is initialized
            if (Header is null)
            {
                throw new ApplicationException("Header must be set first!");
            }

            using (var featuresEnumerator = featureCollection.GetEnumerator())
            {
                // scan the original sequence looking for a geometry that we can
                // use to figure out the shape type.  keep the original features
                // around so we don't have to loop through the input twice; we
                // shouldn't have to look *too* far for a non-empty geometry.
                Geometry representativeGeometry = null;
                var      headFeatures           = new List <IFeature>();
                while (representativeGeometry?.IsEmpty != false && featuresEnumerator.MoveNext())
                {
                    var feature = featuresEnumerator.Current;
                    headFeatures.Add(feature);
                    representativeGeometry = feature.Geometry;
                }

                var shapeFileType = Shapefile.GetShapeType(representativeGeometry);
                using (_dbaseWriter)
                    using (var shapefileWriter = new ShapefileWriter(_geometryFactory, _streamProviderRegistry, shapeFileType))
                    {
                        _dbaseWriter.Write(Header);
                        string[] fieldNames = Array.ConvertAll(Header.Fields, field => field.Name);
                        object[] values     = new object[fieldNames.Length];

                        // first, write the one(s) that we scanned already.
                        foreach (var feature in headFeatures)
                        {
                            Write(feature);
                        }

                        // now continue through the features we haven't scanned yet.
                        while (featuresEnumerator.MoveNext())
                        {
                            Write(featuresEnumerator.Current);
                        }

                        void Write(IFeature feature)
                        {
                            shapefileWriter.Write(feature.Geometry);

                            var attribs = feature.Attributes;

                            for (int i = 0; i < fieldNames.Length; i++)
                            {
                                values[i] = attribs[fieldNames[i]];
                            }

                            _dbaseWriter.Write(values);
                        }
                    }
            }
        }
        /// <summary>
        /// Writes the specified feature collection.
        /// </summary>
        /// <param name="featureCollection">The feature collection.</param>
        public void Write(IList <IFeature> featureCollection)
        {
            // Test if the Header is initialized
            if (Header == null)
            {
                throw new ApplicationException("Header must be set first!");
            }

#if DEBUG
            // Test if all elements of the collections are features
            foreach (object obj in featureCollection)
            {
                if (obj.GetType().IsAssignableFrom(typeof(IFeature)))
                {
                    throw new ArgumentException("All the elements in the given collection must be " + typeof(IFeature).Name);
                }
            }
#endif

            try
            {
                // Write shp and shx
                var geometries = new IGeometry[featureCollection.Count];
                var index      = 0;
                foreach (IFeature feature in featureCollection)
                {
                    geometries[index++] = feature.Geometry;
                }
                ShapefileWriter.WriteGeometryCollection(_shpFile, new GeometryCollection(geometries, _geometryFactory));

                // Write dbf
                _dbaseWriter.Write(Header);
                foreach (IFeature feature in featureCollection)
                {
                    var       attribs = feature.Attributes;
                    ArrayList values  = new ArrayList();
                    for (int i = 0; i < Header.NumFields; i++)
                    {
                        values.Add(attribs[Header.Fields[i].Name]);
                    }
                    _dbaseWriter.Write(values);
                }
            }
            finally
            {
                // Close dbf writer
                _dbaseWriter.Close();
            }
        }
        /// <summary>
        /// Write the enumeration of features to shapefile (shp, shx and dbf)
        /// </summary>
        /// <param name="filename">Filename to create</param>
        /// <param name="features">Enumeration of features to write, features will be enumerated once</param>
        /// <param name="fields">Fields that should be written, only those attributes specified here will be mapped from the feature attributetable while writing</param>
        /// <param name="shapeGeometryType">Type of geometries shapefile</param>
        /// <param name="dbfEncoding">Optional Encoding to be used when writing the DBF-file (default Windows-1252)</param>
        public static void WriteFeatures(string filename, IEnumerable <IFeature> features, DbaseFieldDescriptor[] fields, ShapeGeometryType shapeGeometryType,
                                         Encoding dbfEncoding = null)
        {
            // Set default encoding if not specified
            if (dbfEncoding == null)
            {
                dbfEncoding = DbaseEncodingUtility.GetEncodingForCodePageIdentifier(1252);
            }

            // Open shapefile and dbase stream writers
            using (var shpWriter = new ShapefileWriter(Path.ChangeExtension(filename, ".shp"), shapeGeometryType))
            {
                using (var dbfWriter = new DbaseFileWriter(Path.ChangeExtension(filename, ".dbf"), dbfEncoding))
                {
                    var dbfHeader = new DbaseFileHeader(dbfEncoding);
                    foreach (var field in fields)
                    {
                        dbfHeader.AddColumn(field.Name, field.DbaseType, field.Length, field.DecimalCount);
                    }
                    dbfWriter.Write(dbfHeader);

                    int      numFeatures = 0;
                    string[] fieldNames  = Array.ConvertAll(fields, field => field.Name);
                    object[] values      = new object[fieldNames.Length];
                    foreach (var feature in features)
                    {
                        shpWriter.Write(feature.Geometry);
                        for (int i = 0; i < fieldNames.Length; i++)
                        {
                            values[i] = feature.Attributes[fieldNames[i]];
                        }
                        dbfWriter.Write(values);
                        numFeatures++;
                    }

                    // set the number of records
                    dbfHeader.NumRecords = numFeatures;
                    // Update the header
                    dbfWriter.Write(dbfHeader);
                    // write the end of dbase file marker
                    dbfWriter.WriteEndOfDbf();
                    // close the dbase stream
                    dbfWriter.Close();
                }
            }
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="recordCount"></param>
        public static void WriteDummyDbf(string filename, int recordCount)
        {
            DbaseFileHeader dbfHeader = new DbaseFileHeader();

            dbfHeader.NumRecords = recordCount;
            dbfHeader.AddColumn("Description", 'C', 20, 0);

            DbaseFileWriter dbfWriter = new DbaseFileWriter(filename);

            dbfWriter.Write(dbfHeader);
            for (int i = 0; i < recordCount; i++)
            {
                List <double> columnValues = new List <double>();
                columnValues.Add((double)i);
                dbfWriter.Write(columnValues);
            }
            // End of file flag (0x1A)
            dbfWriter.Write(0x1A);
            dbfWriter.Close();
        }
Пример #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="recordCount"></param>
        public static void WriteDummyDbf(string filename, int recordCount)
        {
            filename = Path.ChangeExtension(filename, "dbf");
            var dbfHeader = new DbaseFileHeader {
                NumRecords = recordCount
            };

            dbfHeader.AddColumn("Description", 'C', 20, 0);

            var dbfWriter = new DbaseFileWriter(filename);

            dbfWriter.Write(dbfHeader);
            for (var i = 0; i < recordCount; i++)
            {
                var columnValues = new List <double> {
                    i
                };
                dbfWriter.Write(columnValues);
            }
            // End of file flag (0x1A)
            dbfWriter.Write(0x1A);
            dbfWriter.Close();
        }
Пример #7
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="recordCount"></param>
		public static void WriteDummyDbf(string filename, int recordCount)
        {
            filename = Path.ChangeExtension(filename, "dbf");
            var dbfHeader = new DbaseFileHeader {NumRecords = recordCount};
            dbfHeader.AddColumn("Description",'C', 20, 0);
			
			var dbfWriter = new DbaseFileWriter(filename);
			dbfWriter.Write(dbfHeader);
			for (var i = 0; i < recordCount; i++)
			{
				var columnValues = new List<double> {i};
			    dbfWriter.Write(columnValues);
			}
            // End of file flag (0x1A)
            dbfWriter.Write(0x1A);
			dbfWriter.Close();
		}
        /// <summary>
        /// Write the enumeration of features to shapefile (shp, shx and dbf)
        /// </summary>
        /// <param name="filename">Filename to create</param>
        /// <param name="features">Enumeration of features to write, features will be enumerated once</param>
        /// <param name="fields">Fields that should be written, only those attributes specified here will be mapped from the feature attributetable while writing</param>
        /// <param name="shapeGeometryType">Type of geometries shapefile</param>
        /// <param name="dbfEncoding">Optional Encoding to be used when writing the DBF-file (default Windows-1252)</param>
        public static void WriteFeatures(string filename, IEnumerable<IFeature> features, DbaseFieldDescriptor[] fields, ShapeGeometryType shapeGeometryType,
            Encoding dbfEncoding = null)
        {

            // Set default encoding if not specified
            if (dbfEncoding == null)
                dbfEncoding = Encoding.GetEncoding(1252);

            // Open shapefile and dbase stream writers
            using (var shpWriter = new ShapefileWriter(Path.ChangeExtension(filename, ".shp"), shapeGeometryType))
            {
                using (var dbfWriter = new DbaseFileWriter(Path.ChangeExtension(filename, ".dbf"), dbfEncoding))
                {
                    var dbfHeader = new DbaseFileHeader(dbfEncoding);
                    foreach (var field in fields)
                    {
                        dbfHeader.AddColumn(field.Name, field.DbaseType, field.Length, field.DecimalCount);
                    }
                    dbfWriter.Write(dbfHeader);

                    var numFeatures = 0;
                    foreach (var feature in features)
                    {
                        shpWriter.Write(feature.Geometry);
                        var values = new object[fields.Length];
                        for (var i = 0; i < fields.Length; i++)
                        {
                            values[i] = feature.Attributes[fields[i].Name];
                        }
                        dbfWriter.Write(values);
                        numFeatures++;
                    }

                    // set the number of records
                    dbfHeader.NumRecords = numFeatures;
                    // Update the header
                    dbfWriter.Write(dbfHeader);
                    // write the end of dbase file marker
                    dbfWriter.WriteEndOfDbf();
                    // close the dbase stream
                    dbfWriter.Close();
                }
            }
        }
        /// <summary>
        /// Method to write a dummy dbase file
        /// </summary>
        /// <param name="dbfWriter">The dbase file writer</param>
        /// <param name="recordCount">The number of records</param>
        public static void WriteDummyDbf(DbaseFileWriter dbfWriter, int recordCount)
        {
            // Create the dummy header
            var dbfHeader = new DbaseFileHeader {NumRecords = recordCount};
            // add some dummy column
            dbfHeader.AddColumn("Description", 'C', 20, 0);

            // Write the header
            dbfWriter.Write(dbfHeader);
            // Write the features
            for (var i = 0; i < recordCount; i++)
            {
                var columnValues = new List<double> {i};
                dbfWriter.Write(columnValues);
            }

            // End of file flag (0x1A)
            dbfWriter.WriteEndOfDbf();

            dbfWriter.Close();
        }
        public static void WriteDummyDbf(DbaseFileWriter dbfWriter, int recordCount)
        {
            var dbfHeader = new DbaseFileHeader { NumRecords = recordCount };
            dbfHeader.AddColumn("Description", 'C', 20, 0);


            dbfWriter.Write(dbfHeader);
            for (var i = 0; i < recordCount; i++)
            {
                var columnValues = new List<double> { i };
                dbfWriter.Write(columnValues);
            }
            // End of file flag (0x1A)
            dbfWriter.Write(0x1A);
            dbfWriter.Close();

        }