Select to view content in your preferred language

How to add a new layer to a grouplayer

673
2
Jump to solution
01-21-2024 05:31 PM
YetaoYang
Emerging Contributor

I created a grouplayer and called the pointtoraster tool to get an output layer. So how do I add the layer output by this tool to this layer group?

var map = MapView.Active.Map;
// 创建一个图层组
GroupLayer groupLayer = null;
await QueuedTask.Run(() =>
{
// 创建图层组
groupLayer = LayerFactory.Instance.CreateGroupLayer(map, 0, "表面模型");
});
foreach (FeatureLayer selectedLayer in _selectedLayers)
{
var fcName = selectedLayer.Name;
var fieldName = comboBox2.Text;
var gdbPath = Project.Current.DefaultGeodatabasePath;
var outputdata = System.IO.Path.Combine(gdbPath, $"{fcName}_表面模型");

await QueuedTask.Run(async () =>
{
try
{
// 构建工具箱工具的参数
var parameters = Geoprocessing.MakeValueArray(
fcName,
fieldName,
outputdata,
"MAXIMUM",
null,
cellsize
);

// 执行工具
var progDlg = new ProgressDialog("进度条", "取消", 100, true);
progDlg.Show();
var results = await Geoprocessing.ExecuteToolAsync("conversion.PointToRaster", parameters);
// 工具运行完毕后关闭进度条
progDlg.Hide();

// 处理结果
// 获取错误信息
if (results.IsFailed)
{
await QueuedTask.Run(() =>
{
Geoprocessing.ShowMessageBox(results.Messages, "工具执行失败", GPMessageBoxStyle.Error);
});
}
else
{
MessageBox.Show("工具执行成功!");
await QueuedTask.Run(() =>
{
var layerDoc = new LayerDocument(new Uri(outputdata).LocalPath);
var createParams = new LayerCreationParams(layerDoc.GetCIMLayerDocument())
{
//Set visibility
IsVisible = false,
};
//Layer outputLayer = LayerFactory.Instance.CreateLayer<Layer>(createParams, MapView.Active.Map);
LayerFactory.Instance.CreateLayer<GroupLayer>(createParams, groupLayer);
// 获取 GroupLayer 中的图层集合
var layerCollection = groupLayer.Layers;

});
}
}
catch (Exception ex)
{
await QueuedTask.Run(() =>
{
//System.Windows.MessageBox.Show($"发生错误:{ex.Message}");
System.Diagnostics.Debug.WriteLine($"发生错误:{ex.Message}");
System.Diagnostics.Debug.WriteLine($"StackTrace: {ex.StackTrace}");
});
}
});

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

Your code implements creation of RasterLayer form lyrx file. Try line below to create raster layer from path:

                                var rasterLayer = LayerFactory.Instance.CreateLayer(new Uri(outputdata), groupLayer) as RasterLayer;

More info in ArcGIS Pro SDK API Reference

View solution in original post

0 Kudos
2 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Your code implements creation of RasterLayer form lyrx file. Try line below to create raster layer from path:

                                var rasterLayer = LayerFactory.Instance.CreateLayer(new Uri(outputdata), groupLayer) as RasterLayer;

More info in ArcGIS Pro SDK API Reference

0 Kudos
YetaoYang
Emerging Contributor

wow,thanks for your help, it works!

0 Kudos