地理处理

常规

如何执行模型工具

1
2
3
4
5
6
7
8
9
10
11
12
// get the model tool's parameter syntax from the model's help
string input_roads = @"C:\data\Input.gdb\PlanA_Roads";
string buff_dist_field = "Distance"; // use values from a field
string input_vegetation = @"C:\data\Input.gdb\vegetation";
string output_data = @"C:\data\Output.gdb\ClippedFC2";

// the model name is ExtractVegetation
string tool_path = @"C:\data\MB\Models.tbx\ExtractVegetation";

var args = Geoprocessing.MakeValueArray(input_roads, buff_dist_field, input_vegetation, output_data);

var result = await Geoprocessing.ExecuteToolAsync(tool_path, args);

设置地理处理范围环境

1
2
3
var parameters = Geoprocessing.MakeValueArray(@"C:\data\data.gdb\HighwaysUTM11", @"C:\data\data.gdb\Highways_extent");
var ext = Geoprocessing.MakeEnvironmentArray(extent: "460532 3773964 525111 3827494");
var gp_result = await Geoprocessing.ExecuteToolAsync("management.CopyFeatures", parameters, ext);

在地理处理窗格中打开脚本工具对话框

1
2
3
4
5
6
7
8
9
string input_data = @"C:\data\data.gdb\Population";
string out_pdf = @"C:\temp\Reports.pdf";
string field_name = "INCOME";
// use defaults for other parameters - no need to pass any value
var arguments = Geoprocessing.MakeValueArray(input_data, out_pdf, field_name);

string toolpath = @"C:\data\WorkflowTools.tbx\MakeHistogram";

Geoprocessing.OpenToolDialog(toolpath, arguments);

获取地理处理工程项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var gpItems = CoreModule.CurrentProject.Items.OfType<GeoprocessingProjectItem>();

// go through all the available toolboxes
foreach (var gpItem in gpItems)
{
var itemsInsideToolBox = gpItem.GetItems();

// then for each toolbox list the tools inside
foreach (var toolItem in itemsInsideToolBox)
{
string newTool = String.Join(";", new string[] { toolItem.Path, toolItem.Name });
// do something with the newTool
// for example, add to a list to track or use them later
}
}

阻止使用 GP 创建的要素类自动添加到地图

1
2
3
4
5
// However, settings in Pro App's Geoprocessing Options will override option set in code
// for example, in Pro App's Options > Geoprocessing dialog, if you check 'Add output datasets to an open map'
// then the output WILL BE added to history overriding settings in code
var CopyfeaturesParams = Geoprocessing.MakeValueArray("C:\\data\\Input.gdb\\PlanA_Roads", "C:\\data\\Input.gdb\\Roads_copy");
IGPResult gpResult = await Geoprocessing.ExecuteToolAsync("management.CopyFeatures", CopyfeaturesParams, null, null, null, GPExecuteToolFlags.None);

GPExecuteToolFlags.AddToHistory 会将执行消息添加到 Hisotry

1
2
3
4
5
// However, settings in Pro App's Geoprocessing Options will override option set in code
// for example, if in Options > Geoprocessing dialog, if you uncheck 'Write geoprocessing operations to Geoprocessing History'
// then the output will not be added to history.
var args2 = Geoprocessing.MakeValueArray("C:\\data\\Vegetation.shp", "NewField", "TEXT");
var result2 = await Geoprocessing.ExecuteToolAsync("management.AddField", args2, null, null, null, GPExecuteToolFlags.AddToHistory);

