AE10.2與C#2012開發
阿新 • • 發佈:2018-11-26
此例項是以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();
/// <summary>
/// 應用程式的主入口點。
/// </summary>
[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)