基于gdal的geojson转shapefile的C#实现
2017-01-12 17:49
597 查看
里面东西有点杂,包括了进度条代码,gdal环境的安装可以参考百度上的教程。直接复制代码没法运行,另外,文件夹1装的是转化后的shp,文件夹2装的是需要转化的geojson.
这样做主要是为了批量处理大量geojson文件。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Diagnostics;using System.IO;namespace dos{public partial class Form1 : Form{private my_processbar progressForm = new my_processbar();// 代理定义,可以在Invoke时传入相应的参数private delegate void funHandle(int nValue);private funHandle myHandle = null;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){// 启动线程System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadFun));thread.Start();/*DirectoryInfo theFolder = new DirectoryInfo(@"F:\GDAL\bin\1\");FileInfo[] f = theFolder.GetFiles();for (int i = 0; i < f.Count(); i++){string command = "ogr2ogr -f \"ESRI Shapefile\" 2\\";command += Path.GetFileNameWithoutExtension(f[i].FullName);command += ".shp ";command += "1\\";command += f[i].Name;textBox1.Text+= Execute(command,10);//command += f[i].Name;}*/}private void ShowProgressBar(){myHandle = new funHandle(progressForm.SetProgressValue);progressForm.ShowDialog();}private void ThreadFun(){MethodInvoker mi = new MethodInvoker(ShowProgressBar);this.BeginInvoke(mi);//System.Threading.Thread.Sleep(1000); // sleep to show windowDirectoryInfo theFolder = new DirectoryInfo(@"F:\GDAL\bin\1\");FileInfo[] f = theFolder.GetFiles();for (int i = 0; i < f.Count(); i++){string command = "ogr2ogr -f \"ESRI Shapefile\" 2\\";command += Path.GetFileNameWithoutExtension(f[i].FullName);command += ".shp ";command += "1\\";command += f[i].Name;textBox1.Text += Execute(command, 10);this.Invoke(this.myHandle, new object[] { (Convert.ToInt32( Convert.ToDouble(i+1) / Convert.ToDouble(f.Count()) * 100.0)) });}/* for (int i = 0; i < 1000; ++i){System.Threading.Thread.Sleep(5);// 这里直接调用代理this.Invoke(this.myHandle, new object[] { (i / 10) });}*/}public static string Execute(string command, int seconds){string output = ""; //输出字符串if (command != null && !command.Equals("")){Process process = new Process();//创建进程对象ProcessStartInfo startInfo = new ProcessStartInfo();startInfo.FileName = "cmd.exe";//设定需要执行的命令startInfo.WorkingDirectory = @"F:\GDAL\bin\";startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出startInfo.UseShellExecute = false;//不使用系统外壳程序启动startInfo.RedirectStandardInput = false;//不重定向输入startInfo.RedirectStandardOutput = true; //重定向输出startInfo.CreateNoWindow = true;//不创建窗口process.StartInfo = startInfo;try{if (process.Start())//开始进程{if (seconds == 0){process.WaitForExit();//这里无限等待进程结束}else{process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒}output = process.StandardOutput.ReadToEnd();//读取进程的输出}}catch{}finally{if (process != null)process.Close();}}return output;}}}进度条框的代码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace dos{public partial class my_processbar : Form{public my_processbar(){InitializeComponent();}public void SetProgressValue(int value){this.progressBar1.Value = value;this.label1.Text = value.ToString() + "%";// 这里关闭,比较好,呵呵!if (value == this.progressBar1.Maximum) this.Close();}}}
相关文章推荐
- C#结合GDAL使用Band的ReadRaster和WriteRaster方法实现3波段图像水平镜像
- C#基于Socket服务器端实现
- 基于C#的socket编程的TCP异步实现
- C#基于Linq和反射实现数据持久化框架Xml4DB
- 用C#实现基于TCP协议的网络通讯
- 用C#实现基于TCP协议的网络通讯
- 基于C#实现简单离线注册码生成与验证
- 基于MATLAB和C#编程实现二元函数梯度下降法求极小值
- 基于C#实现的仿windows左侧伸缩菜单效果
- 基于ASP.net C#技术来实现,介绍如何处理Session对象变量失效的问题
- C#实现基于ffmpeg加虹软的人脸识别的示例
- C# 优化基于插件的开发框架实现Ribbon界面与功能的分离附DEMO
- C#结合GDAL实现图像加运算
- 基于 SQLite 数据库的 C# 实体模型的实现
- 基于C#的UDP协议的异步实现
- C#实现list的多个和单个删除(基于win8商店应用)
- 基于C#的商店销售管理系统的设计与实现
- 【出版直播】博客园征途系列,《设计模式-基于C#的工程化实现及扩展》推迟上市,期待圣诞大餐
- C#学习- 通过基于接口的编程来实现数据库的切换
- 用C#实现基于TCP协议的网络通讯