public static ShpFileBody ReadShpFileBody(BinaryReader binaryReader) { ShpFileBody shpBody = new ShpFileBody(); shpBody.recordNo = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpBody.recordLength = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpBody.recordContent = binaryReader.ReadBytes(shpBody.recordLength * 2); //读取逻辑 return(shpBody); }
private void button1_Click(object sender, EventArgs e) { TextFileName = localFilePath; string allLine = null; if (!File.Exists(TextFileName)) // 首先判断,文件是否已经存在 { MessageBox.Show(TextFileName + "文件不存在!"); // 如果文件不存在,那么提示无法读取! return; } allLine = ShpFunction.ShowString(TextFileName); textBox1.Text = allLine; }
private void ReadFileBtn_Click(object sender, EventArgs e) { TextFileName = textBox2.Text.Trim(); string allLine = null; if (!File.Exists(TextFileName)) { MessageBox.Show(TextFileName + "文件不存在!");// 如果文件不存在,那么提示无法读取! return; } allLine = ShpFunction.ShowString(TextFileName); ShowTxt.Text = allLine; }
public static void WriteShpHeader(BinaryWriter binaryWriter, ShpHeader shpHeader) { ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.fileCode); ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse1); ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse2); ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse3); ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse4); ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.unUse5); ShpFunction.WriteInt32WithBigEnduim(binaryWriter, shpHeader.fileLength); binaryWriter.Write(shpHeader.fileVersion); binaryWriter.Write(shpHeader.shapeType); ShpFunction.WriteBoundingBox(binaryWriter, shpHeader.boundingBox); //写header逻辑 }
public static void MergeManyShpFile(string[] filenames, string tempfile, string TargetFile) { ShpFunction.MergeShp(filenames[0], filenames[1], tempfile); string temp = ""; for (int i = 2; i < filenames.Length; i++) { ShpFunction.MergeShp(filenames[i], tempfile, TargetFile); temp = tempfile; tempfile = TargetFile; TargetFile = temp; } File.Delete(tempfile); }
private void button6_Click(object sender, EventArgs e) { string foldPath = ""; string tempfile = ""; string TargetFile = ""; FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { foldPath = dialog.SelectedPath; }//获取文件夹名字 string[] filenames = Directory.GetFiles(foldPath, "*.shp"); //获取该文件夹下面的所有文件名并限制文件读取的类型 MessageBox.Show("请选择合并所有文件后的总文件地址"); SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Shp文件|*.shp"; //设置文件类型 sfd.Title = "请选择合并所有文件后的总文件地址"; sfd.FilterIndex = 1; //设置默认文件类型显示顺序 sfd.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 if (sfd.ShowDialog() == DialogResult.OK) //点了保存按钮进入 { TargetFile = sfd.FileName; } sfd.Dispose();//设置目的文件储存位置 MessageBox.Show("由于需要缓存空间,请选择缓存文件的地址(在合并完成后会删除,不会对其他空间进行占用)"); SaveFileDialog sfd1 = new SaveFileDialog(); sfd1.Title = "请选择缓存文件的地址"; sfd1.Filter = "Shp文件|*.shp"; //设置文件类型 sfd1.FilterIndex = 1; //设置默认文件类型显示顺序 sfd1.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 if (sfd1.ShowDialog() == DialogResult.OK) //点了保存按钮进入 { tempfile = sfd1.FileName; } sfd1.Dispose(); //设置缓存文件储存位置 TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks); //获取当前时间的刻度数 ShpFunction.MergeManyShpFile(filenames, tempfile, TargetFile); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts2.Subtract(ts1).Duration(); //时间差的绝对值 String spanTime = ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒" + ts.Milliseconds.ToString() + "毫秒"; //以X小时X分X秒的格式现实执行时间 MessageBox.Show("合并结束,耗时" + spanTime); }
public static ShpHeader ReadShpHeader(BinaryReader binaryReader) { ShpHeader shpHeader = new ShpHeader(); shpHeader.fileCode = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpHeader.unUse1 = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpHeader.unUse2 = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpHeader.unUse3 = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpHeader.unUse4 = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpHeader.unUse5 = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpHeader.fileLength = ShpFunction.ReadInt32WithBigEnduim(binaryReader); shpHeader.fileVersion = binaryReader.ReadInt32(); shpHeader.shapeType = binaryReader.ReadInt32(); shpHeader.boundingBox = ShpFunction.ReadBoundingBox(binaryReader); //读取逻辑 return(shpHeader); }
public static ShpHeader MergeShpHeader(ShpHeader shpHeader1, ShpHeader ShpHeader2) { ShpHeader newShpHeader = new ShpHeader(); newShpHeader.fileCode = shpHeader1.fileCode; newShpHeader.unUse1 = shpHeader1.unUse1; newShpHeader.unUse2 = shpHeader1.unUse2; newShpHeader.unUse3 = shpHeader1.unUse3; newShpHeader.unUse4 = shpHeader1.unUse4; newShpHeader.unUse5 = shpHeader1.unUse5; newShpHeader.fileLength = shpHeader1.fileLength + ShpHeader2.fileLength - 50; newShpHeader.fileVersion = shpHeader1.fileVersion; newShpHeader.shapeType = shpHeader1.shapeType; newShpHeader.boundingBox = ShpFunction.MergeBoundingBox(shpHeader1.boundingBox, ShpHeader2.boundingBox); //合并逻辑 return(newShpHeader); }
public static string ShowString(string TextFileName) { BinaryReader binaryReader = null; FileStream fileStream = null; string allLine = null; try { fileStream = new FileStream(TextFileName, FileMode.Open, FileAccess.Read, FileShare.None); binaryReader = new BinaryReader(fileStream); ShpHeader newShpHeader = new ShpHeader();//定义header newShpHeader = ShpFunction.ReadShpHeader(binaryReader); ShpFileBody newShpBody = new ShpFileBody(); allLine = allLine + ShpFunction.ShpHeader_to_String(newShpHeader); //大端格式读出记录编号,长度 while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { newShpBody = ShpFunction.ReadShpFileBody(binaryReader); allLine = allLine + ShpFunction.ShpBody_to_String(newShpBody); } ShpFunction.CloseReader(binaryReader, fileStream); } catch (Exception ex) { Console.WriteLine("在读取文件的过程中,发生了异常!"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } finally { if (fileStream != null) { try { fileStream.Close(); } catch { // 最后关闭文件,无视关闭是否会发生错误了. } } } return(allLine); }
public static void MergeManyShpFile(string[] filenames, string TargetFile) { string Temp1 = TargetFile.Substring(TargetFile.LastIndexOf("\\"), TargetFile.Length - TargetFile.LastIndexOf("\\")); string tempfile = TargetFile.Replace(Temp1, "") + "\\Tempfile.shp"; ShpFunction.MergeShp(filenames[0], filenames[1], tempfile); string temp = ""; for (int i = 2; i < filenames.Length; i++) { ShpFunction.MergeShp(filenames[i], tempfile, TargetFile); temp = tempfile; tempfile = TargetFile; TargetFile = temp; } File.Copy(tempfile, TargetFile, true); File.Delete(tempfile); }
private void button3_Click(object sender, EventArgs e) { TextFileName = textBox2.Text.Trim(); TextFileName1 = textBox3.Text.Trim(); SaveFileDialog sfd = new SaveFileDialog(); //string localFilePath, fileNameExt, newFileName, FilePath; sfd.Filter = "Shp文件|*.shp"; //设置文件类型 sfd.FilterIndex = 1; //设置默认文件类型显示顺序 sfd.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 if (sfd.ShowDialog() == DialogResult.OK) //点了保存按钮进入 { localFilePath = sfd.FileName.ToString(); //获得文件路径 string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径 } TextFileName2 = localFilePath; if (!File.Exists(TextFileName) && !File.Exists(TextFileName1)) // 首先判断,文件是否已经存在 { MessageBox.Show(TextFileName + "文件不存在!"); // 如果文件不存在,那么提示无法读取! return; } ShpFunction.MergeShp(TextFileName, TextFileName1, TextFileName2); MessageBox.Show("合并成功!"); }
private void button6_Click(object sender, EventArgs e) { string foldPath = ""; FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { foldPath = dialog.SelectedPath; MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }//获取文件夹名字 string[] filenames = Directory.GetFiles(foldPath); //获取该文件夹下面的所有文件名 SaveFileDialog sfd = new SaveFileDialog(); //string localFilePath, fileNameExt, newFileName, FilePath; sfd.Filter = "Shp文件|*.shp"; //设置文件类型 sfd.FilterIndex = 1; //设置默认文件类型显示顺序 sfd.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 if (sfd.ShowDialog() == DialogResult.OK) //点了保存按钮进入 { localFilePath = sfd.FileName.ToString(); //获得文件路径 string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径 } TextFileName2 = localFilePath; TextFileName1 = "d:\\hhhh.shp"; string temp = ""; for (int i = 0; i < filenames.Length; i++) { File.Delete(TextFileName2); ShpFunction.MergeShp(filenames[i], TextFileName1, TextFileName2); temp = TextFileName1; TextFileName1 = TextFileName2; TextFileName2 = temp; } }
public static void MergeShp(String TextFileName, String TextFileName1, String TextFileName2) { BinaryReader binaryReader = null; BinaryReader binaryReader1 = null; FileStream fileStream = null; FileStream fileStream1 = null; try { fileStream = new FileStream(TextFileName, FileMode.Open, FileAccess.Read, FileShare.None); binaryReader = new BinaryReader(fileStream); ShpHeader newShpHeader = new ShpHeader();//定义header newShpHeader = ShpFunction.ReadShpHeader(binaryReader); ShpFileBody newShpBody = new ShpFileBody(); fileStream1 = new FileStream(TextFileName1, FileMode.Open, FileAccess.Read, FileShare.None); binaryReader1 = new BinaryReader(fileStream1); ShpHeader newShpHeader1 = new ShpHeader();//定义header newShpHeader1 = ShpFunction.ReadShpHeader(binaryReader1); ShpFileBody newShpBody1 = new ShpFileBody(); //写入新文件 FileStream fileStream2 = null; BinaryWriter binaryWriter = null; // 首先判断,文件是否已经存在 if (File.Exists(TextFileName2)) { File.Delete(TextFileName2);// 如果文件已经存在,那么删除掉. } fileStream2 = new FileStream(TextFileName2, FileMode.Create, FileAccess.Write, FileShare.None); binaryWriter = new BinaryWriter(fileStream2); //大端格式写入fileCode; newShpHeader = ShpFunction.MergeShpHeader(newShpHeader, newShpHeader1); //合并文件头 ShpFunction.WriteShpHeader(binaryWriter, newShpHeader); while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { newShpBody = ShpFunction.ReadShpFileBody(binaryReader); ShpFunction.WriteShpBody(binaryWriter, newShpBody); } while (binaryReader1.BaseStream.Position < binaryReader1.BaseStream.Length) { newShpBody1 = ShpFunction.ReadShpFileBody(binaryReader1); newShpBody1.recordNo = newShpBody1.recordNo + newShpBody.recordNo; ShpFunction.WriteShpBody(binaryWriter, newShpBody1); } //大端格式读出记录编号,长度 ShpFunction.CloseReader(binaryReader, fileStream); ShpFunction.CloseReader(binaryReader1, fileStream1); ShpFunction.CloseWriter(binaryWriter, fileStream2); } catch (Exception ex) { Console.WriteLine("在读取文件的过程中,发生了异常!"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } finally { if (fileStream != null) { try { fileStream.Close(); } catch { // 最后关闭文件,无视关闭是否会发生错误了. } } } //合并逻辑 }