# -*- coding:UTF-8 -*-
# 读取与导入ArcObjects模块
def GetLibPath():
"""Return location ArcGIS type libraries as string"""
#This will still work on 64-bit machines because Python runs in 32 bit mode
import _winreg
keyESRI=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\\ESRI\\Desktop10.0")
return _winreg.QueryValueEx(keyESRI,"InstallDir")[0]+"com\\" #u'C:\\Program Files\\ArcGIS\\Desktop10.0\\com\\'
def GetModule(sModuleName):
"""Import ArcGIS module"""
from comtypes.client import GetModule
sLibPath=GetLibPath()
GetModule(sLibPath+sModuleName)
def GetStandaloneModules():
""""Import commonly used ArcGIS libraries for standalone scripts"""
GetModule("esriSystem.olb")
GetModule("esriGeometry.olb")
GetModule("esriCarto.olb")
GetModule("esriDisplay.olb")
GetModule("esriGeoDatabase.olb")
GetModule("esriDataSourcesGDB.olb")
GetModule("esriDataSourcesFile.olb")
GetModule("esriOutput.olb")
#调用GetStandaloneModules执行模块的一次性导入
# 以后调用只要 import comtypes.gen.esriGeometry as esriGeometry 这种方式导入模块即可
GetStandaloneModules()
#绑定产品注册ArcGIS Version的类库文件(C:\Program Files\Common Files\ArcGIS\bin\ArcGISVersion.dll)
from comtypes.client import GetModule
agsversion=comtypes.GUID("{6FCCEDE0-179D-4D12-B586-58C88D26CA78}")
GetModule((agsversion,1,0))
导入前后如下目录如图示:
# 创建与转换对象类型
def NewObj(MyClass,MyInterface):
from comtypes.client import CreateObject
try:
ptr=CreateObject(MyClass,interface=MyInterface)
return ptr
except:
return None
def CType(obj,interface):
try:
newobj=obj.QueryInterface(interface)
return newobj
except:
return None
def CLSID(MyClass):
return str(MyClass._reg_clsid)
# 注册运行许可
def InitLicenseState():
import comtypes.gen.ArcGISVersionLib as esriVersion
import comtypes.gen.esriSystem as esriSystem
pVM=NewObj(esriVersion.VersionManager,esriVersion.IArcGISVersion)
if not pVM.LoadVersion(esriVersion.esriArcGISDesktop,"10.0"):
return False
pInit=NewObj(esriSystem.AoInitialize,esriSystem.IAoInitialize)
ProductList=[esriSystem.esriLicenseProductCodeArcInfo,\
esriSystem.esriLicenseProductCodeArcEditor, \
esriSystem.esriLicenseProductCodeArcView]
for eProduct in ProductList:
licenseStatus=pInit.IsProductCodeAvailable(eProduct)
if licenseStatus!=esriSystem.esriLicenseAvailable:
continue
licenseStatus=pInit.Initialize(eProduct)
return (licenseStatus==esriSystem.esriLicenseCheckedOut)
return False
ESRI的支持人员博客
http://blog.csdn.net/warrenwyf/article/details/6040311