Python访问ArcObjects

本文介绍了如何使用Python脚本导入ArcGIS模块,包括获取库路径、导入常用模块及初始化许可证状态等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
# -*- 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.youkuaiyun.com/warrenwyf/article/details/6040311

   

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值