您的位置:首页 > Web前端 > JavaScript

基于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();}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息