예제 #1
0
        /// <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();
        }
예제 #2
0
        /// <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,
                                         ICoordinateSystem coordinateSystem = null,
                                         Encoding dbfEncoding = null)
        {
            if (coordinateSystem != null)
            {
                string prjFile = Path.ChangeExtension(filename, ".prj");
                PrjFileWriter.Write(prjFile, coordinateSystem, dbfEncoding == null ? Encoding.Default : dbfEncoding);
            }

            // 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();
                }
            }
        }
예제 #3
0
        public static DbaseFileHeader GetHeader(DbaseFieldDescriptor[] dbFields, int count)
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.NumRecords = count;

            foreach (DbaseFieldDescriptor dbField in dbFields)
            {
                header.AddColumn(dbField.Name, dbField.DbaseType, dbField.Length, dbField.DecimalCount);
            }

            return(header);
        }
예제 #4
0
        /// <summary>
        /// Gets the stub header.
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        public static DbaseFileHeader GetHeader(IFeature feature, int count)
        {
            IAttributesTable attribs = feature.Attributes;

            string[]        names  = attribs.GetNames();
            DbaseFileHeader header = new DbaseFileHeader();

            header.NumRecords = count;
            foreach (string name in names)
            {
                Type type = attribs.GetType(name);
                if (type == typeof(double) || type == typeof(float))
                {
                    header.AddColumn(name, 'N', DoubleLength, DoubleDecimals);
                }
                else if (type == typeof(short) || type == typeof(ushort) ||
                         type == typeof(int) || type == typeof(uint))
                {
                    header.AddColumn(name, 'N', IntLength, IntDecimals);
                }
                else if (type == typeof(long) || type == typeof(ulong))
                {
                    header.AddColumn(name, 'N', LongLength, IntDecimals);
                }
                else if (type == typeof(string))
                {
                    header.AddColumn(name, 'C', StringLength, StringDecimals);
                }
                else if (type == typeof(bool))
                {
                    header.AddColumn(name, 'L', BoolLength, BoolDecimals);
                }
                else if (type == typeof(DateTime))
                {
                    header.AddColumn(name, 'D', DateLength, DateDecimals);
                }
                else
                {
                    throw new ArgumentException("Type " + type.Name + " not supported");
                }
            }
            return(header);
        }