using System;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
以上引用全部打勾–>确定
所有引用皆在属性表里修改“嵌入互操作类型”为False;
axTOCControl
、EagleEyeMapControl
、mainMapControl
、切换到布局视图再添加PageLayoutControl控件,命名为:axPageLayoutControl
;修改命名是为了抄写源代码与命名规范的需要;using ESRI.ArcGIS.esriSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MapOperation
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
//基本权限,一定要的
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine);
IAoInitialize m_aoinitialize = new AoInitializeClass();
m_aoinitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
//扩展权限
m_aoinitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
// LicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
}
#region 封装的方法
///
/// 加载工作空间里面的要素和栅格数据
///
///
private void AddAllDataset(IWorkspace pWorkspace, AxMapControl mapControl)
{
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
pEnumDataset.Reset();
//将Enum数据集中的数据一个个读到DataSet中
IDataset pDataset = pEnumDataset.Next();
//判断数据集是否有数据
while (pDataset != null)
{
if (pDataset is IFeatureDataset) //要素数据集
{
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
pEnumDataset1.Reset();
IGroupLayer pGroupLayer = new GroupLayerClass();
pGroupLayer.Name = pFeatureDataset.Name;
IDataset pDataset1 = pEnumDataset1.Next();
while (pDataset1 != null)
{
if (pDataset1 is IFeatureClass) //要素类
{
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
if (pFeatureLayer.FeatureClass != null)
{
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
pGroupLayer.Add(pFeatureLayer);
mapControl.Map.AddLayer(pFeatureLayer);
}
}
pDataset1 = pEnumDataset1.Next();
}
}
else if (pDataset is IFeatureClass) //要素类
{
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
mapControl.Map.AddLayer(pFeatureLayer);
}
else if (pDataset is IRasterDataset) //栅格数据集
{
IRasterWorkspaceEx pRasterWorkspace = (IRasterWorkspaceEx)pWorkspace;
IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(pDataset.Name);
//影像金字塔判断与创建
IRasterPyramid3 pRasPyrmid;
pRasPyrmid = pRasterDataset as IRasterPyramid3;
if (pRasPyrmid != null)
{
if (!(pRasPyrmid.Present))
{
pRasPyrmid.Create(); //创建金字塔
}
}
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromDataset(pRasterDataset);
ILayer pLayer = pRasterLayer as ILayer;
mapControl.AddLayer(pLayer, 0);
}
pDataset = pEnumDataset.Next();
}
mapControl.ActiveView.Refresh();
//同步鹰眼
SynchronizeEagleEye();
}
private void ClearAllData()
{
if (mainMapControl.Map != null && mainMapControl.Map.LayerCount > 0)
{
//新建mainMapControl中Map
IMap dataMap = new MapClass();
dataMap.Name = "Map";
mainMapControl.DocumentFilename = string.Empty;
mainMapControl.Map = dataMap;
//新建EagleEyeMapControl中Map
IMap eagleEyeMap = new MapClass();
eagleEyeMap.Name = "eagleEyeMap";
EagleEyeMapControl.DocumentFilename = string.Empty;
EagleEyeMapControl.Map = eagleEyeMap;
}
}
///
/// 获取RGB颜色
///
/// 红
/// 绿
/// 蓝
///
private IRgbColor GetRgbColor(int intR, int intG, int intB)
{
IRgbColor pRgbColor = null;
if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
{
return pRgbColor;
}
pRgbColor = new RgbColorClass();
pRgbColor.Red = intR;
pRgbColor.Green = intG;
pRgbColor.Blue = intB;
return pRgbColor;
}
///
/// 获取地图单位
///
///
///
private string GetMapUnit(esriUnits _esriMapUnit)
{
string sMapUnits = string.Empty;
switch (_esriMapUnit)
{
case esriUnits.esriCentimeters:
sMapUnits = "厘米";
break;
case esriUnits.esriDecimalDegrees:
sMapUnits = "十进制";
break;
case esriUnits.esriDecimeters:
sMapUnits = "分米";
break;
case esriUnits.esriFeet:
sMapUnits = "尺";
break;
case esriUnits.esriInches:
sMapUnits = "英寸";
break;
case esriUnits.esriKilometers:
sMapUnits = "千米";
break;
case esriUnits.esriMeters:
sMapUnits = "米";
break;
case esriUnits.esriMiles:
sMapUnits = "英里";
break;
case esriUnits.esriMillimeters:
sMapUnits = "毫米";
break;
case esriUnits.esriNauticalMiles:
sMapUnits = "海里";
break;
case esriUnits.esriPoints:
sMapUnits = "点";
break;
case esriUnits.esriUnitsLast:
sMapUnits = "UnitsLast";
break;
case esriUnits.esriUnknownUnits:
sMapUnits = "未知单位";
break;
case esriUnits.esriYards:
sMapUnits = "码";
break;
default:
break;
}
return sMapUnits;
}
///
/// 绘制多边形
///
///
///
public IPolygon DrawPolygon(AxMapControl mapCtrl)
{
IGeometry pGeometry = null;
if (mapCtrl == null) return null;
IRubberBand rb = new RubberPolygonClass();
pGeometry = rb.TrackNew(mapCtrl.ActiveView.ScreenDisplay, null);
return pGeometry as IPolygon;
}
#endregion
#region LoadMxFile方法加载地图文档文件
private void btnLoadMxFile_Click(object sender, EventArgs e)
{
//加载数据前如果有数据则清空
try
{
OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.CheckFileExists = true;
pOpenFileDialog.Title = "打开地图文档";
pOpenFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt|发布地图文件(*.pmf)|*.pmf|所有地图格式(*.mxd;*.mxt;*.pmf)|*.mxd;*.mxt;*.pmf";
pOpenFileDialog.Multiselect = false; //不允许多个文件同时选择
pOpenFileDialog.RestoreDirectory = true; //存储打开的文件路径
if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
{
string pFileName = pOpenFileDialog.FileName;
if (pFileName == "")
{
return;
}
if (mainMapControl.CheckMxFile(pFileName)) //检查地图文档有效性
{
ClearAllData();
mainMapControl.LoadMxFile(pFileName);
}
else
{
MessageBox.Show(pFileName + "是无效的地图文档!", "信息提示");
return;
}
}
}
catch (Exception ex)
{
MessageBox.Show("打开地图文档失败" + ex.Message);
}
}
#endregion
注意添加try 语句,以防该操作失败致使程序暂停运行;
然后依次点击控件按钮,操作其余步骤*******
(所有代码书上源码包中)
小编匆促的写了这个技术文档,有问题不许提(呵呵),本书叫ArcGIS Engine,ArcGIS 安装有问题的先安装软件(不装Engine的,没认证权限的等)。