1图层操作 查看图层名称
private void 查看图层名称ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 获取地图控件中的所有图层
IEnumLayer enumLayer = MainMapControl.Map.Layers;
enumLayer.Reset();
// 保存图层名称和序号的字符串
StringBuilder layerInfo = new StringBuilder();
// 循环计数变量
int layerIndex = 0;
// 遍历每个图层并获取名称和序号
ILayer layer = enumLayer.Next();
while (layer != null)
{
// 将图层序号和名称添加到字符串中
layerInfo.AppendLine($"图层序号: {layerIndex}, 图层名称: {layer.Name}");
// 增加图层序号
layerIndex++;
layer = enumLayer.Next();
}
// 显示图层信息
MessageBox.Show(layerInfo.ToString(), "图层信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
2选择图层并高亮显示
private ILayer selectedLayer = null;
private void 选择图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 获取地图控件中的所有图层
IEnumLayer enumLayer = MainMapControl.Map.Layers;
enumLayer.Reset();
// 保存图层信息的字符串
StringBuilder layerInfo = new StringBuilder();
// 循环计数变量
int layerIndex = 0;
// 遍历每个图层并获取名称
ILayer layer = enumLayer.Next();
while (layer != null)
{
// 将图层序号和名称添加到字符串中
layerInfo.AppendLine($"图层序号: {layerIndex}, 图层名称: {layer.Name}");
layerIndex++;
layer = enumLayer.Next();
}
// 提示用户输入图层序号
string input = Interaction.InputBox("请输入要选择的图层序号:", "选择图层", "");
int selectedLayerIndex;
// 检查用户输入的是否是有效的整数
if (int.TryParse(input, out selectedLayerIndex))
{
// 获取地图控件中的所有图层
enumLayer.Reset();
for (int i = 0; i <= selectedLayerIndex; i++)
{
selectedLayer = enumLayer.Next();
if (selectedLayer == null)
{
break;
}
}
// 清除之前的选择
MainMapControl.Map.ClearSelection();
// 如果图层是要素图层,则进行选择
if (selectedLayer is IFeatureLayer featureLayer)
{
// 执行选择图层后的操作,例如高亮显示或其他处理
IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
featureSelection.SelectFeatures(null, esriSelectionResultEnum.esriSelectionResultNew, false);
MessageBox.Show($"您选择的图层是:{selectedLayer.Name},已高亮显示。", "选择结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (selectedLayer is IRasterLayer rasterLayer)
{
MessageBox.Show($"您选择的图层是:{selectedLayer.Name}。", "选择结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("无效的图层序号。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("选择失败。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
3缩放至所选图层
private void 缩放至所选图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 检查是否有图层被选中
if (selectedLayer != null)
{
// 获取图层的范围
IEnvelope layerExtent = null;
if (selectedLayer is IFeatureLayer featureLayer)
{
layerExtent = GetLayerExtent(featureLayer);
}
else if (selectedLayer is IRasterLayer rasterLayer)
{
layerExtent = GetRasterExtent(rasterLayer.Raster);
}
if (layerExtent != null && !layerExtent.IsEmpty)
{
// 设置地图控件的范围为图层范围
MainMapControl.Extent = layerExtent;
MainMapControl.Refresh();
}
else
{
MessageBox.Show("无法获取图层的范围。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("请先选择一个图层。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
// 获取图层的范围
private IEnvelope GetLayerExtent(ILayer layer)
{
if (layer is IFeatureLayer featureLayer)
{
// 获取要素图层的范围
IFeatureClass featureClass = featureLayer.FeatureClass;
IGeoDataset geoDataset = featureClass as IGeoDataset;
return geoDataset.Extent;
}
else
{
// 其他类型的图层可能无法获取范围
return null;
}
}
private IEnvelope GetRasterExtent(IRaster raster)
{
if (raster is IRasterProps rasterProps)
{
return rasterProps.Extent;
}
else
{
// 如果无法获取栅格图层的范围,则返回空
return null;
}
}
4清除所选图层
private void 清除选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 检查是否有图层被选中
if (selectedLayer != null)
{
if (selectedLayer is IFeatureLayer featureLayer)
{
// 如果选中的图层是要素图层,则清除其选择集
IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
featureSelection.Clear();
}
else if (selectedLayer is IRasterLayer rasterLayer)
{
// 如果选中的图层是栅格图层,则无法清除选择集
// 可以根据需要执行其他操作,比如重置地图范围等
// 这里仅刷新地图控件
}
else
{
MessageBox.Show("无效的图层类型。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// 刷新地图控件
MainMapControl.Refresh();
}
}