Calling ao in Python

Source: Internet
Author: User

 

Use ArcObjects objects in Python

[Environment] ArcGIS 10, Python 2.6

After ArcGIS 10, VBA will exit the ArcGIS product. It can be imagined that the position of Python in the ArcGIS product will be very important in the future. Although Python already exists in ArcGIS for a long time, it is usually possible to export the model as a Python script, it is then used to exchange with others or use the scheduled tasks of the operating system for other methods.ProgramCall. This article describes how to write a Python script and use the ArcObjects object in the script to give full play to the characteristics of the python glue language, construct an interactive bridge between other programs and ArcGIS.

We all know that ArcObjects in ArcGIS is a COM Object. To call a COM Object in Python, we need an open-source Class Library: comtypes. We can download and install it from SourceForge:

Http://sourceforge.net/projects/comtypes/

The installation interface in Windows looks like this:

 

After this step, all the calls to ArcGIS will be handled in the python environment. Let's first look at how to load the ArcObjects Component Library in Python.

I have written a method to easily load the olb file of ArcGIS:

Def getaomodule (modulename ):
Import comtypes
From comtypes. Client import getmodule
Getmodule ('C:/program files (x86)/ArcGIS/topics top10.0/COM/'+ modulename)

In this way, to use the geometric objects in AO, you only needCodeLoad the esrigeometry Object Library: getaomodule ('esrigeometry. olb') Here we can see the comtypes we just installed. We can use it to load the ArcGIS Object Library. Similarly, here we also need to rely on comtypes to help us create the ArcObjects object (COM Object). For convenience, I have also defined a method:

def aoobj (myclass, myinterface):
from comtypes. client import Createobject
try:
OBJ = Createobject (myclass, interface = myinterface)
return OBJ
skip T:
return none, if I want to create an ArcObjects object in Python, such as a point, we can use the following method: getaomodule ('esrigeometry. olb')
Import comtypes. gen. esrisystem as esrisystem
Pt = aoobj (esrisystem. point, esrisystem. ipoint) Now, we can create an ArcObjects object. However Another important problem that needs to be solved is how to convert the object interface type. For example, if I get an iworkspace interface of an object, but I need to call the ifeatureworkspace interface method of this object, a conversion is required at this time, in python, I have prepared another method to do this:

Def aoctype (OBJ, interface ):
Try:
Newobj = obj. QueryInterface (Interface)
Return newobj
Except t:
Return none. If I want to convert the object of an iworkspace interface to the ifeatureworkspace interface, we can perform this operation: getaomodule ('esrigeodatabase. olb ')
Import comtypes. gen. esrigeodatabase as esrigeodatabase
FW = aoctype (W, esrigeodatabase. ifeatureworkspace) through the getaomodule, aoobj, and aoctype methods above, we can easily use the ArcObjects object. Next, let's use an example of querying elements from the ArcSDE database to familiarize ourselves with the use of ArcObjects objects in Python:

Getaomodule ('esrisystem. olb ')
Getaomodule ('esrisponcesgdb. olb ')
Getaomodule ('esrigeodatabase. olb ')

Import comtypes. gen. esrisystem as esrisystem
Aoinit = aoobj (esrisystem. aoinitialize, esrisystem. iaoinitialize)
Pcode = esrisystem. esrilicenseproductcodearcinfo
Status = aoinit. isproductcodeavailable (pcode)
If status = esrisystem. esrilicenseavailable:
Aoinit. initialize (pcode)
Props = aoobj (esrisystem. propertyset, esrisystem. ipropertyset)
Props. setproperty ("server", "localhost ")
Props. setproperty ("instance", "5151 ")
Props. setproperty ("user", "SDE ")
Props. setproperty ("password", "SDE ")
Props. setproperty ("version", "SDE. Default ")
Import comtypes. gen. esrisponcesgdb as esrisponcesgdb
Import comtypes. gen. esrigeodatabase as esrigeodatabase
WF = aoobj (esridatasourcesgdb. sdeworkspacefactory, esrigeodatabase. iworkspacefactory)
W = WF. Open (props, false)
FW = aoctype (W, esrigeodatabase. ifeatureworkspace)
Fc = fw. openfeatureclass ("SDE. Sde. Cities ")
C = FC. Search (none, false)
F = C. nextfeature ()
While (F! = None ):
Print (STR (F. objectid ))
F = C. nextfeature ()

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/warrenwyf/archive/2010/11/28/6040311.aspx

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.