多环缓冲器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//The data referenced in this snippet can be downloaded from the arcgis-pro-sdk-community-samples repo
//https://github.com/Esri/arcgis-pro-sdk-community-samples
async Task<IGPResult> CreateRings(EditingTemplate currentTemplate)
{
var paramsArray = Geoprocessing.MakeValueArray(currentTemplate.MapMember.Name,
@"C:\Data\FeatureTest\FeatureTest.gdb\Points_MultipleRingBuffer",
new List<string> { "1000", "2000" }, "Meters", "Distance",
"ALL", "FULL");

IGPResult ringsResult = await Geoprocessing.ExecuteToolAsync("Analysis.MultipleRingBuffer", paramsArray);
var messages = string.IsNullOrEmpty(gpResult.ReturnValue)
? $@"Error in gp tool: {gpResult.ErrorMessages}"
: $@"Ok: {gpResult.ReturnValue}";

return ringsResult;
}

地理处理工具的非阻塞执行

1
2
3
4
5
6
7
8
9
10
11
12
//The data referenced in this snippet can be downloaded from the arcgis-pro-sdk-community-samples repo
//https://github.com/Esri/arcgis-pro-sdk-community-samples
string in_data = @"C:\tools\data.gdb\cities";
string cities_buff = @"E:\data\data.gdb\cities_2km";

var valueArray = Geoprocessing.MakeValueArray(in_data, cities_buff, "2000 Meters");

// to let the GP tool run asynchronously without blocking the main thread
// use the GPThread option of GPExecuteToolFlasgs
//
GPExecuteToolFlags flags = GPExecuteToolFlags.GPThread; // instruct the tool run non-blocking GPThread
IGPResult bufferResult = await Geoprocessing.ExecuteToolAsync("Analysis.Buffer", valueArray, null, null, null, flags);

如何传递具有多个或复杂输入值的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var environments = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);

string toolName = "Snap_edit"; // or use edit.Snap

// Snap tool takes multiple inputs each of which has
// Three (3) parts: a feature class or layer, a string value and a distance
// Each part is separated by a semicolon - you can get example of sytax from the tool documentation page
var snapEnv = @"'C:/SnapProject/fgdb.gdb/line_1' END '2 Meters';'C:/SnapProject/fgdb.gdb/points_1' VERTEX '1 Meters';'C:/SnapProject/fgdb.gdb/otherline_1' END '20 Meters'";

var infc = @"C:/SnapProject/fgdb.gdb/poly_1";

var snapParams = Geoprocessing.MakeValueArray(infc, snapEnv);

GPExecuteToolFlags tokens = GPExecuteToolFlags.RefreshProjectItems | GPExecuteToolFlags.GPThread | GPExecuteToolFlags.AddToHistory;

IGPResult snapResult = await Geoprocessing.ExecuteToolAsync(toolName, parameters, environments, null, null, tokens);

//return gpResult;

地理处理选项

获取地理处理选项

1
2
3
4
5
6
//These options are for behavior of interactive GP tools _only_.

var overwrite_gp = ApplicationOptions.GeoprocessingOptions.OverwriteExistingDatasets;
var remove_gp = ApplicationOptions.GeoprocessingOptions.RemoveOverwrittenLayers;
var addoutput_gp = ApplicationOptions.GeoprocessingOptions.AddOutputDatasetsToOpenMap;
var history_gp = ApplicationOptions.GeoprocessingOptions.WriteGPOperationsToHistory;

设置地理处理选项

1
2
3
4
5
6
7
8
9
10
11
12
//Note: changing these options modifies behavior of interactive GP tools _only_.
//Use the ArcGIS.Desktop.Core.Geoprocessing.GPExecuteToolFlags enum parameter
//to modify the behavior of Geoprocessing.ExecuteToolAsync(...)

//Note: setting GeoprocessingOptions requires the QueuedTask
QueuedTask.Run(() =>
{
ApplicationOptions.GeoprocessingOptions.SetOverwriteExistingDatasets(true);
ApplicationOptions.GeoprocessingOptions.SetRemoveOverwrittenLayers(false);
ApplicationOptions.GeoprocessingOptions.SetAddOutputDatasetsToOpenMap(true);
ApplicationOptions.GeoprocessingOptions.SetWriteGPOperationsToHistory(false);
});

;