Tuesday, June 25, 2013

Using ArcPy with Visual Studio 2010 and 2012


ArcPy has been commonly used by the GIS community with PythonWin. This environment can be useful for certain scenarios but is not ideal for heavy implementations. An alternative consists in using Visual Studio as a development platform and to use PyTools for exposing the power of the Visual Studio to the Python developers. Some of the advantages are:
  • Powerful IDE known by the developers community
  • Better intelliSense and debugging tools
  • Integration with Source Safe
  • Debugging and profiling capabilities
  • Better Performance
  • Ability to build robust and complex applications
Below are provided some basic steps for enabling this integration. The example is quite simple for easy understanding. Other ArcPy functions can be used as required following the same design principle.

1 – Confirm that CPython is installed (comes with ArcGIS)

2 – Install PyTools for Visual Studio.
2.1 – Visual Studio 2010: https://pytools.codeplex.com/downloads/get/523959
2.2 – Visual Studio 2012: https://pytools.codeplex.com/downloads/get/634989 (2.0 Alpha) or https://pytools.codeplex.com/downloads/get/523956 (1.5.1 RTM)
Further details in: https://pytools.codeplex.com/wikipage?title=PTVS%20Installation
3 – Open Visual Studio (e.g. 2012) and create a new project:
image

4 – In the “Other Languages” tab select the option Python as we are using the CPython interpreter and set the properties has shown below (change the solution and project name accordingly) and click ok to confirm.
image

5 – Visual Studio will create the solution and project including the PythonExample.py file.
image

6 – In the Solution Explorer window right-click the PythonExample project and create a new folder.
image
 
7 – Name the folder "Framework". This folder can host generic classes, utilities, wrappers and functions for desktop, server and cloud that that can be reused in any project. Right-click the folder, click on Add -- > New Item and create a new python class with the name Workspace.py.
image image

8 – To change a bit the implementation of the class define it using a singleton pattern and set the content as follows:
image # -*- coding: utf-8 -*-
# *************** All rights reserved. Copyright (c) 2013 ****************
#
# Author: Jose Sousa
# Date: 25/06/2013
# Functionality: Provides a Python utility for working with an Esri workspace
#
# ****************************************************************************************
import arcpy, os, sys
class Workspace(object):
    """Helper for working with an Esri Workspace"""
    _instance = None
    def __init__( self ) :
        if self._instance is not None :
            raise RuntimeError('Only one instance of Workspace is allowed!')
    @classmethod   
    def  Instance(cls) :
        if cls._instance is None :
            cls._instance = Workspace()
        return cls._instance
    def SetWorkspace(self, workspace) :
        arcpy.env.workspace = workspace
 
9 – In the Solution Explorer window double-click the PythonExample.py file to edit its contents. Type the code below and note that the intelliSense will appear automatically to assist with the implementation. Note that the first lines of code are specifying the location where the modules required by this class are placed. Python will compile all the libraries at run-time,  from each of these locations referenced in this python file.
image image
 
10 – Set the workspace used by ArcPy by calling the SetWorkspace method provided by the Workspace class.
image import os, sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Framework"))
from Workspace import Workspace
Workspace.Instance().SetWorkspace("C:/data/base.gdb")

 
11 – Set a breakpoint on the Workspace.Instance().SetWorkspace line by clicking on left side of the vertical green color or by clicking on F9. Build the solution and click on F5 to run and debug the application (you can minimize the command prompt window that will appear). Visual Studio will launch the application and stop on the line with the breakpoint.
image
 
12 – On the Immediate Window inspect the code above (e.g. copy the os.path.dirname(os.path.abspath(__file__)) and paste it on the immediate window clicking enter to confirm).  The immediate window will provide the output produced by that code.
image
 
13 – Use a different technique to inspect the code above by selecting the entire line and right-clicking and selecting QuickWatch… Note that the Quick Watch window will also output the result. This window is ideal for performing additional queries.
image image
 
14 – Debug into the SetWorkspace method by clicking on F11. Note that the variable will be passed throughout the Workspace class and that the singleton (Instance) will initialize the class for the first time. There will only be one instance of this class throughout all the lifecycle of the application. Continue clicking on F11 until it reaches the SetWorkspace method (note that you can place a breakpoint anywhere inside of the method and click on F5). Note that just by placing the mouse cursor on top of the workspace variable it will display its value.
image
 
15 – Note that we can now extend the application further to include Toolboxes for desktop and server and so on. The framework API can also be extended to wrap most of the ArcPy functions and to provide other custom functionality.

Conclusion:
 
ArcPy can be used inside Visual Studio and developers can take advantage of the IDE to fully build more complex applications. Typical use cases would be to invoke directly dll libraries through command line or calling ArcObjects using C++ for better performance. Other approaches can also be used such as communicating via web services for better interoperability including with the Esri Server Object Extensions (SOEs) or any other 3rd party web services. SOEs can expose ArcObjects via web services that can augment the default ArcGIS for Server services capabilities. Your Python application can take advantage of that.
 
This integration has been tested in ArcGIS for Desktop 10, 10.1 and the upcoming 10.2 release.

6 comments:

  1. Hi,
    I'm trying to use IronPython to call a function in a python script (which uses arcpy) and get its return value, but I'm not sure if your approach is what I'm looking for. Any guidance would be highly appreciated.

    Thanks in advance.

    ReplyDelete
  2. This post, and whole blog, is really useful! Thanks a lot!

    ReplyDelete
  3. hola jose, como puedo integrar visual studio 2013 con Arcpy?

    quiero realizar scripts en visual studios y que se ejecuten en Arcgis, es posible?

    saludos

    ReplyDelete
    Replies
    1. Hi Gato,

      You can use arcpy with Visual Studio 2013 or 2015 by installing the appropriate version of PyTools from github:

      https://github.com/Microsoft/PTVS/releases/tag/v2.2.2

      Cheers,
      Jose

      Delete
  4. my environment work space is not being recognized. Does\will your code address this problem?
    GDBPathName = r'B:\projects\VS_ArcGISProjects\Data\HLZ1.gdb'
    arcpy.env.workspace = GDBPathName
    arcpy.env.scratchWorkspace = GDBPathName

    ReplyDelete