public DbfRecord(DbfHeader header) { _contents = new byte[header.RecordLength]; _contents[0] = (byte)' '; _header = header; }
private static DbfHeader BuildHeader(SqlDataReader reader, out int geomOrdinal, out int colCount) { DbfHeader dbfHeader = new DbfHeader(); DataTable schema = reader.GetSchemaTable(); geomOrdinal = -1; colCount = schema.Rows.Count - 1; foreach (DataRow row in schema.Rows) { int oridinal = (int)row["ColumnOrdinal"]; int size = (int)row["ColumnSize"]; string name = row["ColumnName"] as string; switch ((row["DataType"] as Type).Name) { case "String": dbfHeader.AddCharacter(name, (byte)size); break; case "SqlGeometry": geomOrdinal = oridinal; break; default: throw new Exception(String.Format("'{0}' is not a recognized data type", (row["DataType"] as Type).Name)); } } if (geomOrdinal == -1) throw new Exception("Geometry column was not found"); return dbfHeader; }
public static void Reproject(string destinationFolder, string projection, params ReprojectShapefile[] shapes) { ProjectionInfo targetProjection = ProjectionInfo.FromEsriString(projection); foreach (ReprojectShapefile shape in shapes) { string shapePath = Path.Combine(destinationFolder, shape.DestinationName); ShapefileHeader shapeHeader = ShapefileHeader.CreateEmpty(ShapefileGeometryType.Polygon); DbfHeader dbfHeader = new DbfHeader(); dbfHeader.AddCharacter("Label", 80); GeometryFactory gf = new GeometryFactory(); using (ShapefileDataWriter writer = ShapefileDataWriter.Create(shapePath + ".shp", dbfHeader, shapeHeader)) { GeometryTransform transform = null; if (File.Exists(Path.ChangeExtension(shape.Source, ".prj"))) { transform = GeometryTransform.GetTransform(shape.Source, projection); if (transform != null) File.WriteAllText(shapePath + ".prj", projection); else File.Copy(Path.ChangeExtension(shape.Source, ".prj"), shapePath + ".prj"); } using (ShapefileIndexReader index = new ShapefileIndexReader(Path.ChangeExtension(shape.Source, ".shx"))) { if (transform != null) writer.Header.Bounds.ExpandToInclude(transform.Apply(index.Header.Bounds)); else writer.Header.Bounds.ExpandToInclude(index.Header.Bounds); Task[] tasks = new Task[Environment.ProcessorCount]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Factory.StartNew(() => { using (ShapefileBlockReader reader = new ShapefileBlockReader(shape.Source, index, gf, transform)) { while (reader.Read()) writer.Write(reader.Geometry, reader.Record.GetString(shape.Label)); } }); } Task.WaitAll(tasks); writer.Flush(); } } } }
public DbfHeader Clone() { DbfHeader header = new DbfHeader(); header.LastUpdate = this.LastUpdate; foreach (DbfColumn col in _columns) { header.AddColumn(col.Name, col.Type, col.Length, col.DecimalCount); } return(header); }
public static ShapefileDataWriter Create(string path, DbfHeader dbfHeader, ShapefileHeader shapeHeader) { ShapefileDataWriter writer = new ShapefileDataWriter(path, shapeHeader, FileMode.CreateNew, FileAccess.Write); writer._writerShape.BaseStream.Seek(100L, SeekOrigin.Begin); writer._writerIndex.BaseStream.Seek(100L, SeekOrigin.Begin); writer._dbf = DbfFile.Create(Path.ChangeExtension(path, ".dbf"), dbfHeader); writer._currentRecord = new DbfRecord(dbfHeader); writer._recordNumber = 1; writer._filePos = 50; return writer; }
public static void Combine(string finalShape, string projection, params CombineShapefile[] combineShapes) { DbfHeader dbfHeader = new DbfHeader(); dbfHeader.AddCharacter("Label", 80); ShapefileHeader shapeHeader = ShapefileHeader.CreateEmpty(ShapefileGeometryType.Polygon); GeometryFactory gf = new GeometryFactory(); using (ShapefileDataWriter writer = ShapefileDataWriter.Create(finalShape, dbfHeader, shapeHeader)) { // Write the projection file. File.WriteAllText(Path.ChangeExtension(finalShape, ".prj"), projection); foreach (CombineShapefile workerShp in combineShapes) { GeometryTransform transform = GeometryTransform.GetTransform(workerShp.FilePath, projection); using (ShapefileIndexReader index = new ShapefileIndexReader(Path.ChangeExtension(workerShp.FilePath, ".shx"))) { if (transform != null) writer.Header.Bounds.ExpandToInclude(transform.Apply(index.Header.Bounds)); else writer.Header.Bounds.ExpandToInclude(index.Header.Bounds); Task[] tasks = new Task[Environment.ProcessorCount]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Factory.StartNew(() => { using (ShapefileBlockReader reader = new ShapefileBlockReader(workerShp.FilePath, index, gf, transform)) { while (reader.Read()) writer.Write(reader.Geometry, reader.Record.GetString(workerShp.Label)); } }); } Task.WaitAll(tasks); writer.Flush(); } } } }
private DbfFile(Stream stream, bool isWriting, DbfHeader header = null) { if (header == null) { Header = new DbfHeader(); } else { Header = header; } _stream = stream; _reader = new BinaryReader(stream); if (isWriting) { _headerNeedsWrite = true; _writer = new BinaryWriter(stream); } else { Header.Read(_reader); } }
public DbfHeader Clone() { DbfHeader header = new DbfHeader(); header.LastUpdate = this.LastUpdate; foreach (DbfColumn col in _columns) header.AddColumn(col.Name, col.Type, col.Length, col.DecimalCount); return header; }
public static DbfFile Create(string path, DbfHeader header = null) { return(new DbfFile( File.Create(path), true, header)); }
public DbfRecordEnumerator(BinaryReader reader, DbfHeader header) { _reader = reader; _record = new DbfRecord(header); }