public ShapeFile(BinaryReader br) { Initialize(); FileCode = NumericsHelper.ReverseInt(br.ReadInt32()); // Big, Reverse for actual value for (var i = 0; i < 5; i++) { br.ReadInt32(); // Skip 5 empty Integer (4-byte) slots } FileLength = NumericsHelper.ReverseInt(br.ReadInt32()); // Big, Reverse for actual value Version = br.ReadInt32(); ShapeType = (ShapeTypeEnum)br.ReadInt32(); BoundingBox = new BoundingBoxZ(br); }
public void ImportFromBinaryReader(ShapeType shapeType, BinaryReader br) { try { long streamLength = br.BaseStream.Length; RecordNumber = NumericsHelper.ReverseInt(br.ReadInt32()); // Big, Reverse for actual value ContentLength = NumericsHelper.ReverseInt(br.ReadInt32()); // Big, Reverse for actual value ShapeType = (ShapeType)br.ReadInt32(); if (ShapeType == ShapeType.Null || shapeType == ShapeType.Null) { return; } if (ShapeType != shapeType) { // Shape Type doesn't match type specified in file header. According to specs, this is invalid. Throw exception throw new Exception( $"Unable to process shape! File shape of {shapeType} does not match record shape of {ShapeType} which violates ESRI specifications for shape files!"); } ShapeClass shapeClass = ShapeClass.Coordinate; bool hasMultiplePoints = true; bool hasParts = true; bool hasPartTypes = false; switch (shapeType) { case ShapeType.MultiPatch: shapeClass = ShapeClass.Depth; hasPartTypes = true; break; case ShapeType.MultiPoint: hasParts = false; break; case ShapeType.MultiPointM: shapeClass = ShapeClass.Measurement; hasParts = false; break; case ShapeType.MultiPointZ: shapeClass = ShapeClass.Depth; hasParts = false; break; case ShapeType.Null: throw new Exception( "The application should have never gotten to this point.\r\nSomething is wrong with the code!\r\nLYNCH THE DEVELOPER!"); case ShapeType.Point: hasMultiplePoints = false; hasParts = false; break; case ShapeType.PointM: shapeClass = ShapeClass.Measurement; hasMultiplePoints = false; hasParts = false; break; case ShapeType.PointZ: shapeClass = ShapeClass.Depth; hasMultiplePoints = false; hasParts = false; break; case ShapeType.Polygon: case ShapeType.Polyline: break; case ShapeType.PolygonM: shapeClass = ShapeClass.Measurement; break; case ShapeType.PolygonZ: shapeClass = ShapeClass.Depth; break; case ShapeType.PolylineM: shapeClass = ShapeClass.Measurement; break; case ShapeType.PolylineZ: shapeClass = ShapeClass.Depth; break; default: throw new Exception($"Unable to process shape! Record Shape of {ShapeType} is unknown!"); } if (hasMultiplePoints) { XMin = br.ReadDouble(); YMin = br.ReadDouble(); XMax = br.ReadDouble(); YMax = br.ReadDouble(); } NumberOfParts = hasParts ? br.ReadInt32() : 1; NumberOfPoints = hasMultiplePoints ? br.ReadInt32() : 1; //Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, "Shape")); List <Part> parts = new List <Part>(); for (int i = 0; i < NumberOfParts; i++) // Grab the parts { Part part = new Part { ShapeId = Id, SortIndex = i, PartTypeId = -1, // TODO: Find out what the appropriate part types are (if necessary) StartIndex = hasParts ? br.ReadInt32() : 0 // Get the start index }; parts.Add(part); if (i > 0) // If this isn't the first element { parts[i - 1].EndIndex = parts[i].StartIndex - 1; } // Set the last element's end index to this element's start index minus one if (i + 1 == NumberOfParts) // If this is the last element { parts[i].EndIndex = NumberOfPoints - 1; } // Set the ending index to the number of points minus one (to account for 0 based index) //Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, $"Part {i}")); } for (int i = 0; i < NumberOfParts; i++) // Set the number of points. This is done after initial grab to account for first/last elements { if (hasParts && hasPartTypes) { parts[i].PartTypeId = br.ReadInt32(); } parts[i].NumberOfPoints = parts[i].EndIndex - parts[i].StartIndex + 1; //Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, $"Part {i}")); } for (int i = 0; i < NumberOfParts; i++) // For each part { for (int j = 0; j < parts[i].NumberOfPoints; j++) // For each point in each part { parts[i].Points.Add(new Point { SortIndex = j, X = br.ReadDouble(), Y = br.ReadDouble() }); // Grab the point //Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, $"Part {i} | Point {j}")); } } if (shapeClass == ShapeClass.Depth) { if (hasMultiplePoints) { ZMin = br.ReadDouble(); ZMax = br.ReadDouble(); } for (int i = 0; i < NumberOfParts; i++) { for (int j = 0; j < parts[i].NumberOfPoints; j++) { //Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, // streamLength, // $"Part {i} | Point {j} | Setting Z Value")); parts[i].Points.ToArray()[j].Z = br.ReadDouble(); } } } if (shapeClass == ShapeClass.Depth || shapeClass == ShapeClass.Measurement) { if (hasMultiplePoints) { MMin = br.ReadDouble(); MMax = br.ReadDouble(); } for (int i = 0; i < NumberOfParts; i++) { for (int j = 0; j < parts[i].NumberOfPoints; j++) { //Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, // streamLength, // $"Part {i} | Point {j} | Setting M Value")); parts[i].Points.ToArray()[j].M = br.ReadDouble(); } } } try { string wktTemplate = "{0}({1})"; string shapeTypeName = ShapeType.ToString().ToUpperInvariant().Replace("POLYLINE", "MULTILINESTRING"); IReadOnlyCollection <string> coordinateStrings = parts.Select(s => s.CoordinateString).ToArray(); if (ShapeType == ShapeType.Point || ShapeType == ShapeType.PointZ || ShapeType == ShapeType.PointM) { coordinateStrings = coordinateStrings.Select(s => s.Trim('(').Trim(')').Trim()).ToArray(); } string coordinateString = string.Join(",", coordinateStrings); string wktString = string.Format(wktTemplate, shapeTypeName, coordinateString); //Debug.WriteLine(wktString); Geometry = DbGeometry.FromText(wktString, SRID); } catch (Exception e) { Console.WriteLine($"\r\n{e.Message}\r\n{e}"); throw; } } catch (Exception e) { Console.WriteLine($"\r\n{e.Message}\r\n{e}"); throw; } }
public RecordHeader(BinaryReader br) { RecordNumber = NumericsHelper.ReverseInt(br.ReadInt32()); // Big, Reverse for actual value ContentLength = NumericsHelper.ReverseInt(br.ReadInt32()); // Big, Reverse for actual value }
public void ImportFromFile(FileInfo file) { try { using (BinaryReader br = new BinaryReader(file.OpenRead())) { long streamLength = br.BaseStream.Length; Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, file.Name)); FileCode = NumericsHelper.ReverseInt(br.ReadInt32()); for (int i = 0; i < 5; i++) { br.ReadInt32(); // Skip 5 empty Integer (4-byte) slots } ContentLength = NumericsHelper.ReverseInt(br.ReadInt32()); // Big Endian, Reverse for actual value FileVersion = br.ReadInt32(); ShapeType = (ShapeType)br.ReadInt32(); XMin = br.ReadDouble(); YMin = br.ReadDouble(); XMax = br.ReadDouble(); YMax = br.ReadDouble(); ZMin = br.ReadDouble(); ZMax = br.ReadDouble(); MMin = br.ReadDouble(); MMax = br.ReadDouble(); //int rowsAffected; //using (ShapefileEntities db = new ShapefileEntities()) //{ // db.Entry(this).State = EntityState.Added; // rowsAffected = db.SaveChanges(); //} //if (rowsAffected > 0 // && Id > 0) //{ //List<Shape> shapes = new List<Shape>(); while (br.PeekChar() > -1) { Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, file.Name)); Shapes.Add(new Shape(SRID, ShapeType, br)); } Console.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, file.Name)); // using (SqlBulkCopy sbc = new SqlBulkCopy(DataHelper.DefaultConnectionString)) // { // sbc.BatchSize = DataHelper.DefaultBatchSize; // sbc.BulkCopyTimeout = DataHelper.DefaultTimeoutSeconds; // sbc.DestinationTableName = "Shape"; // sbc.EnableStreaming = true; // sbc.SqlRowsCopied += DataHelper.SqlBulkCopy_SqlRowsCopied; // sbc.NotifyAfter = 250; // sbc.ColumnMappings.Add("ShapeFileId", "ShapeFileId"); // sbc.ColumnMappings.Add("ShapeType", "ShapeType"); // sbc.ColumnMappings.Add("RecordNumber", "RecordNumber"); // sbc.ColumnMappings.Add("ContentLength", "ContentLength"); // sbc.ColumnMappings.Add("XMin", "XMin"); // sbc.ColumnMappings.Add("YMin", "YMin"); // sbc.ColumnMappings.Add("XMax", "XMax"); // sbc.ColumnMappings.Add("YMax", "YMax"); // sbc.ColumnMappings.Add("ZMin", "ZMin"); // sbc.ColumnMappings.Add("ZMax", "ZMax"); // sbc.ColumnMappings.Add("MMin", "MMin"); // sbc.ColumnMappings.Add("MMax", "MMax"); // sbc.ColumnMappings.Add("NumberOfParts", "NumberOfParts"); // sbc.ColumnMappings.Add("NumberOfPoints", "NumberOfPoints"); // sbc.ColumnMappings.Add("DTGeometry", "Geometry"); // try // { // DataTable shapesData = DataHelper.CreateDataTable(shapes); // sbc.WriteToServerAsync(shapesData); // } // catch (Exception e) // { // Console.WriteLine($"\r\n{e.Message}\r\n{e}"); // throw; // } // finally // { // sbc.Close(); // } // } //} //else // throw new FileLoadException("The ShapeFile record failed to save properly or doesn't have a valid ID"); } } catch (Exception e) { Console.WriteLine($"\r\n{e.Message}\r\n{e}"); throw; } }
public void ImportFromFile(FileInfo file) { try { using (BinaryReader br = new BinaryReader(file.OpenRead())) { long streamLength = br.BaseStream.Length; FileCode = NumericsHelper.ReverseInt(br.ReadInt32()); for (int i = 0; i < 5; i++) { br.ReadInt32(); // Skip 5 empty Integer (4-byte) slots } ContentLength = NumericsHelper.ReverseInt(br.ReadInt32()); // Big Endian, Reverse for actual value FileVersion = br.ReadInt32(); ShapeType = (ShapeType)br.ReadInt32(); XMin = br.ReadDouble(); YMin = br.ReadDouble(); XMax = br.ReadDouble(); YMax = br.ReadDouble(); ZMin = br.ReadDouble(); ZMax = br.ReadDouble(); MMin = br.ReadDouble(); MMax = br.ReadDouble(); int rowsAffected; using (ShapefileEntities db = new ShapefileEntities()) { db.Entry(this).State = EntityState.Added; rowsAffected = db.SaveChanges(); } if (!(rowsAffected > 0) || !(Id > 0)) { throw new Exception( "The index file was not added to the database properly. No ID is present to assign to the child index records. Unable to proceed!"); } List <ShapeIndex> shapeIndices = new List <ShapeIndex>(); int counter = 0; while (br.PeekChar() > -1) { LH.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, file.Name)); shapeIndices.Add(new ShapeIndex { IndexFileId = Id, RecordNumber = ++counter, Offset = NumericsHelper.ReverseInt(br.ReadInt32()), ContentLength = NumericsHelper.ReverseInt(br.ReadInt32()) }); } LH.Write(StringHelper.GetProgressString(br.BaseStream.Position, streamLength, file.Name)); using (SqlBulkCopy sbc = new SqlBulkCopy(DataHelper.DefaultConnectionString)) { sbc.BatchSize = DataHelper.DefaultBatchSize; sbc.BulkCopyTimeout = DataHelper.DefaultTimeoutSeconds; sbc.DestinationTableName = "ShapeIndex"; sbc.EnableStreaming = true; sbc.SqlRowsCopied += DataHelper.SqlBulkCopy_SqlRowsCopied; sbc.NotifyAfter = DataHelper.DefaultBatchSize; sbc.ColumnMappings.Add("Id", "Id"); sbc.ColumnMappings.Add("IndexFileId", "IndexFileId"); sbc.ColumnMappings.Add("RecordNumber", "RecordNumber"); sbc.ColumnMappings.Add("Offset", "Offset"); sbc.ColumnMappings.Add("ContentLength", "ContentLength"); try { DataTable shapeIndicesData = DataHelper.CreateDataTable(shapeIndices); sbc.WriteToServerAsync(shapeIndicesData); } catch (Exception e) { LH.Error($"\r\n{e.Message}\r\n{e}"); throw; } finally { sbc.Close(); } } } } catch (Exception e) { LH.Error($"\r\n{e.Message}\r\n{e}"); throw; } }