Accessing ArcObjects in Python

If you have used arcpy in ArcGIS for some time, you might have noticed that not all of those operations that are accessible to you via the user interface in ArcMap are exposed as functions and methods in arcpy.

From the Esri help:

It was not designed to be a complete replacement for ArcObjects or an attempt at creating a function, method, or property for every conceivable button, dialog box, menu choice, or context item in the ArcMap interface (that is what ArcObjects provides).

So, there are no plans to make the whole ArcGIS platform available via arcpy which is why it is sometimes referred to as coarse-grained API into ArcGIS. For those situations when you need to have a finer control over the GIS data management and maintenance, Esri recommends using ArcObjects. It is usually related to advanced data management operations where support in arcpy is very limited such as LIDAR data management, network dataset generation, write-access to the properties of workspaces and data repositories, or metadata management. All of this can be done in ArcObjects, and hence, the name – fine-grained API into ArcGIS.

Here are just a few of the things you cannot do in arcpy.

  • You cannot create a new network dataset with arcpy and neither do you have any geoprocessing tools for that; this can be done solely by using the New Network Dataset wizard.
  • You cannot create a new empty ArcMap map document with arcpy. This means if your workflows rely on generating map documents and adding layers into it, you need to pre-create an empty map document which will be used as a template.
  • You cannot create new bookmarks in a map document and neither can you import existing bookmarks into a map document. This can be done manually from the ArcMap user interface only.

However, if you do need to automate some of those workflows, either for one-time job when you need to process some data really quickly or when building a script that will be run on a regular basis, the only option you have is to use ArcObjects.

Learning ArcObjects can be hard due to its complexity. You would also need to learn Java or .NET (C# or VB) if you want to write ArcGIS add-ins or develop stand-alone applications. If you are not comfortable with those languages and have most of the workflows written in Python, I have good news for you. It is possible to access ArcObjects from Python.

This means that you can write your Python code using arcpy and other packages and incorporate some of the ArcObjects-based operations right into your code. This comes very handy when you lack just some minor operations in arcpy and need to use ArcObjects without getting out of your existing Python code.

To get started, please review this GIS.SE post: How do I access ArcObjects from Python? It has enough information to let you set up everything needed.

Then follow these steps:

  1. Install comtypes package (I recommend using pip, see How to install pip on Windows? on how to get it)
  2. Download the snippets.py file to get examples and some helper functions.
  3. Change the “10.2” in the snippets file to “10.3” and installation paths of ArcGIS accordingly.
  4. You are ready to access the ArcObjects from your Python code! Look here for a sample that will create a new map document.

There is no need to install the ArcObjects SDK; the only thing you need to have installed is ArcGIS Desktop.

You will need to play around with ArcObjects reference to find out what assembly and what interface you need to import. Here is the place you can start exploring the object model diagrams. Here is the section I recommend reviewing, Learning ArcObjects. Skip those parts you find irrelevant for you, though, as it covers nearly all ArcGIS Desktop operations.

In the API Reference part of the Help, you will find detailed information about the namespaces used in ArcObjects. Reading through this and visiting this often is an excellent way to lean ArcObjects. Here is an example of the Carto namespace.

Even though you do not need to know C#, VB, or Java, it is still worth to be able to read the code, as there are tons of useful snippets and code samples available in the Help system. Those will help you find out what interface should be used, any data type casting needed, and many more.

To learn more about the ArcObjects, listen to a recorded live-training seminar from Esri. Please share in comments what kind of operations you are missing in arcpy and would need to use ArcObjects to implement them.

 

Advertisements

3 thoughts on “Accessing ArcObjects in Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s