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
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/5239593 – Open Visual Studio (e.g. 2012) and create a new project:
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
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.
5 – Visual Studio will create the solution and project including the PythonExample.py file.
6 – In the Solution Explorer window right-click the PythonExample project and create a new folder.
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.
8 – To change a bit the implementation of the class define it using a singleton pattern and set the content as follows:
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.
10 – Set the workspace used by ArcPy by calling the SetWorkspace method provided by the Workspace class.
|import os, sys|
from Workspace import Workspace
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.
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.
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.
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.
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.
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.