/// <summary> /// 图层切割栅格数据 /// </summary> /// <param name="rasterLayer">栅格数据图层</param> /// <param name="clipLayer">切割适量图层</param> /// <param name="outputFullPath">切割栅格完整路径</param> public void ClipRasterByLayer(ILayer rasterLayer, ILayer clipLayer, string outputFullPath) { string rasterPath = GetLayerPath(rasterLayer); string clipPath = GetLayerPath(clipLayer); ExtractByMask clipTool = new ExtractByMask(); clipTool.in_raster = rasterPath; clipTool.in_mask_data = clipPath; clipTool.out_raster = outputFullPath; Geoprocessor processor = new Geoprocessor(); IGPProcess process = null; processor.OverwriteOutput = true; process = clipTool; processor.Validate(process, true); processor.Execute(process, null); DirectoryInfo di = new DirectoryInfo(outputFullPath); string rasterDir = di.Parent.FullName; string name = di.Name; IWorkspaceFactory rasterWorkspaceFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace rasterWorkspace = rasterWorkspaceFactory.OpenFromFile(rasterDir, 0) as IRasterWorkspace; IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(name); IRasterLayer rasterLyr = new RasterLayerClass(); rasterLyr.CreateFromDataset(rasterDataset); m_mapControl.Map.AddLayer(rasterLyr as ILayer); }
/// <summary> /// 图层切割栅格数据 /// </summary> /// <param name="rasterLayer">栅格数据图层</param> /// <param name="clipLayer">切割适量图层</param> /// <param name="outputFullPath">切割栅格完整路径</param> public void ClipRasterByLayer(ILayer rasterLayer, ILayer clipLayer, string outputFullPath) { string rasterPath = GetLayerPath(rasterLayer); string clipPath = GetLayerPath(clipLayer); ExtractByMask clipTool = new ExtractByMask(); clipTool.in_raster = rasterPath; clipTool.in_mask_data = clipPath; clipTool.out_raster = outputFullPath; Geoprocessor processor = new Geoprocessor(); IGPProcess process = null; processor.OverwriteOutput = true; process = clipTool; processor.Validate(process, true); processor.Execute(process, null); DirectoryInfo di = new DirectoryInfo(outputFullPath); string rasterDir = di.Parent.FullName; string name = di.Name; IWorkspaceFactory rasterWorkspaceFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace rasterWorkspace = rasterWorkspaceFactory.OpenFromFile(rasterDir, 0) as IRasterWorkspace; IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(name); IRasterLayer rasterLyr = new RasterLayerClass(); rasterLyr.CreateFromDataset(rasterDataset); m_mapControl.Map.AddLayer(rasterLyr as ILayer); }
/// <summary> /// 栅格掩膜提取 /// </summary> /// <param name="inRasterFile"></param> /// <param name="inBorderFile"></param> /// <param name="outFile"></param> public static void RasterExtractByMask(string inRasterFile, string inBorderFile, string outFile) { ExtractByMask pExtractByMask = new ExtractByMask { in_raster = inRasterFile, in_mask_data = inBorderFile, out_raster = outFile }; GeoprocessorHelper.GpExecute(pExtractByMask); }
/// <summary> /// 采用矢量数据裁剪栅格 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MnuClipRasterByVector_Click(object sender, EventArgs e) { Geoprocessor GP; GP = new Geoprocessor(); ExtractByMask extractbymask1 = new ExtractByMask();//实例化extract by mask工具类 extractbymask1.in_raster = Application.StartupPath + "\\mapdata\\baotouDEM.img"; extractbymask1.out_raster = Application.StartupPath + "\\temp\\maskDEM.img"; extractbymask1.in_mask_data = Application.StartupPath + "\\mapdata\\New_Shapefile.shp"; GP.Execute(extractbymask1, null); MessageBox.Show("成功导出!"); }
private void btn_OK_Click(object sender, EventArgs e) { Geoprocessor gp = new Geoprocessor(); ExtractByMask mask = new ExtractByMask(); mask.in_raster = rasterName; mask.in_mask_data = shapefileName; mask.out_raster = txtOutput.Text; try { gp.Execute(mask, null); MessageBox.Show("裁切成功!"); } catch (System.Exception ex) { MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
/// <summary> /// 执行裁剪 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Clip_backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //设置CancellationPending可以退出任务线程 if (worker.CancellationPending == true) { e.Cancel = true; } else { FileInfo inputfileinfo = new FileInfo(clip_input.Text); FileInfo maskfileinfo = null; FileInfo outputfileinfo = new FileInfo(clip_output.Text); if (clip_mask.Text != "") { maskfileinfo = new FileInfo(clip_mask.Text); } IWorkspaceFactory outputworkspaceFactory = new RasterWorkspaceFactoryClass(); IWorkspace outputworkspaceop = outputworkspaceFactory.OpenFromFile(outputfileinfo.DirectoryName, 0); //创建输出工作空间 string inputextension = "1"; //0为矢量,1为栅格 string maskextension = "1"; //0为矢量,1为栅格 if (inputfileinfo.Extension == ".shp") { inputextension = "0"; } if (clip_mask.Text == "") { maskextension = "2"; //自定义包络线 } else { if (maskfileinfo.Extension == ".shp") { maskextension = "0"; } } if (clip_featureboundarycheck.Enabled == true && clip_featureboundarycheck.Checked) { maskextension += "1"; //根据图形边界裁剪 } else if (clip_featureboundarycheck.Enabled == true) { maskextension += "0"; //根据包络线裁剪 } //启动预处理计时 watch = new Stopwatch(); watch.Start(); //执行裁剪 switch (inputextension + maskextension) { case "00": //矢量裁剪矢量,mask要素类型不是polygon,根据mask包络线裁剪矢量 case "001": //矢量裁剪矢量,mask要素类型是polygon,根据polygon裁剪矢量 Geoprocessor geoprocessor = new Geoprocessor(); object infeature = inputfileinfo.FullName + ";" + maskfileinfo.FullName; object outfeature = outputfileinfo.FullName; Intersect intersect = new Intersect(infeature, outfeature); geoprocessor.Execute(intersect, null); break; case "01": //栅格裁剪矢量,根据mask包络线裁剪矢量 case "000": //矢量裁剪矢量,mask要素类型是polygon,根据mask包络线裁剪矢量 case "100": //矢量裁剪栅格,mask要素类型是polygon,根据mask包络线裁剪矢量 case "02": //mask裁剪矢量,没有mask数据,自定义包络线裁剪矢量 case "12": //mask裁剪栅格,没有mask数据,自定义包络线裁剪栅格 case "10": //矢量裁剪栅格,mask要素类型不是polygon,根据mask包络线裁剪矢量 IEnvelope envelope = new EnvelopeClass(); envelope.XMax = Convert.ToDouble(clip_rightbottomX.Text); envelope.XMin = Convert.ToDouble(clip_lefttopX.Text); envelope.YMax = Convert.ToDouble(clip_lefttopY.Text); envelope.YMin = Convert.ToDouble(clip_rightbottomY.Text); IPoint point = new PointClass(); IPointCollection pCol = new PolygonClass(); point.X = envelope.XMax; point.Y = envelope.YMax; pCol.AddPoint(point, Type.Missing, Type.Missing); point.X = envelope.XMax; point.Y = envelope.YMin; pCol.AddPoint(point, Type.Missing, Type.Missing); point.X = envelope.XMin; point.Y = envelope.YMin; pCol.AddPoint(point, Type.Missing, Type.Missing); point.X = envelope.XMin; point.Y = envelope.YMax; pCol.AddPoint(point, Type.Missing, Type.Missing); IPolygon polygon = pCol as IPolygon; polygon.Close(); //自定义图层 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "Shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; //设置geometry definition IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon; pGeometryDefEdit.SpatialReference_2 = null; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(Application.StartupPath + "\\temp", 0) as IFeatureWorkspace; //检查文件名是否已存在 int changefilename = 0; while (System.IO.File.Exists(Application.StartupPath + "\\temp\\mask" + changefilename + ".shp")) { changefilename++; //若存在则依次递增,直至文件名不重复 } pFeatureWorkspace.CreateFeatureClass("mask" + changefilename + ".shp", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); IFeatureClass featureclass = pFeatureWorkspace.OpenFeatureClass("mask" + changefilename + ".shp"); IFeature feature = featureclass.CreateFeature(); feature.Shape = polygon; feature.Store(); Geoprocessor geoprocessor2 = new Geoprocessor(); if (inputextension == "0") { //自定义包络线裁剪矢量 object infeature2 = inputfileinfo.FullName + ";" + Application.StartupPath + "\\temp\\mask" + changefilename + ".shp";//mask1为根据自定义包络线生成的polygon,放在临时文件夹 object outfeature2 = outputfileinfo.FullName; Intersect intersect2 = new Intersect(infeature2, outfeature2); geoprocessor2.Execute(intersect2, null); } else { //自定义包络线裁剪栅格 object inraster2 = inputfileinfo.FullName; object maskdata2 = Application.StartupPath + "\\temp\\mask" + changefilename + ".shp";//mask为根据自定义包络线生成的polygon,放在临时文件夹 object outraster2 = outputfileinfo.FullName; ExtractByMask extractbymask2 = new ExtractByMask(inraster2, maskdata2, outraster2); geoprocessor2.Execute(extractbymask2, null); } break; case "101": //矢量裁剪栅格,mask要素类型是polygon,根据polygon裁剪矢量 case "11": //栅格裁剪栅格 object inraster = inputfileinfo.FullName; object maskdata = maskfileinfo.FullName; object outraster = outputfileinfo.FullName; ExtractByMask extractbymask = new ExtractByMask(inraster, maskdata, outraster); Geoprocessor geoprocessor3 = new Geoprocessor(); geoprocessor3.Execute(extractbymask, null); break; } } }