private static void ReproCase() { Geoprocessor gp = new Geoprocessor { OverwriteOutput = true }; // Instantiate the geoprocessor using the managed assembly. string tbx = GetArcPadExePath().Replace("ArcPad.exe", @"DesktopTools10.0\Toolboxes\ArcPad Tools.tbx"); // Get path to ArcPadTools toolbox. string gdb = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"data\Riverside.gdb"); string axf = gdb.Replace(".gdb", ".axf"); string tool = "ArcPadCheckout_ArcPad"; string feat = string.Join(";", GetFeatures(OpenGeodatabaseWorkspace(gdb), ref gp)); // Get the features in the included gdb. Console.WriteLine("Will attempt to run the {0} tool...\n", tool); try { gp.AddToolbox(tbx); // Add the toolbox IVariantArray parameters = new VarArrayClass(); parameters.Add(feat); // Features parameters.Add(""); // Schema Only parameters.Add(""); // Password parameters.Add(""); // Encrypt parameters.Add(axf); // Axf if (File.Exists(axf)) { File.Delete(axf); // Delete the axf if it exists. Tool will not allow you to overwrite } var result = (IGeoProcessorResult2)gp.Execute(tool, parameters, null); // Execute the gp tool Messages(result); // Display the GP Messages for the successful tool. } catch (Exception e) { Messages(e, ref gp); } // Display the Exception/GP Messages for the failing tool finally { gp.RemoveToolbox(tbx); } }
/// <summary> /// 根据输入的中红外影像获取洪湖矢量边界 /// </summary> /// <param name="inputPath"></param> /// <param name="savePath"></param> public void CalculateBoundary(string inputPath, string savePath) { try { //使用模型来处理 _gp.AddToolbox(@"D:\RasterMaSysTemp\hhData\MyToolbox.tbx"); IVariantArray parametersArray = new VarArrayClass(); parametersArray.Add(inputPath); parametersArray.Add(savePath); _gp.Execute("GetHonghuBoundary", parametersArray, null); } catch (Exception e) { MessageBox.Show("获取边界出现错误:" + e.ToString()); } }
private void managed方式ToolStripMenuItem_Click(object sender, EventArgs e) { // Initialize the geoprocessor. Geoprocessor GP = new Geoprocessor(); GP.OverwriteOutput = true; // Add the BestPath toolbox. GP.AddToolbox(tbxPath); // Generate the array of parameters. IVariantArray parameters = new VarArrayClass(); parameters.Add(FileGDBPath + "\\LotIds"); parameters.Add("100 Feet"); parameters.Add(FileGDBPath + "\\LotIds_BufferCustomArray2"); object sev = null; try { // Execute the model tool by name. GP.Execute("Model2", parameters, null); Console.WriteLine(GP.GetMessages(ref sev)); } catch (Exception ex) { // Print geoprocessing execution error messages. Console.WriteLine(GP.GetMessages(ref sev)); //for (int i = 0; i < GP.MessageCount; i++) // Console.WriteLine(GP.GetMessage(i)); } }
public static bool CheckForArcPadTools(Geoprocessor gp, bool finalCheck = false) { while (true) { string tool; List <string> tools = new List <string>(); IGpEnumList toolCollection = gp.ListToolboxes("arc*"); while ((tool = toolCollection.Next()) != null) { tools.Add(tool); } if (!tools.Contains("ArcPad Tools(ArcPad)")) { if (!finalCheck) { gp.AddToolbox(ArcPadTbxPath); finalCheck = true; continue; } else { throw new Exception("Missing ArcPad Tools."); } } break; } return(true); }
/// <summary> /// 调用一个模型 /// </summary> public void GPtest2() { Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; gp.SetEnvironmentValue("scratchworkspace", @"F:\gpTest1\data"); gp.AddToolbox(@"F:\gpTest\Toolbox.tbx"); IVariantArray parameters = new VarArrayClass(); parameters.Add(@"F:\gpTest\data\test .xlsx"); parameters.Add("a"); parameters.Add("b"); ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem(2379); parameters.Add(spatialReference); parameters.Add(@"F:\gpTest\data\project1.shp"); try { gp.Execute("excel", parameters, null); } catch (Exception ex) { string a = ex.Message; } }
private void OK_Click(object sender, EventArgs e) { // 2013-04-10 张航宇 // 添加验证 if (string.IsNullOrWhiteSpace(buttonEdit1.Text)) { MessageBox.Show("请选择原DEM路径!"); return; } if (string.IsNullOrWhiteSpace(buttonEdit2.Text)) { MessageBox.Show("请选择山脊线生成路径!"); return; } if (string.IsNullOrWhiteSpace(buttonEdit3.Text)) { MessageBox.Show("请选择山谷线生成路径!"); return; } //1-定义GeoProcessor对象 this.gp = new Geoprocessor(); object sev = null; //2-设置参数 gp.OverwriteOutput = true; //3-设置工具箱所在的路径 gp.AddToolbox(Application.StartupPath + @"\Convert\TerrainTool.tbx"); //4-设置输入参数 ESRI.ArcGIS.esriSystem.IVariantArray parameters = new VarArrayClass(); parameters.Add(buttonEdit1.Text); parameters.Add(buttonEdit2.Text + "\\shanji"); parameters.Add(buttonEdit3.Text + "\\shangu"); parameters.Add(Convert.ToDouble(spinEdit1.Value)); IGeoProcessorResult results = null; try { results = (IGeoProcessorResult)gp.Execute("Terrain", parameters, null); if (results == null) { MessageBox.Show("生成失败!"); } MessageBox.Show("生成成功!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("生成失败!"); //throw; } }
public static string ConvertMXDToMSD(string mxdfile, string convertMXDtoMSDtoolboxPath) { if (!File.Exists(convertMXDtoMSDtoolboxPath)) { MessageBox.Show(string.Format("GP Toolbox {0} to convert mxd to msd is not found", convertMXDtoMSDtoolboxPath), "Convert MXD to MSD", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } Geoprocessor gp = new Geoprocessor() { OverwriteOutput = true }; gp.AddToolbox(convertMXDtoMSDtoolboxPath); IVariantArray gpparams = new VarArrayClass(); gpparams.Add(mxdfile); IGeoProcessorResult gpresult = null; try { gpresult = gp.Execute(Properties.Settings.Default.toolname, gpparams, null) as IGeoProcessorResult; } catch (Exception) { object sev = 2; System.Windows.MessageBox.Show(gp.GetMessages(ref sev)); return(null); } string msdfile = ((IGPString)gpresult.GetOutput(0)).Value; //MessageBox.Show(msdfile); gp = null; gpresult = null; return(msdfile); }
private void button1_Click(object sender, EventArgs e) { Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; gp.AddToolbox(this.PATH_ENVIEMNT + @"\Data\toolbox.tbx"); IVariantArray parameters = new VarArrayClass(); //MessageBox.Show(this.str_shp_road); //MessageBox.Show(this.str_tab_volunteer); //MessageBox.Show(this.str_shp_net6x6); //parameters.Add(6); //parameters.Add(6); parameters.Add("6"); parameters.Add("6"); parameters.Add(this.str_shp_road); //parameters.Add(this.m_pMapC2.Extent); parameters.Add(this.str_tab_volunteer); parameters.Add(this.str_shp_net6x6); gp.Execute("VModel", parameters, null); //this.m_pMapC2.AddShapeFile(this.PATH_ENVIEMNT + @"\Data", "net6x6.shp"); }
/// <summary> /// 执行自定义Model GP /// </summary> /// <param name="parameters">GP 输入参数</param> /// <param name="gpname">GP 名字</param> /// <param name="modelPath">工具箱路径</param> /// <param name="tempFolder">临时工作区路径</param> /// <returns></returns> public bool RunModelTool(string modelPath, string tempFolder, IVariantArray parameters, string gpname) { if (parameters == null) { return(false); } Geoprocessor geoprocessor = new Geoprocessor(); geoprocessor.OverwriteOutput = true; geoprocessor.SetEnvironmentValue("scratchworkspace", tempFolder); try { geoprocessor.AddToolbox(modelPath); geoprocessor.Execute(gpname, parameters, null); Marshal.ReleaseComObject(parameters); return(true); } catch { return(false); } }
// 执行可见范围分析模型 private void VisbilityModel(IFeatureLayer viewer, IFeatureLayer action, IFeatureLayer building) { Geoprocessor gp = new Geoprocessor() { OverwriteOutput = true }; gp.AddToolbox(DEBUG_DIR + "toolbox.tbx"); IVariantArray parameters = new VarArrayClass(); parameters.RemoveAll(); //parameters.Add(DEBUG_DIR + "Action.shp"); //parameters.Add(DEBUG_DIR + "Building.shp"); //parameters.Add(DEBUG_DIR + "Viewer.shp"); parameters.Add(action); parameters.Add(building); parameters.Add(viewer); parameters.Add(DEBUG_DIR + "VisitArea_FID_%值%.shp"); gp.Execute("myModel", parameters, null); }
public static string ConvertMXDToMSD(string mxdfile, string convertMXDtoMSDtoolboxPath) { if (!File.Exists(convertMXDtoMSDtoolboxPath)) { MessageBox.Show(string.Format("GP Toolbox {0} to convert mxd to msd is not found", convertMXDtoMSDtoolboxPath), "Convert MXD to MSD", MessageBoxButton.OK, MessageBoxImage.Error); return null; } Geoprocessor gp = new Geoprocessor() { OverwriteOutput = true }; gp.AddToolbox(convertMXDtoMSDtoolboxPath); IVariantArray gpparams = new VarArrayClass(); gpparams.Add(mxdfile); IGeoProcessorResult gpresult = null; try { gpresult = gp.Execute(Properties.Settings.Default.toolname, gpparams, null) as IGeoProcessorResult; } catch (Exception) { object sev = 2; System.Windows.MessageBox.Show(gp.GetMessages(ref sev)); return null; } string msdfile = ((IGPString)gpresult.GetOutput(0)).Value; //MessageBox.Show(msdfile); gp = null; gpresult = null; return msdfile; }
private void OK_Click(object sender, EventArgs e) { // 2013-04-10 张航宇 // 添加验证 if (string.IsNullOrWhiteSpace(buttonEdit1.Text)) { MessageBox.Show("请选择原DEM路径!"); return; } if (string.IsNullOrWhiteSpace(buttonEdit2.Text)) { MessageBox.Show("请选择山脊线生成路径!"); return; } if (string.IsNullOrWhiteSpace(buttonEdit3.Text)) { MessageBox.Show("请选择山谷线生成路径!"); return; } //1-定义GeoProcessor对象 this.gp = new Geoprocessor(); object sev = null; //2-设置参数 gp.OverwriteOutput = true; //3-设置工具箱所在的路径 gp.AddToolbox(Application.StartupPath + @"\Convert\TerrainTool.tbx"); //4-设置输入参数 ESRI.ArcGIS.esriSystem.IVariantArray parameters = new VarArrayClass(); parameters.Add(buttonEdit1.Text); parameters.Add(buttonEdit2.Text+"\\shanji"); parameters.Add(buttonEdit3.Text+"\\shangu"); parameters.Add(Convert.ToDouble(spinEdit1.Value)); IGeoProcessorResult results = null; try { results = (IGeoProcessorResult)gp.Execute("Terrain", parameters, null); if (results == null) { MessageBox.Show("生成失败!"); } MessageBox.Show("生成成功!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("生成失败!"); //throw; } }
private void btn_visit_Click(object sender, EventArgs e) { //FormProgressBar bar = new FormProgressBar(13); //bar.Show(); //IFeatureLayer pFeatureLayer_Building = GetFeatureLayerByName("Building"); //IFeatureLayer pFeatureLayer_Action = GetFeatureLayerByName("Action"); //IFeatureLayer pFeatureLayer_Viewer = GetFeatureLayerByName("Viewer"); //bar.GO(); //Geoprocessor gp = new Geoprocessor() { // OverwriteOutput = true, // AddOutputsToMap = true //}; //bar.GO(); //// 联合 //ESRI.ArcGIS.AnalysisTools.Union pUnion = new ESRI.ArcGIS.AnalysisTools.Union() { // in_features = DEBUG_DIR + "Action.shp" + ";" + DEBUG_DIR + "Building.shp", // out_feature_class = DEBUG_DIR + "BuildingAndAction.shp" //}; //gp.Execute(pUnion, null); //bar.GO(); //// 面转栅格 //ESRI.ArcGIS.ConversionTools.PolygonToRaster pPolygonToRaster = new ESRI.ArcGIS.ConversionTools.PolygonToRaster() { // in_features = DEBUG_DIR + "BuildingAndAction.shp", // out_rasterdataset = DEBUG_DIR + "RsBuildingAndAction.tif", // value_field = "height", // cellsize = 0.000001 //}; //gp.Execute(pPolygonToRaster, null); //bar.GO(); //// 观测点要素迭代 //IFeatureCursor pFeatureCursor = pFeatureLayer_Viewer.FeatureClass.Search(null, false); //IFeature pFeature = pFeatureCursor.NextFeature(); //int i = 0; //while (pFeature != null) //{ // m_pMapC2.Map.ClearSelection(); // m_pMapC2.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); // m_pMapC2.Map.SelectFeature(pFeatureLayer_Viewer, pFeature); // m_pMapC2.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); // // 视域分析 // ESRI.ArcGIS.SpatialAnalystTools.Viewshed pViewshed = new ESRI.ArcGIS.SpatialAnalystTools.Viewshed() { // in_observer_features = pFeatureLayer_Viewer, // in_raster = DEBUG_DIR + "RsBuildingAndAction.tif", // out_raster = DEBUG_DIR + "RsVisibility_" + (i).ToString() + ".tif" // }; // gp.Execute(pViewshed, null); // bar.GO(); // // 掩模 // ESRI.ArcGIS.SpatialAnalystTools.ExtractByMask pExtractByMask = new ESRI.ArcGIS.SpatialAnalystTools.ExtractByMask() { // in_raster = DEBUG_DIR + "RsVisibility_" + (i).ToString() + ".tif", // in_mask_data = pFeatureLayer_Action, // out_raster = DEBUG_DIR + "RsVisAction_" + (i).ToString() + ".tif" // }; // gp.Execute(pExtractByMask, null); // bar.GO(); // // 栅格转面 // ESRI.ArcGIS.ConversionTools.RasterToPolygon pRasterToPolygon = new ESRI.ArcGIS.ConversionTools.RasterToPolygon() { // in_raster = DEBUG_DIR + "RsVisAction_" + (i).ToString() + ".tif", // raster_field = "Value", // out_polygon_features = DEBUG_DIR + "VisAction_" + (i).ToString() + ".shp" // }; // gp.Execute(pRasterToPolygon, null); // m_pMapC2.AddShapeFile(DEBUG_DIR, "VisAction_" + (i).ToString() + ".shp"); // IFeatureLayer pFeatureLayer_vis = GetFeatureLayerByName("visaction_" + (i).ToString()); // IFeatureCursor pFeatureCursor_vis = pFeatureLayer_vis.FeatureClass.Search(null, false); // IFeature pFeat = pFeatureCursor_vis.NextFeature(); // while (pFeat != null) // { // if (pFeat.get_Value(3).ToString() == "1") // { // IArea pArea = pFeat.Shape as IArea; // MessageBox.Show(pArea.Area.ToString()); // } // pFeat = pFeatureCursor_vis.NextFeature(); // } // bar.GO(); // pFeature = pFeatureCursor.NextFeature(); // i += 1; //} //bar.OK(); //m_pMapC2.Map.DeleteLayer(GetFeatureLayerByName("Viewer")); //m_pMapC2.Refresh(); Geoprocessor gp = new Geoprocessor() { OverwriteOutput = true, //TemporaryMapLayers = false //AddOutputsToMap = true }; gp.AddToolbox(DEBUG_DIR + "toolbox.tbx"); IVariantArray parameters = new VarArrayClass(); parameters.RemoveAll(); parameters.Add(DEBUG_DIR + "Action.shp"); parameters.Add(DEBUG_DIR + "Building.shp"); parameters.Add(GetFeatureLayerByName("Viewer")); //parameters.Add(DEBUG_DIR + "Viewer.shp"); parameters.Add(DEBUG_DIR + "VisitArea_%Value%.shp"); gp.Execute("myMod", parameters, null); //axTOCControl_main.EnableLayerDragDrop = true; //m_pMapC2.AddShapeFile(DEBUG_DIR, "VisitArea_0.shp"); //m_pMapC2.AddShapeFile(DEBUG_DIR, "VisitArea_1.shp"); //m_pMapC2.AddShapeFile(DEBUG_DIR, "VisitArea_2.shp"); ////IArea pArea = GetFeatureLayerByName("Building").FeatureClass.Search(null, false).NextFeature().Shape as IArea; //////IArea pArea = GetFeatureLayerByName("VisitArea_0") as IArea; ////MessageBox.Show(pArea.Area.ToString()); }