1. AE二次开发——地图的基本操作(加载地图文档,加载shape,加载mdb,地图的保存,缩放,漫游)
1. 加载数据Icommand方法
ICommand Butdata = new ControlsAddDataCommandClass();
Butdata.OnCreate(axMapControl1.Object);
Butdata.OnClick();
axMapControl1.CurrentTool = Butdata as ITool;
2.加载mxd地图文档
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "地图文档(*.mxd)|*.Mxd|地图模板(*.mxt)|*.Mxt";
openFileDialog.Multiselect = false;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string sFileName = openFileDialog.FileName;
axMapControl1.LoadMxFile(sFileName);
}
3.加载shape
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.Title = "打开shp";
openFileDialog.Filter = "Shapefile(*.shp)|*.shp";
openFileDialog.Multiselect = true;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
FileInfo fileinfo = new FileInfo(openFileDialog.FileName);
string path = fileinfo.Directory.ToString();
string filename = fileinfo.Name.Substring(0, fileinfo.Name.IndexOf("."));
try
{
axMapControl1.AddShapeFile(path, filename);
}
catch (Exception ce)
{
MessageBox.Show("添加失败!" + ce.ToString());
}
}
3.加载mdb
为完成
4.保存
try
{
string sMxdFileName = axMapControl1.DocumentFilename;
IMapDocument pMapDocument = new MapDocumentClass();
if (sMxdFileName != null && axMapControl1.CheckMxFile(sMxdFileName))
{
if (pMapDocument.get_IsReadOnly(sMxdFileName))
{
MessageBox.Show("本地图文档是只读,不能保存");
pMapDocument.Close();
return;
}
}
else
{
SaveFileDialog pSaveFileDialog = new SaveFileDialog();
pSaveFileDialog.Title = "选择保存路径";
pSaveFileDialog.OverwritePrompt = true;
pSaveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
pSaveFileDialog.RestoreDirectory = true;
if (pSaveFileDialog.ShowDialog() == DialogResult.OK)
{
sMxdFileName = pSaveFileDialog.FileName;
}
else
{
return;
}
}
pMapDocument.New(sMxdFileName);
pMapDocument.ReplaceContents(axMapControl1.Map as IMxdContents);
pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
pMapDocument.Close();
MessageBox.Show("保存文档成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
5.另存为
try
{
SaveFileDialog psaveFileDialog = new SaveFileDialog();
psaveFileDialog.Title = "另存为";
psaveFileDialog.OverwritePrompt = true;
psaveFileDialog.Filter = "ArcMap文档(*mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
psaveFileDialog.RestoreDirectory = true;
if (psaveFileDialog.ShowDialog() == DialogResult.OK)
{
string sFilePath = psaveFileDialog.FileName;
IMapDocument pMapDocument = new MapDocumentClass();
pMapDocument.New(sFilePath);
pMapDocument.ReplaceContents(axMapControl1.Map as IMxdContents);
pMapDocument.Save(true, true);
pMapDocument.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
6.地图缩放(放大,缩小)
ICommand Zoomin = new ControlsMapZoomInToolClass();
Zoomin.OnCreate(axMapControl1.Object);
Zoomin.OnClick();
axMapControl1.CurrentTool = (ITool)Zoomin;
ICommand Zoomout = new ControlsMapZoomOutToolClass();
Zoomout.OnCreate(axMapControl1.Object);
Zoomout.OnClick();
axMapControl1.CurrentTool = (ITool)Zoomout;
7.地图漫游
ICommand Pan = new ControlsMapPanToolClass();
Pan.OnCreate(axMapControl1.Object);
Pan.OnClick();
axMapControl1.CurrentTool = (ITool)Pan;
8.全图显示
ICommand Full = new ControlsMapFullExtentCommandClass();
Full.OnCreate(axMapControl1.Object);
Full.OnClick();
9.属性查询
ICommand Identify = new ControlsMapIdentifyToolClass();
Identify.OnCreate(axMapControl1.Object);
Identify.OnClick();
axMapControl1.CurrentTool = (ITool)Identify;