1 //(一)通过栅格范围裁剪 2 /// 3 ///图像裁剪算法测试,本算法实现了将World.tif根据起始点(0,0)到终止点(500,500)的矩形范围裁剪生成clip_xy.tif文件 4 /// 5 private void button_xy_Click () 6 { 7 #region 1、参数设置 8 //Clip裁剪 9 PIE.CommonAlgo.DataPreImgClip_Exchange_Info info = new PIE.CommonAlgo.DataPreImgClip_Exchange_Info(); 10 11 info.InputFilePath = @"D:\Data\World.tif"; //待裁剪影像 12 PIE.DataSource.IRasterDataset rDataset = PIE.DataSource.DatasetFactory.OpenRasterDataset(info.InputFilePath, PIE.DataSource.OpenMode.ReadOnly); 13 if (rDataset == null) return; 14 int count=rDataset.GetBandCount(); 15 List list = new List { }; 16 for (int i = 0; i < count; i++) { 17 list.Add(i); 18 } 19 info.listBands = list; 20 info.XFactor = 1; 21 info.YFactor = 1; 22 info.bInvalidValue = false; 23 info.OutputFilePath = @"D:\Data\clip_xy.tif"; //裁剪保存结果 24 info.Type = 0; // (等于0表示通过栅格范围裁剪) 25 info.XStart = 0; 26 info.XEnd = 500; 27 info.YStart = 0; 28 info.YEnd = 500; 29 info.FileType = "GTiff"; //Tiff数据的FileTypeCode为“GTiff”,IMG数据的FileTypeCode为"HFA",其他格式的为"ENVI"。; 30 31 PIE.SystemAlgo.ISystemAlgo algo = PIE.SystemAlgo.AlgoFactory.Instance().CreateAlgo("PIE.CommonAlgo.dll", "PIE.CommonAlgo.ImageClipAlgo"); 32 if (algo == null) return; 33 #endregion 34 //2、算法执行 35 PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents; 36 algo.Name = "影像裁剪"; 37 algo.Params = info; 38 //PIE.SystemAlgo.AlgoFactory.Instance().AsynExecuteAlgo(algo); 39 //bool OK = PIE.SystemAlgo.AlgoFactory.Instance().ExecuteAlgo(algo); 40 bool OK = algo.Execute(); 41 MessageBox.Show("裁剪成功!"); 42 } 43 //(二)使用shape文件裁剪 44 /// 45 ///图像裁剪算法测试,本算法实现了将World.tif根据省级行政区.shp文件裁剪生成clip_shp.tif文件 46 /// 47 private void button_shp_Click () 48 { 49 #region 1、参数设置 50 //Clip裁剪 51 PIE.CommonAlgo.DataPreImgClip_Exchange_Info info = new PIE.CommonAlgo.DataPreImgClip_Exchange_Info(); 52 //参数设置 53 string featurePath = @"D:\Data\省级行政区.shp"; //裁剪的几何要素 54 info.InputFilePath = @"D:\Data\World.tif"; //待裁剪影像 55 PIE.DataSource.IRasterDataset rDataset = PIE.DataSource.DatasetFactory.OpenRasterDataset(info.InputFilePath, PIE.DataSource.OpenMode.ReadOnly); 56 if (rDataset == null) return; 57 int count = rDataset.GetBandCount(); 58 List list = new List { }; 59 for (int i = 0; i < count; i++) 60 { 61 list.Add(i); 62 } 63 info.listBands = list; 64 info.bInvalidValue = false; 65 info.OutputFilePath = @"D:\Data\clip_shp.tif"; //裁剪保存结果 66 info.ShpFilePath = featurePath; 67 info.Type = 1; // (等于1表示使用shape文件裁剪) 68 info.FileType = "GTiff"; //Tiff数据的FileTypeCode为“GTiff”,IMG数据的FileTypeCode为"HFA",其他格式的为"ENVI"。; 69 PIE.SystemAlgo.ISystemAlgo algo = PIE.SystemAlgo.AlgoFactory.Instance().CreateAlgo("PIE.CommonAlgo.dll", "PIE.CommonAlgo.ImageClipAlgo"); 70 if (algo == null) return; 71 #endregion 72 //2、算法执行 73 PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents; 74 algo.Name = "影像裁剪"; 75 algo.Params = info; 76 PIE.SystemAlgo.AlgoFactory.Instance().AsynExecuteAlgo(algo); 77 //bool OK = PIE.SystemAlgo.AlgoFactory.Instance().ExecuteAlgo(algo); 78 //bool OK = algo.Execute(); 79 MessageBox.Show("裁剪成功!"); 80 } 81 //(三)使用几何范围裁剪 82 /// 83 ///图像裁剪算法测试,本算法实现了将World.tif根据省级行政区.shp文件中索引为0的geometry裁剪生成clip_shp.tif文件 84 /// 85 private void button_geometry_Click () 86 { 87 #region 1、参数设置 88 //Clip裁剪 89 PIE.CommonAlgo.DataPreImgClip_Exchange_Info info = new PIE.CommonAlgo.DataPreImgClip_Exchange_Info(); 90 //参数设置 91 string featurePath = @"D:\Data\省级行政区.shp"; //裁剪的几何要素 92 PIE.DataSource.IFeatureDataset fDataset = PIE.DataSource.DatasetFactory.OpenFeatureDataset(featurePath); 93 PIE.DataSource.IFeature feature = fDataset.GetNextFeature(); 94 fDataset.ResetReading(); 95 PIE.Geometry.IGeometry geometryClip = null; 96 while (feature != null) 97 { 98 geometryClip = feature.Geometry; 99 feature = fDataset.GetNextFeature();100 break;101 }102 info.InputFilePath = @"D:\Data\World.tif"; //待裁剪影像103 PIE.DataSource.IRasterDataset rDataset = PIE.DataSource.DatasetFactory.OpenRasterDataset(info.InputFilePath, PIE.DataSource.OpenMode.ReadOnly);104 if (rDataset == null) return;105 int count = rDataset.GetBandCount();106 List list = new List { };107 for (int i = 0; i < count; i++)108 {109 list.Add(i);110 }111 info.listBands = list;112 info.bInvalidValue = false;113 info.OutputFilePath = @"D:\Data\clip_geometry.tif"; //裁剪保存结果114 info.Type = 2; // (等于2表示使用几何范围裁剪)115 info.Geometry = geometryClip;116 info.FileType = "GTiff"; //Tiff数据的FileTypeCode为“GTiff”,IMG数据的FileTypeCode为"HFA",其他格式的为"ENVI"。;117 PIE.SystemAlgo.ISystemAlgo algo = PIE.SystemAlgo.AlgoFactory.Instance().CreateAlgo("PIE.CommonAlgo.dll", "PIE.CommonAlgo.ImageClipAlgo");118 if (algo == null) return;119 #endregion120 //2、算法执行121 PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents;122 algo.Name = "影像裁剪";123 algo.Params = info; PIE.SystemAlgo.AlgoFactory.Instance().AsynExecuteAlgo(algo);124 //bool OK = PIE.SystemAlgo.AlgoFactory.Instance().ExecuteAlgo(algo);125 //bool OK = algo.Execute();126 MessageBox.Show("裁剪成功!"); 127 }