ArcGIS 10 之後,Python 在 ArcGIS 自動化中的角色越來越重要。若要在 Python 中直接操作 ArcObjects,需要透過 comtypes 載入 COM 組件。
comtypes 可從 https://sourceforge.net/projects/comtypes/ 取得。安裝後,就能用 GetModule 載入 olb 檔。
from comtypes.client import GetModule
GetModule('C:/Program Files (x86)/ArcGIS/Desktop10.0/com/esriGeometry.olb')
ArcObjects 多為 COM 物件,建立時需指定 class 與 interface。若取得 IWorkspace,卻需要 IFeatureWorkspace,可用 QueryInterface 轉換。
def AoObj(MyClass, MyInterface):
from comtypes.client import CreateObject
return CreateObject(MyClass, interface=MyInterface)
def AoCType(obj, interface):
return obj.QueryInterface(interface)
透過這兩個函式,加上載入模組的 GetAoModule,就能在 Python 中查詢 ArcSDE 要素、開啟 FeatureClass、讀取 ObjectID 等。