Thursday, February 4, 2016

Setting up a Python development environment for ArcGIS


I use Intellij IDEA 15.0.3 with its Python plugin to development spatial analysis platforms using ESRI ArcGIS libraries. Although the ESRI licenses are not free, I have found them to be good investments given the ease of performing spatial analysis and managing GIS datasets. For this tutorial, I will use PyCharm Community Edition instead of the full-fledged Intellij IDEA. You will need to have ArcGIS installed already. If you wish, you can also install the separate 64-bit geoprocessing package. ArcGIS 10.3 uses Python 2.7.
  1. Download PyCharm IDE.
  2. Install PyCharm IDE.
  3. Start PyCharm IDE.
  4. Create New Project. If you have existing interpreters, choose the one that was installed by ArcGIS.
  5. Open File-Settings (or Ctrl+Alt+S).
  6. Click on Project Interpreter.
  7. Click on the gear icon (near the top right corner of the window).
  8. Select "Create VirtualEnv". This will give you a separate Python environment for each project.
  9. Give the virtual environment a name and location.
  10. Check "Inherit global site-packages".
  11. Click OK to clear the Settings windows.
  12. Go to the Project window.
  13. Right-click on "MyFirstArcpyProject".
  14. Select New-Python File.
  15. Call the new Python file "ExtractSpatialData.py".
  16. Go to your ArcMap or ArcCatalog's geoprocessing results frame.
  17. Right-click on the operation that you want to reuse.
  18. Click on "Copy As Python Snippet".
  19. Go to the PyCharm window.
  20. Paste the clipboard contents in ExtractSpatialData.py.
  21. Go to the top of the file.
  22. Add the line "import arcpy".
  23. Add the line "arcpy.env.workspace = <path to your datasets>".
  24. Run ExtractSpatialData.py (Ctrl+Shift+F10).
Instead of setting arcpy.env.workspace, you can also use the full path name to your input, clip, and output feature classes. If you are using extensions such as spatial analysis, you will need to checkout the license, e.g. arcpy.CheckOutExtension("Spatial"). See checkoutextension for more details.

No comments:

Post a Comment