此实例是以ArcEngine为平台,在Visual C#的集成开发环境下进行开发的一个简单实例。目的是让大家初步了解二次开发的大致过程。此实例要求达到的功能是:搭建系统的基本框架,实现地图控件(MapControl)、工具栏控件(ToolbarControl)、图层管理控件(TocControl)之间的交互操作,同时实现地图加载、全屏显示、放大、缩小、漫游等基本的GIS功能。
2.1 代码展示
2.1.1 主应用程序
namespace ZY
{
static class Program
{
private static LicenseInitializer m_AOLicenseInitializer = new ZY.LicenseInitializer();
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
//ESRI License Initializer generated code.
m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine },
new esriLicenseExtensionCode[] { });
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
//ESRI License Initializer generated code.
//Do not make any call to ArcObjects after ShutDownApplication()
m_AOLicenseInitializer.ShutdownApplication();
}
}
}
2.1.2 引用arcengine组件库
2.1.3 实现打开shp文件的功能,添加openFileDialog1控件
private void menuAddShp_Click(object sender, EventArgs e)
{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
//添加文件过滤器选择后缀.shp
openFileDialog1.Filter = "ShapeFile文件(*.shp)|*.shp";
//设置文件对话框的初始路径
openFileDialog1.InitialDirectory = @"C:\Program Files (x86)\ArcGIS\DeveloperKit10.2\Samples\data\World";
//示例数据文件夹
openFileDialog1.Multiselect = false;
DialogResult pDialogResult = openFileDialog1.ShowDialog();
if (pDialogResult != DialogResult.OK)
return;
//获取文件名与路径
string pPath = openFileDialog1.FileName;
string pFolder = System.IO.Path.GetDirectoryName(pPath);
string pFileName = System.IO.Path.GetFileName(pPath);
IWorkspace PWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory as IFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName);
IFeatureLayer pFLayer = new FeatureLayerClass();
pFLayer.FeatureClass = pFC;
pFLayer.Name = pFC.AliasName;
ILayer pLayer = pFLayer as ILayer;
IMap pMap = axMapControl1.Map;
pMap.AddLayer(pLayer);
axMapControl1.ActiveView.Refresh();
/*IWorkspace pWorkspace1 = pWorkspaceFactory.OpenFromFile(@"C:\Program Files (x86)\ArcGIS\DeveloperKit10.2\Samples\data\World", 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace1 as IFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass("world30.shp");
IFeatureLayer pFLayer = new FeatureLayerClass();
pFLayer.FeatureClass = pFC;
pFLayer.Name = pFC.AliasName;
ILayer pLayer=pFLayer as */
}
2.1.4 实现打开地图文档mxd```
private void
```adMapDocument()
{
//利用System中OpenFileDialog方法,显示一个对话框,提示用户打开文件
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = m_Path;
//对话框结果不为OK不往下进行
DialogResult DR=openFileDialog.ShowDialog();
//设置对话框的名称
openFileDialog.Title = "打开地图文档";
//获取或设置当前文件名筛选器字符串,来决定打开文件的类型为*.Mxd
openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
//判断,如果对话框结果不为OK的话不继续往下进行
if (DR != DialogResult.OK)
return;
//获取文件的路径filePath以及文件名称
string filePath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
//定义axMapControl控制鼠标指针选项为沙漏光标
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
//传入LoadMxFlie方法的三个参数,filePath—文件路径;0—地图名称或索引;Type.Missing—通过反射进行调用获取参数的默认值
axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
//定义axMapControl控制鼠标指针为默认箭头
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
axMapControl1.Extent = axMapControl1.FullExtent;
}
else
{
MessageBox.Show(filePath + "不是有效的地图文档");
}
}
2.1.5 添加mdb数据
public void Open
WorkspaceFromFileAccess(string clsName, string DBPath)
{
//新建一个Access的工作空间工厂
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
//根据Access路径打开一个Access工作空间工厂,获得工作空间对象
IWorkspace workspace = workspaceFactory.OpenFromFile(DBPath, 0);
IFeatureWorkspace accessWorkspace = workspace as IFeatureWorkspace;
//打开图层名为clsName的数据集,获取其要素类对象FeatureClass
IFeatureClass tFeatureClass = accessWorkspace.OpenFeatureClass(clsName);
//实例化一个图层(IFeatureLayer)对象素类
//该对象类用于装载被打开的tFeatureClass,最后axMapControl控件上显示
IFeatureLayer pFtLayer =new FeatureLayerClass() ;
pFtLayer.FeatureClass= tFeatureClass ;
pFtLayer.Name = clsName;
axMapControl1.AddLayer(pFtLayer);
}
2.2 打开效果
2.2.1 系统界面
系统添加了打开,另存为,缩放,漫游,全图等控件。
2.2.2 读取自带world.lyr数据
2.2.3 系统2.0版本,加载mxd文档,shp文件,mdb空间数据文件
2.3 实现二次开发的图像绘制功能
2.3.1 打开美国地图mxd文件,漫游至加州
2.3.2 线段绘制实现代码
private void 线ToolStripMenuItem_Click(object sender, EventArgs e)
{
//设置枚举变量的绘制几何图形类型
oprFlag = Operation.ConstructionPolyLine;//对应上述所定义的枚举Operation
geoCollection = new PolylineClass();
ptCollection = new PolylineClass();
}
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
//类型声明
missing = Type.Missing;
//若为添加折线事件
if (oprFlag == Operation.ConstructionPolyLine)
{
//axMapControl1控件的当前控件为指针(null)
axMapControl1.CurrentTool = null;
//通过AddPoint方法从GetPoint函数中获取鼠标单击的坐标
ptCollection.AddPoint(GetPoint(e.mapX, e.mapY), ref missing, ref missing);
//定义几何类型绘制折线的方法
Geometry = axMapControl1.TrackLine();
//通过addFeature函数的两个参数,Highways—绘制折线的图层;Geometry—绘制的几何折线
AddFeature("Highways", Geometry);
//折线添加完成之后结束编辑状态
oprFlag = Operation.Nothing;
解读一些简单的Python代码
3.1 Demo1\04批量删除gdb中的数据
import arcpy;
from arcpy import env
env.workspace=r'C:\Users\yanrui\Documents\ArcGIS\Default.gdb'
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
arcpy.Delete_management(fc)
ArcPy 是一个以成功的 arcgisscripting 模块为基础并继承了 arcgisscripting 功能进而构建而成的站点包。目的是为以实用高效的方式通过 Python 执行地理数据分析、数据转换、数据管理和地图自动化创建基础。
3.2 使用 Python 内置的 len 函数来获取该数值
import arcpy
arcpy.env.workspace = "c:/ Shapefiles"
fcs = arcpy.ListFeatureClasses()
fcCount = len(fcs)
print fcCount
3.3 列出工作空间中所有以字母 G 开头的要素类
import arcpy
arcpy.env.workspace = "D:/ data.gdb"
fcs = arcpy.ListFeatureClasses("G*")
3.4 为文件夹内形式为标记图像文件格式 (TIFF) 图像的所有栅格创建栅格金字塔
arcpy.env.workspace= "D:/ images"
#
for tiff in arcpy.ListRasters("*", "TIF"):
# Create pyramids
#
arcpy.BuildPyramids_management(tiff)
3.5 Python插入多边形数据
import arcpy
f=open(r'C:\data.txt')
cursor=arcpy.InsertCursor(r'C:\Data\polygon.shp')
array=arcpy.Array()
point=arcpy.Point()
for line in f:
pt=line.split()
str = pt[3].split(';')
name = pt[1]
for j in str:
xy = j.split(',')
point.X = float(xy[0]);
point.Y = float(xy[1])
array.add(point)
row = cursor.newRow()
row.shape = array
row.name = name
array.removeAll()
cursor.insertRow(row)