Skip to content

Instantly share code, notes, and snippets.

@calvinmetcalf
Last active February 7, 2024 06:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save calvinmetcalf/5048936 to your computer and use it in GitHub Desktop.
Save calvinmetcalf/5048936 to your computer and use it in GitHub Desktop.
arcpy tutorial

Let's make a tool!

import arcpy, os, tempfile

import is used to bring in another library, here we bring in:

  • arcpy which is the esri library and
  • os which for Operating system things
  • tempfile is for making temperary files
thing1 = arcpy.GetParameterAsText(0)
thing2 = arcpy.GetParameterAsText(1)

this assigns thing1 the value for whatever is in the first box, and thing2 gets the second box, notice that the numbering starts at 0, this is what usually happends in Python quick reference: toolbox anytool you can get to inside arcgis desktop you can get through python the naming convention is

arcpy.toolboxname.toolname()
#or
arcpy.toolname_toolboxname()
#e.g. append tool is
arcpy.management.Append()
#or
arcpy.Append_management()

Both version mean the same thing and in case you hadn't noticed Data Management is managment in python, most of the other toolboxes have the same name as the tool, analysis, cartography, and conversion. The only other tool I acutally have used that has a different name is Spatial Analyst which is sa, but thats the least strange thing of that toolbox.

Quick pyton basics:

  • python is "white space sensitive", on other words it uses intent to mean things.
  • you need to use 2 spaces, 4 spaces, or 1 tab to indent and you need to be consistent in your file
    • side note space/tab debate is up there with religion and politics with programs, so be aware.
  • types of data include
    • numbers
      • Integers

        • regular: whole numbers in the range -2147483648 through 2147483647 esri equivalent is "SHORT", desplayed as

           2147483647
        • long whole numbers bigger that that ESRI equivalent is "LONG", desplayed as

           2147483648L
        • Booleans: 0 or 1, ESRI does not have this type, but Access does, desplayed as

           true
           false
      • Reals aka floats, numbers with decimal points, ESRI equivilent is technically "DOUBLE" and "FLOAT" gets converted to this. desplayed as

         1.5

        note that floating points are an aproximiation of decimals so

         1.5-0.1

        returns

         1.3999999999999999
      • Complex, you will never use this

    • Sequences
      • Mutable

        • List, these are a major component of python, they are represented with square brackets

           myList = [1,2,3]

          they are called mutable because you can modify them, like append a value

           myList.append(4)

          myList is now [1,2,3,4]

      • Immutable

        • Strings, pieces of ASCII text, equivilent to ESRI "TEXT", represented as

           'hello'
        • Unicode, pieces of Unicode text, ESRI doesn't like unicode so avoid like the plauge, represented as

           u'hellø'
        • Tuple, like a list but can't be changed, represented as

           (1,2,3)
    • Mappings
      • Dictionary, a map of keys to values represented as:

         myDict = {"a":1,2:"b"}

        you can then look up and set values like so

         myDict["a"]

        returns 1

         myDict["e"]=4

        sets the key "e" to 4 you can also set them to more complex values

         myDict["d"] = [1,2,3]
         myDict[(1,2,3)]="some amount of text"

        the only thing that can't be a key is a list or another dictionary but they can be values

    • The rest, don't worry about them for now The iteraters like lists and dicts are called that because you can itterate through them, so if you have a list of [1,2,3], you can interate through it with:
for v in [1,2,3]:
	print(v)
#prints 1 2 3
# you can also iterate dicts
for v in {1:"a",2:2,3:(1,2,3)}:
	print(v)
#also prints 1,2,3
for v,k in {1:"a",2:2,3:(1,2,3)}.iteritems():
	print(v)
	print(k)
prints 1 a 2 2 3 (1,2,3)

so lets take this knowledge and make a tool, specifically we are going to make our own Feature Class To Geodatabase(multiple)

import arcpy, os

features = arcpy.GetParameterAsText(0)
db = arcpy.GetParameterAsText(1)

for feature in features.split(";"):
    outName = os.path.splitext(os.path.split(feature)[1])[0]
    arcpy.conversion.FeatureClassToFeatureClass(feature,db,outName)

so features is a semicolin seperated string that we use the string method split on to get a list of the values, then this is where os comes in, os.split takes "c://a/b/c.txt" and returns (u'c:////a//b//',u'c.txt') and os.path.splitext takes "c.txt" and returns(u'c',u'.txt') so we can get the file name. Note this isn't as good as the internal one, as it will explode if you try to do something from a database connetion.

now we add it in step 1 step 2 step 3 step 4 step 5 step 6 step 7 step 7

@cprice-usgs
Copy link

Instead of using the tempfile module you could instead use arcpy.CreateScratchName, which has the added benefit of creating unique object names in containers tempfile doesn't know about (ie geodatabases).

@anwar79melb
Copy link

Hi Calvin,
This is the nice tutorial. How can I read the complete tutorial?
Sincerely,
Anwar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment