/// <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(); }
/// <summary> /// Method to write a dummy dbase file /// </summary> /// <param name="streamProviderRegistry">The stream provider registry</param> /// <param name="recordCount">The number of records</param> public static void WriteDummyDbf(IStreamProviderRegistry streamProviderRegistry, int recordCount) { using (var dbfWriter = new DbaseFileWriter(streamProviderRegistry)) { WriteDummyDbf(dbfWriter, recordCount); } }
public ShapefileDataWriter(IStreamProviderRegistry streamProviderRegistry, IGeometryFactory geometryFactory, Encoding encoding) { _geometryFactory = geometryFactory; _streamProviderRegistry = streamProviderRegistry; // Writers _dbaseWriter = new DbaseFileWriter(streamProviderRegistry, encoding); }
/// <summary> /// Method to write a dummy dbf file /// </summary> /// <param name="filename">The dbase filename</param> /// <param name="recordCount">The number of records</param> public static void WriteDummyDbf(string filename, int recordCount) { // assert the filename is correct filename = Path.ChangeExtension(filename, "dbf"); using (var dbfWriter = new DbaseFileWriter(filename)) { WriteDummyDbf(dbfWriter, recordCount); } }
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); } }
/// <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(); } } }
private void WriteGridAbundances(Project currentProject, string shapefileName) { try { List <IPolygon> projectGrids = new List <IPolygon>(); General_queries projq = new General_queries(currentProject); foreach (Station st in currentProject.StationsList) { IPolygon grid = st.Grid; StationStats tempStationStats = new StationStats(st.Guid, st.StationID); foreach (StationStats stst in projq.AllStatsByStation) { if (stst.StationGUID == st.Guid) { tempStationStats = stst; grid.UserData = tempStationStats; } } projectGrids.Add(grid); } GeometryCollection gc = new GeometryCollection(projectGrids.ToArray()); //Open Writer ShapefileWriter shpWriter = new ShapefileWriter(); shpWriter.Write(shapefileName, gc); //Create Header & Columns for points DbaseFileHeader dbfHeader = new DbaseFileHeader(); dbfHeader.AddColumn("Station_ID", 'C', 20, 0); //One column for each species in project foreach (SpeciesStats spcst in projq.AllStatsBySpecies) { dbfHeader.AddColumn(spcst.SpeciesName, 'N', 20, 0); } dbfHeader.NumRecords = gc.Count; //DBF Writer DbaseFileWriter dbfWriter = new DbaseFileWriter(shapefileName + ".dbf"); dbfWriter.Write(dbfHeader); //Loop through Business Object to get Features foreach (IPolygon p in gc.Geometries) { StationStats data = (StationStats)p.UserData; ArrayList columnValues = new System.Collections.ArrayList(); //Add Values columnValues.Add(data.StationID); foreach (SpeciesStats s in data.SpeciesStats) { columnValues.Add(s.SpeciesPictures); } dbfWriter.Write(columnValues); } //Close File dbfWriter.Close(); } catch (Exception ex) { throw ex; } }