private int ShowHeader(SHPHeader ASHPHeader) { byte[] B = IntegerBytes(ASHPHeader.FileLength); int result = B[3] + (B[2] * 256) + (B[1] * 256 * 256) + (B[0] * 256 * 256 * 256); if (debug != null) { AddDebug("FileCode: " + ASHPHeader.FileCode.ToString()); AddDebug("Filelength: " + ASHPHeader.FileLength.ToString()); AddDebug("FileSize: " + Convert.ToString(2 * result)); } result = (result * 2 - 100) / 8; if (debug != null) { AddDebug("NumRecords: " + result.ToString()); AddDebug("Version: " + Convert.ToString(ASHPHeader.Version)); AddDebug("ShapeType: " + Convert.ToString(ASHPHeader.ShapeType)); AddDebug("XMin: " + Convert.ToString(ASHPHeader.XMin)); AddDebug("YMin: " + Convert.ToString(ASHPHeader.YMin)); AddDebug("XMax: " + Convert.ToString(ASHPHeader.XMax)); AddDebug("YMax: " + Convert.ToString(ASHPHeader.YMax)); AddDebug("ZMin: " + Convert.ToString(ASHPHeader.ZMin)); AddDebug("ZMax: " + Convert.ToString(ASHPHeader.ZMax)); AddDebug("MMin: " + Convert.ToString(ASHPHeader.MMin)); AddDebug("MMax: " + Convert.ToString(ASHPHeader.MMax)); } return(result); }
private void ReadSHPHeader(Stream f, ref SHPHeader ASHPHeader) { BinaryReader bin = new BinaryReader(f); int myInt32; double myDouble; for (int i = 0; i < 9; ++i) { myInt32 = bin.ReadInt32(); AddInteger(ref ASHPHeader, i, myInt32); } for (int i = 0; i < 8; ++i) { myDouble = bin.ReadDouble(); AddDouble(ref ASHPHeader, i, myDouble); } }
private void AddInteger(ref SHPHeader ASHPHeader, int Order, int MyInt32) { switch (Order) { case 0: ASHPHeader.FileCode = MyInt32; break; case 1: ASHPHeader.Unused1 = MyInt32; break; case 2: ASHPHeader.Unused2 = MyInt32; break; case 3: ASHPHeader.Unused3 = MyInt32; break; case 4: ASHPHeader.Unused4 = MyInt32; break; case 5: ASHPHeader.Unused5 = MyInt32; break; case 6: ASHPHeader.FileLength = MyInt32; break; case 7: ASHPHeader.Version = MyInt32; break; case 8: ASHPHeader.ShapeType = MyInt32; break; } }
private void AddDouble(ref SHPHeader ASHPHeader, int Order, double MyDouble) { switch (Order) { case 0: ASHPHeader.XMin = MyDouble; break; case 1: ASHPHeader.YMin = MyDouble; break; case 2: ASHPHeader.XMax = MyDouble; break; case 3: ASHPHeader.YMax = MyDouble; break; case 4: ASHPHeader.ZMin = MyDouble; break; case 5: ASHPHeader.ZMax = MyDouble; break; case 6: ASHPHeader.MMin = MyDouble; break; case 7: ASHPHeader.MMax = MyDouble; break; } }
/// <summary> /// Loads an ESRI(c) SHP file into a Map series. /// </summary> /// <param name="Series"></param> /// <param name="FileName"></param> /// <param name="Table"></param> /// <param name="FieldName"></param> /// <param name="FieldValue"></param> /// <param name="Debug"></param> public void LoadMap(Map Series, string FileName, DataTable Table, string FieldName, string FieldValue, ListBox.ObjectCollection Debug, string filter) { debug = Debug; SHPHeader shpHeader = new SHPHeader(); SHPRecordHeader shpRecordHeader = new SHPRecordHeader(); // Verify map file name string tmpName = FileName; // replaceFilePath(FileName, ".shp"); FileStream f = File.OpenRead(tmpName); try { if (debug != null) { debug.Clear(); } AddDebug("Real File size: " + f.Length.ToString()); f.Position = 0; ReadSHPHeader(f, ref shpHeader); VerifySignature(shpHeader.FileCode, tmpName); ShowHeader(shpHeader); tmpName = replaceFilePath(FileName, ".shx"); FileStream fx = File.OpenRead(tmpName); try { fx.Position = 0; ReadSHPHeader(fx, ref shpHeader); VerifySignature(shpHeader.FileCode, tmpName); if (debug != null) { AddDebug(" "); AddDebug("Real File size: " + fx.Length.ToString()); } int NumRecords = ShowHeader(shpHeader); Series.Clear(); Series.BeginUpdate(); Series.Chart.AutoRepaint = false; for (int i = 1; i <= NumRecords; ++i) { ReadRecordHeader(fx, ref shpRecordHeader); if (debug != null) { AddDebug(" "); AddDebug("Record Num: " + Convert.ToString(i) + " Offset: " + Convert.ToString(2 * shpRecordHeader.RecordNumber) + " Length: " + Convert.ToString(shpRecordHeader.RecordLength)); } f.Position = 2 * shpRecordHeader.RecordNumber; ReadRecordHeader(f, ref shpRecordHeader); if (debug != null) { AddDebug("Record Num: " + Convert.ToString(shpRecordHeader.RecordNumber)); } BinaryReader bin = new BinaryReader(f); ShapeType = bin.ReadInt32(); LoadShape(bin, Table, Series, FieldName, FieldValue, i - 1); } Series.EndUpdate(); Series.Chart.AutoRepaint = true; //Series.Chart.Invalidate(); } finally { fx.Close(); } } finally { f.Close(); } }