├── .gitignore ├── ExampleData ├── points.dbf ├── points.prj ├── points.shp └── points.shx ├── README.md ├── Tips ├── call_from_command_line.md ├── call_from_command_line.py └── install_pip.cmd └── Tricks ├── create_shp_for_debugging.py └── cursors_fast_reproject.py /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | *.publishproj 131 | 132 | # NuGet Packages Directory 133 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 134 | #packages/ 135 | 136 | # Windows Azure Build Output 137 | csx 138 | *.build.csdef 139 | 140 | # Windows Store app package directory 141 | AppPackages/ 142 | 143 | # Others 144 | sql/ 145 | *.Cache 146 | ClientBin/ 147 | [Ss]tyle[Cc]op.* 148 | ~$* 149 | *~ 150 | *.dbmdl 151 | *.[Pp]ublish.xml 152 | *.pfx 153 | *.publishsettings 154 | 155 | # RIA/Silverlight projects 156 | Generated_Code/ 157 | 158 | # Backup & report files from converting an old project file to a newer 159 | # Visual Studio version. Backup files are not needed, because we have git ;-) 160 | _UpgradeReport_Files/ 161 | Backup*/ 162 | UpgradeLog*.XML 163 | UpgradeLog*.htm 164 | 165 | # SQL Server files 166 | App_Data/*.mdf 167 | App_Data/*.ldf 168 | 169 | ############# 170 | ## Windows detritus 171 | ############# 172 | 173 | # Windows image file caches 174 | Thumbs.db 175 | ehthumbs.db 176 | 177 | # Folder config file 178 | Desktop.ini 179 | 180 | # Recycle Bin used on file shares 181 | $RECYCLE.BIN/ 182 | 183 | # Mac crap 184 | .DS_Store 185 | 186 | 187 | ############# 188 | ## Python 189 | ############# 190 | 191 | *.py[cod] 192 | 193 | # Packages 194 | *.egg 195 | *.egg-info 196 | dist/ 197 | build/ 198 | eggs/ 199 | parts/ 200 | var/ 201 | sdist/ 202 | develop-eggs/ 203 | .installed.cfg 204 | 205 | # Installer logs 206 | pip-log.txt 207 | 208 | # Unit test / coverage reports 209 | .coverage 210 | .tox 211 | 212 | #Translations 213 | *.mo 214 | 215 | #Mr Developer 216 | .mr.developer.cfg 217 | 218 | #PyCharm 219 | .idea/ 220 | -------------------------------------------------------------------------------- /ExampleData/points.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clj2289/ArcPy-Tips/595877952fcc6c1e5cf58593c47af4048163a3ed/ExampleData/points.dbf -------------------------------------------------------------------------------- /ExampleData/points.prj: -------------------------------------------------------------------------------- 1 | PROJCS["Albers Conical Equal Area [Florida Geographic Data Library]",GEOGCS["GCS_North_American_1983_HARN",DATUM["D_North_American_1983_HARN",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Albers"],PARAMETER["False_Easting",400000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-84.0],PARAMETER["Standard_Parallel_1",24.0],PARAMETER["Standard_Parallel_2",31.5],PARAMETER["Central_Parallel",24.0],UNIT["Meter",1.0]] -------------------------------------------------------------------------------- /ExampleData/points.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clj2289/ArcPy-Tips/595877952fcc6c1e5cf58593c47af4048163a3ed/ExampleData/points.shp -------------------------------------------------------------------------------- /ExampleData/points.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clj2289/ArcPy-Tips/595877952fcc6c1e5cf58593c47af4048163a3ed/ExampleData/points.shx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ArcPy-Tips 2 | ========== 3 | 4 | ArcPy tips, tricks, best management practices and most of all...hacks! 5 | 6 | ## Best Management Practices 7 | * Use [data access cursors (arcpy.da.*)](http://resources.arcgis.com/en/help/main/10.2/index.html#/What_is_the_data_access_module/018w00000008000000/) when possible. Avoid using [legacy arcpy cursors](http://resources.arcgis.com/en/help/main/10.2/index.html#//018v0000002z000000) at all cost. 8 | * Always use [a scratch workspace](http://resources.arcgis.com/en/help/main/10.2/index.html#//001w00000003000000) if you _have_ to write data to disk 9 | * Turn off [archiving](http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000018t000000) unless its required 10 | * Use ["with" keyword](https://docs.python.org/2/reference/compound_stmts.html#with) when possible when working with files, cursors, database connections and etc 11 | 12 | ## Tips 13 | * [Call your python scripts from the command line and sent in parameters](Tips/call_from_command_line.md) 14 | * Use the built in python profiler to make your code faster (python -m cProfile -s tottime your.py cities.shp) 15 | * Always use in_memory feature classes when possible for high performance 16 | * Use list comprehension 17 | * [Install pip on Windows](Tips/install_pip.cmd) 18 | * Working with and installing python libraries for 64 Bit python i.e. c:\python27\ArcGISx6410.2\python 19 | 20 | ## Tricks 21 | * [Use arcpy.da.UpdateCursor and arcpy.da.SearchCursor for fast in memory reprojection](Tricks/cursors_fast_reproject.py) 22 | * [Output in_memory feature classes to shapefiles for debugging](Tricks/create_shp_for_debugging.py) 23 | * Output map documents to mxds for debugging 24 | * All Geoprocessing services have a built-in file upload option that you can take advantage of 25 | * For performance reasons, use synchronous tasks when possible 26 | * Set logging level to "Info" in geoprocessing tasks to capture excution time of the task 27 | * Set running instances to 0 for all GP and Mapservices in development to conserve memory on the ArcGIS server 28 | * Access files on a windows file share in an arcpy script using win_unc 29 | * Geoprocssing services are running 64bit (c:\PYTHON27\ArcGISx6410.2) python while scripts that are run via ArcCatalog are running under 32bit python (c:\PYTHON27\ArcGIS10.2) you must install both a 32bit and a 64bit python library to be able to publish a GP service 30 | 31 | ## Hacks 32 | * sql server rowid 33 | -------------------------------------------------------------------------------- /Tips/call_from_command_line.md: -------------------------------------------------------------------------------- 1 | Use [arcpy.GetParameterAsText()](http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000047000000) to read in commandline arguments as you would use [sys.argv](https://docs.python.org/2/library/sys.html) in non-arcpy python scripts. 2 | 3 | [Example arcpy python code](call_from_command_line.py): 4 | 5 | ```python 6 | import arcpy 7 | 8 | input = arcpy.GetParameterAsText(0) 9 | print input 10 | ``` 11 | 12 | Now call this from the commandline with an argument: 13 | 14 | ``` 15 | python call_from_command_line.py "Hello GetParameterAsText!" 16 | 17 | Hello GetParameterAsText! 18 | ``` 19 | -------------------------------------------------------------------------------- /Tips/call_from_command_line.py: -------------------------------------------------------------------------------- 1 | import arcpy 2 | 3 | input = arcpy.GetParameterAsText(0) 4 | print input -------------------------------------------------------------------------------- /Tips/install_pip.cmd: -------------------------------------------------------------------------------- 1 | rem this guide assumes ArcGIS Server 10.2 running on windows 2 | rem download ez_setup.py from http://peak.telecommunity.com/dist/ez_setup.py 3 | rem open command prompt as administrator 4 | 5 | rem install 64 bit version 6 | c:\python27\ArcGISx6410.2\python ez_setup.py 7 | cd c:\python27\ArcGISx6410.2\Scripts\ 8 | easy_install.exe install pip 9 | 10 | rem install 32 bit version 11 | c:\python27\ArcGIS10.2\python ez_setup.py 12 | cd c:\python27\ArcGIS10.2\Scripts\ 13 | easy_install.exe install pip 14 | 15 | rem pip is now ready 16 | rem pip install -------------------------------------------------------------------------------- /Tricks/create_shp_for_debugging.py: -------------------------------------------------------------------------------- 1 | import arcpy 2 | import glob 3 | import os 4 | 5 | # Lets do some clean up first 6 | for shpFilePart in glob.glob("../ExampleData/out_points*"): 7 | os.remove(shpFilePart) 8 | 9 | # define feature class locations 10 | springsShapefile = "../ExampleData/points.shp" 11 | springsInMemory = "in_memory//points" 12 | 13 | # copy in shapefile to in_memory feature class 14 | arcpy.CopyFeatures_management (springsShapefile, springsInMemory) 15 | 16 | # copy in_memory feature class to shapefile 17 | arcpy.CopyFeatures_management (springsInMemory, "../ExampleData/out_points.shp") 18 | -------------------------------------------------------------------------------- /Tricks/cursors_fast_reproject.py: -------------------------------------------------------------------------------- 1 | import arcpy 2 | 3 | # define feature class locations 4 | pointssShapefile = "../ExampleData/points.shp" 5 | pointsInMemory = "in_memory//points" 6 | 7 | # copy in shapefile to in_memory feature class 8 | arcpy.CopyFeatures_management (pointssShapefile, pointsInMemory) 9 | 10 | # create spatial reference object 11 | sr = arcpy.SpatialReference(4236) 12 | 13 | ##### #1 Cursor based reprojection da.UpdateCursor (fastest) 14 | with arcpy.da.UpdateCursor(pointsInMemory, 'SHAPE@WKT', spatial_reference=sr) as cursor: 15 | for row in cursor: 16 | print row[0] 17 | cursor.updateRow(row) 18 | 19 | ##### #2 Cursor based reprojection da.SearchCursor (fastest) 20 | with arcpy.da.SearchCursor(pointsInMemory, 'SHAPE@WKT', spatial_reference=sr) as cursor: 21 | for row in cursor: 22 | print row[0] 23 | 24 | 25 | ##### #3 Cursor based reprojection arcpy.SearchCursor (slower than using da cursors, only use for ArcGIS 10) 26 | cursor = arcpy.UpdateCursor(pointsInMemory, spatial_reference=sr) 27 | for row in cursor: 28 | print row 29 | cursor.updateRow(row) 30 | 31 | ##### #4 Cursor based reprojection arcpy.UpdateCursor (slower than using da cursors, only use for ArcGIS 10) 32 | cursor = arcpy.SearchCursor(pointsInMemory, spatial_reference=sr) 33 | for row in cursor: 34 | print row 35 | --------------------------------------------------------------------------------