コード例 #1
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();
                }
            }
        }
コード例 #2
0
        /// <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(_streamProviderRegistry, 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();
            }
        }
コード例 #3
0
        public static void WriteGeometryCollection(ShapefileWriter shapefileWriter, DbaseFileWriter dbfWriter,
                                                   IGeometryCollection geometryCollection, bool writeDummyDbf = true)
        {
            var numShapes = geometryCollection.NumGeometries;

            for (var i = 0; i < numShapes; i++)
            {
                shapefileWriter.Write(geometryCollection[i]);
            }

            if (writeDummyDbf)
            {
                WriteDummyDbf(dbfWriter, numShapes);
            }
        }
コード例 #4
0
        public static void WriteGeometryCollection(IStreamProviderRegistry streamProviderRegistry,
                                                   IGeometryCollection geometryCollection, bool createDummyDbf = true)
        {
            var shapeFileType = Shapefile.GetShapeType(geometryCollection);

            using (var writer = new ShapefileWriter(geometryCollection.Factory, streamProviderRegistry, shapeFileType))
            {
                var dbfWriter = createDummyDbf ? new DbaseFileWriter(streamProviderRegistry) : null;
                WriteGeometryCollection(writer, dbfWriter, geometryCollection, createDummyDbf);
                if (dbfWriter != null)
                {
                    dbfWriter.Dispose();
                }
            }
        }