コード例 #1
0
        /// <summary>
        /// Method to get the encoding from a stream provider
        /// </summary>
        /// <param name="provider">The stream provider</param>
        /// <returns>
        /// An encoding. If <paramref name="provider"/> is null,
        /// the default ANSI codepage for the system is returned.
        /// </returns>
        internal static Encoding GetEncoding(IStreamProvider provider = null)
        {
            if (provider == null)
            {
                return(DefaultEncoding);
            }

            if (provider.Kind != StreamTypes.DataEncoding)
            {
                throw new ArgumentException("provider");
            }


            string cpgText;

            using (var sr = new StreamReader(provider.OpenRead()))
                cpgText = sr.ReadToEnd();

            try
            {
                return(DbaseEncodingUtility.GetEncodingForCodePageName(cpgText));
            }
            catch
            {
                //return Encoding.Default;
                return(DefaultEncoding);
            }
        }
コード例 #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,
                                         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();
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShapefileDataWriter"/> class.
 /// </summary>
 /// <param name="fileName">File path without any extension</param>
 /// <param name="geometryFactory"></param>
 public ShapefileDataWriter(string fileName, IGeometryFactory geometryFactory)
     : this(fileName, geometryFactory, DbaseEncodingUtility.GetEncodingForCodePageIdentifier(1252))
 {
 }