这里放的代码都是目前感觉比较简洁的,可能会有缺陷,但是还是效率优先吧,以后有问题再补充。
using ESRI.ArcGIS.SystemUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
1. 打开地图文档(简便、建议)
//using ESRI.ArcGIS.Controls; 引用
ICommand command = new ControlsOpenDocCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
mainTOCControl.SetBuddyControl(mainMapControl); // 手动绑定伙伴控件
1.2 保存地图文档
private void saveToolStrip_Click(object sender, EventArgs e)
{
try
{
string sMxdFileName = mainMapControl.DocumentFilename;
IMapDocument pMapDocument = new MapDocumentClass();
//检查文档是否为空以及有效性
if (sMxdFileName != null && mainMapControl.CheckMxFile(sMxdFileName))
{
if (pMapDocument.get_IsReadOnly(sMxdFileName))
{
MessageBox.Show("地图本当为只读,不能保存!");
pMapDocument.Close();
return;
}
else
{
SaveFileDialog pSaveFileDialog = new System.Windows.Forms.SaveFileDialog();
pSaveFileDialog.Title = "请选择保存路径";
pSaveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
//当相同的文件存在是提示错误
pSaveFileDialog.OverwritePrompt = true;
pSaveFileDialog.RestoreDirectory = true;
if (pSaveFileDialog.ShowDialog() == DialogResult.OK)
{
//获取名字
sMxdFileName = pSaveFileDialog.FileName;
}
else
{
return;
}
pMapDocument.New(sMxdFileName);
pMapDocument.ReplaceContents(mainMapControl.Map as IMxdContents);
//保存为绝对路径
pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
pMapDocument.Close();
MessageBox.Show("保存文档成功");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
1.3新建
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog SaveFileDlg = new SaveFileDialog();
SaveFileDlg.Filter = "地图文档文件(*.mxd)|*.mxd";
SaveFileDlg.Title = "输入需要新建地图文档的名称";
SaveFileDlg.ShowDialog();
IMapDocument pMapDocument = new MapDocumentClass();
string strDocFile = SaveFileDlg.FileName;
if (strDocFile == string.Empty)
return;
pMapDocument.New(strDocFile);
pMapDocument.Open(strDocFile, "");
mainMapControl.Map= pMapDocument.get_Map(0);
}
1.4
Application.Exit();
2. 打开数据(数据库,shp, 栅格等多种数据类型)
// 加载很多种数据
ICommand command = new ControlsAddDataCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
mainMapControl.ActiveView.Refresh();
2.2 添加shp数据(为了效率不建议)
// using ESRI.ArcGIS.Geodatabase;
// using ESRI.ArcGIS.DataSourcesFile;
// using ESRI.ArcGIS.DataSourcesRaster;
private void 添加ShapefileToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
//同样实例化一个打开文件的类对象
OpenFileDialog open = new OpenFileDialog();
// 如果打开正确
if (open.ShowDialog() == DialogResult.OK)
{
//首先定义一个空的路径
string filePath = string.Empty;
// 然后定义一个空的文件名
string file = string.Empty;
// 获取完整的文件路径
string filedir = open.FileName;
//如果路径为空,嘛都不返回
if (fileDir == "") return;
// 对完整路径进行截取 获取最后一个斜杠的索引
int pos = filedir.LastIndexOf('\\');
// 截取字符串 路径
filePath =filedir.Substring(0, pos);
//文件名
file = filedir.Substring(pos+1);
// 需要两个参数
axMapControl1.AddShapeFile(filePath, file);
//刷新
axMapControl1.ActiveView.Refresh();
}
}
catch (Exception)
{
MessageBox.Show("请打开正确的文档!", "提醒",MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}
2.3 打开栅格
OpenFileDialog open = new OpenFileDialog();
open.CheckFileExists = true;
open.Title = "打开shp文件";
open.RestoreDirectory = true;
open.Multiselect = true;
open.Filter = "(*.tif)|*.tif|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg|(*.png)|*.png|(*.bmp)|*.bmp|(*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK)
{
string filePath = open.FileName;
if (filePath == "") return;
IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromFilePath(filePath);
mainMapControl.AddLayer(rasterLayer, 0);
mainMapControl.ActiveView.Refresh();
mainTOCControl1.SetBuddyControl(mainMapControl);
}
- 地图浏览
3..1 全图
private void FullExtentTSButton_Click(object sender, EventArgs e)
{
mainMapControl.Extent = mainMapControl.FullExtent;
}
3.2 等比例放大
F1
private void btnZoomInStep_Click(object sender, EventArgs e)
{
IEnvelope pEnvelope = mainMapControl.Extent;
pEnvelope.Expand(0.5, 0.5, true); //放大2倍
mainMapControl.Extent = pEnvelope;
mainMapControl.ActiveView.Refresh();
}
F2
ICommand command = new ControlsMapZoomInFixedCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
3.3 等比例缩小(与放大的区别在于多了一个中心点的设置)
F1
private void btnZoomIOutStep_Click(object sender, EventArgs e)
{
IActiveView pActiveView = mainMapControl.ActiveView;
IPoint centerPoint = new PointClass();
centerPoint.PutCoords((pActiveView.Extent.XMin + pActiveView.Extent.XMax) / 2, (pActiveView.Extent.YMax + pActiveView.Extent.YMin) / 2);
IEnvelope envlop = pActiveView.Extent;
envlop.Expand(1.5, 1.5, true); //与放大的区别在于Expand的参数不同
pActiveView.Extent.CenterAt(centerPoint);
pActiveView.Extent = envlop;
pActiveView.Refresh();
}
F2
ICommand command = new ControlsMapZoomOutFixedCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
3.4 上一视图
F1
// 定义全局变量
IExtentStack pExtentStack;
private void PreViewTSButton_Click(object sender, EventArgs e)
{
pExtentStack = mainMapControl.ActiveView.ExtentStack;
//判断是否可以回到前一视图,第一个视图没有前视图
if (pExtentStack.CanUndo())
{
pExtentStack.Undo(); //撤销到上一视图范围
NextViewTSButton.Enabled = true; //后一视图可以使用
if (!pExtentStack.CanUndo())
{
PreViewTSButton.Enabled = false; //前一视图不能使用
}
}
mainMapControl.ActiveView.Refresh();
}
F2
ICommand command = new ControlsMapZoomToLastExtentBackCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
mainMapControl.ActiveView.Refresh();
3.5 下一视图
F1
private void NextViewTSButton_Click(object sender, EventArgs e)
{
pExtentStack = mainMapControl.ActiveView.ExtentStack;
//判断是否可以回到后一视图,最后一个视图没有后一视图
if (pExtentStack.CanRedo()) //如果可以重做下一视图
{
pExtentStack.Redo(); //重做到下一视图
PreViewTSButton.Enabled = true; //上一视图按钮可以使用
if (!pExtentStack.CanRedo()) //如果不可以重做下一视图
{
NextViewTSButton.Enabled = false; //下一视图不能用
}
}
mainMapControl.ActiveView.Refresh();
}
F2
ICommand command = new ControlsMapZoomToLastExtentForwardCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
mainMapControl.ActiveView.Refresh();
拉框放大、拉框缩小、漫游 = > 无脑方法(直接调用ICommand或者ITool接口)
无脑1. 拉框缩小
private void tsbtnZoomOut_Click(object sender, EventArgs e)
{
mainMapControl.CurrentTool = null;
ICommand command = new ControlsMapZoomOutToolClass();
command.OnCreate(mainMapControl.Object);
mainMapControl.CurrentTool = command as ITool;
}
无脑2. 拉框放大
private void tsbtnZoomIn_Click(object sender, EventArgs e)
{
mainMapControl.CurrentTool = null;
ICommand command = new ControlsMapZoomInToolClass();
command.OnCreate(mainMapControl.Object);
mainMapControl.CurrentTool = command as ITool;
}
无脑3. 漫游
mainMapControl.CurrentTool = null;
ICommand command = new ControlsMapPanToolClass();
command.OnCreate(mainMapControl.Object);
mainMapControl.CurrentTool = command as ITool;
mainMapControl.ActiveView.Refresh();
4. 保存地图文档
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// 获取当前文档的路径与名字
string smxdFileName = mainMapControl.DocumentFilename;
IMapDocument pMapDocument = new MapDocumentClass();
//检查文档的有效性
if (smxdFileName != null && mainMapControl.CheckMxFile(smxdFileName))
{
//如果是只读
if (pMapDocument.get_IsReadOnly(smxdFileName))
{
MessageBox.Show("该文档是只读文档!!!");
pMapDocument.Close();
return;
}
// 如果可以保存
else
{
SaveFileDialog pSaveFileDialog = new System.Windows.Forms.SaveFileDialog();
pSaveFileDialog.Title = "选择保存路径";
pSaveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
pSaveFileDialog.OverwritePrompt = true;
pSaveFileDialog.RestoreDirectory = true;
// 如果可以
if (pSaveFileDialog.ShowDialog() == DialogResult.OK)
{
smxdFileName = pSaveFileDialog.FileName;
}
// 如果失败
else
{
return;
}
//可以之后继续保存
pMapDocument.New(smxdFileName);
pMapDocument.ReplaceContents(mainMapControl.Map as IMxdContents);
pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
pMapDocument.Close();
MessageBox.Show("保存成功!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
4.2 另存为
F1.简便方法, 调用ICommand接口
ICommand command = new ControlsSaveAsDocCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
F2
// 获取当前文档的路径与名字
string smxdFileName = mainMapControl.DocumentFilename;
IMapDocument pMapDocument = new MapDocumentClass();
//检查文档的有效性
if (smxdFileName != null && mainMapControl.CheckMxFile(smxdFileName))
{
//如果是只读
if (pMapDocument.get_IsReadOnly(smxdFileName))
{
MessageBox.Show("该文档是只读文档!!!");
pMapDocument.Close();
return;
}
// 如果可以保存
else
{
SaveFileDialog pSaveFileDialog = new System.Windows.Forms.SaveFileDialog();
pSaveFileDialog.Title = "选择另存路径";
pSaveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
pSaveFileDialog.OverwritePrompt = true;
pSaveFileDialog.RestoreDirectory = true;
// 如果可以
if (pSaveFileDialog.ShowDialog() == DialogResult.OK)
{
smxdFileName = pSaveFileDialog.FileName;
}
// 如果失败
else
{
return;
}
//可以之后继续保存
pMapDocument.New(smxdFileName);
pMapDocument.ReplaceContents(mainMapControl.Map as IMxdContents);
pMapDocument.Save(true, true);
pMapDocument.Close();
MessageBox.Show("另存文档到" + smxdFileName + "成功!");
}
}
5.待续...
- 输出地图(这里真的想用python写脚本, engine这么这么复杂)
7.1. 选择要素
private void 选择工具ToolStripMenuItem_Click(object sender, EventArgs e)
{
mainMapControl.CurrentTool = null;
ICommand command = new ControlsSelectFeaturesToolClass();
command.OnCreate(mainMapControl.Object);
mainMapControl.CurrentTool = command as ITool;
mainMapControl.ActiveView.Refresh();
}
4.2 清除选择
private void 清空选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
IActiveView pActiveView = mainMapControl.ActiveView;
pActiveView.FocusMap.ClearSelection();
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, pActiveView.Extent);
}
7.3 缩放至选择
private void 缩放至选择ToolStripMenuItem_Click(object sender, EventArgs e)
{
ICommand command = new ControlsZoomToSelectedCommandClass();
command.OnCreate(mainMapControl.Object);
command.OnClick();
mainMapControl.ActiveView.Refresh();
}
- 查询
- 空间分析
- 数据视图与布局视图的同步
// 数据视图与布局视图同步
private void mainMapControl_OnAfterScreenDraw(object sender, IMapControlEvents2_OnAfterScreenDrawEvent e)
{
IActiveView pActiveView = (IActiveView)mainPageLayoutControl1.ActiveView.FocusMap;
IDisplayTransformation displayTransformation = pActiveView.ScreenDisplay.DisplayTransformation;
displayTransformation.VisibleBounds = mainMapControl.Extent;
mainPageLayoutControl1.ActiveView.Refresh();
CopyToPageLayout(); // 调用下面的函数
}
// CopyToPageLayout() 布局视图与数据视图同步
private void CopyToPageLayout()
{
IObjectCopy pObjectCopy = new ObjectCopyClass();
object copyFromMap = mainMapControl.Map;
object copiedMap = pObjectCopy.Copy(copyFromMap); // 复制地图到copiedMap中
object copyToMap = mainPageLayoutControl1.ActiveView.FocusMap;
pObjectCopy.Overwrite(copiedMap, ref copyToMap); // 复制地图
mainPageLayoutControl1.ActiveView.Refresh();
}
11.状态栏显示坐标
// 可以设置坐标的显示
private void mainMapControl_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
// 显示当前比例尺
XYSysStripStatusLabel1.Text = " 比例尺 1:" + ((long)this.mainMapControl.MapScale).ToString();
XYStripStatusLabel2.Text = " 当前坐标 X = " + e.mapX.ToString() + " Y = " + e.mapY.ToString() + " " + this.mainMapControl.MapUnits;
}
//当前坐标的后面的坐标单位为“ esriUnknownUnits”或“ esriMeters ”之类,即系统在正常单位的前面加上了“ esri ”,追求完美的我们自然看得不舒服。那就进行简单的替换吧。
//首先定义个全局坐标单位变量 sMapUnits,再 Form1_Load 函数中进行初始化: 添加 axMapControl1 控件的 OnMapReplaced 事件,在事件响应函数中进行坐标单位替换
private void mainMapControl_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
esriUnits mapUnits = mainMapControl.MapUnits;
switch (mapUnits)
{
case esriUnits.esriCentimeters:
sMapUnits = "Centimeters";
break;
case esriUnits.esriDecimalDegrees:
sMapUnits = "Decimal Degrees";
break;
case esriUnits.esriDecimeters:
sMapUnits = "Decimeters";
break;
case esriUnits.esriFeet:
sMapUnits = "Feet";
break;
case esriUnits.esriInches:
sMapUnits = "Inches";
break;
case esriUnits.esriKilometers:
sMapUnits = "Kilometers";
break;
case esriUnits.esriMeters:
sMapUnits = "Meters";
break;
case esriUnits.esriMiles:
sMapUnits = "Miles";
break;
case esriUnits.esriMillimeters:
sMapUnits = "Millimeters";
break;
case esriUnits.esriNauticalMiles:
sMapUnits = "NauticalMiles";
break;
case esriUnits.esriPoints:
sMapUnits = "Points";
break;
case esriUnits.esriUnknownUnits:
sMapUnits = "Unknown";
break;
case esriUnits.esriYards:
sMapUnits = "Yards";
break;
}
}
网友评论