Hollinger Week 4

Chapter 8:

Reading Notes:

    • Search, insert, and update cursors. Can be created by using da.SearchCursor, da.InsertCursor, and da.UpdateCursor.
    • Cursor classes in data access work much better than ArcPy cursor functions.
    • A shared lock is applied when a table or dataset is accessed.
    • Exclusive locks are applied when changes are being made to a table or dataset.
    • You can use the SearchCursor class to write SQL queries.
    • I honestly found the section on when to you what quotation marks a little confusing.
    • The calculate Value tool is a Model Builder Only tool.
    • When calculating Field the existing field must be referenced using its name surrounded by exclamation points. This syntax will not work in a regular Python IDE.
    • You can use the calculate field tool in ArcPy by using the CalculateField function.
    • Arcade expressions can also be used in a Python Script. When using Arcade in the Calculate Field Tool you use one block instead of having the expression and code block separate.
    • It is good practice to close files and you cannot ensure that a file that was opened will be closed otherwise. This will prevent unnecessary locks from occurring.

Answers to Review Questions:

      • A search cursor reads all records and finds specific records based on certain (defined) attributes. An insert cursor adds new rows to a table. An update cursor may modify attribute values or delete rows in a table.
      • Data locks occur because multiple processes may be trying to change the same table or file at the same time in two different applications. Shared locks occur when a table or dataset is accessed and exclusive locks occur when a table or dataset is modified. A lock can be released if the cursor is used inside a with statement, exclusively deleted, or the reset method is called on a cursor.
      • Double quotes are often used around a field name, but the entire clause needs to be recognized as a string, so single quotes are placed around it. An escape character or triple quotes can also be used to solve quotation confusion. Some data like enterprise geodatabases don’t use any quotes. One solution to this can be to use the AddFieldDelimiters function, which will add delimiters for you before using it in the SQL expression.
      • You can use WHERE, GROUP BY, ORDER, and TOP while writing ArcPy functions.
      • You could use the Exists function talked about in Chapter 7 or arcpy.ValidateTableName(name, {workspace}), but Validate Table Name does not determine whether the Table Name exists. You could also use arcpy.CreateUniqueName() to ensure that you are not creating or overwriting a file that already exists.
      • You can build your own custom expressions using the code block. You can define the function in the code block using the def keyword. Write your function and reference it to call the function as you would when programming outside of the code block in a normal script. This expression will be in a separate block from the code block when using Python. This allows you to perform more elaborate calculations. The edits that you make will be written to the table, feature class, etc. because you are using the Calculate Field tool.
      • To read data from a text file you would simply write the following code: f = open(“C:/Data/mytext.txt”) and then f.read() on the next line. This script reads the entire file, but you could also specify how many characters you would like to read. You can also use f.readline() to return one line at a time. f.readlines() will read all lines and return them as a list. For CSVs you can import the CSV file and then use the csv.reader function. For CSV files you can skip the header row using the next() function before you start reading data.

Chapter 9:

Reading Notes:

  • Geometry class creates geometry objects
  • Other useful classes: MultiPoint, PointGeometry, Polygon, Polyline, Array, and Point.
  • Array and Point are not geometry objects.
  • Geometric objects can be used in geoprocessing or written to a feature class.
  • For polygon features, the start and end coordinates will be the same.
  • You can limit the number of vertices printed by using an if statement or a while loop.
  • A search cursor can have a spatial reference set. If it is different than your feature class, geometry will be converted to the spatial reference of the cursor.

Answers to Review Questions:

    • You may need the full geometry object when doing more complex operations or creating new geometry. It should only be used when you need access to the full object because it can be a relatively large dataset that slows down your script. You can use shortcuts like SHAPE@LENGTH when you only need specific portions/properties of the geometry.
    • A point object is not a geometry object but can be used to construct geometry. Whereas, the PointGeometry Class is a geometric object that can take a Point object as an argument as well as a spatial reference argument. PointGeometry has a much more extensive list of properties and methods.
    • To read geometry and print coordinates, you can use a search cursor in a for loop to iterate over rows of a feature class and the coordinates of each point. Each row will contain 1 point in a point feature class. For polygons and polylines, it is less simple. You will need to iterate over each row (which corresponds to the geometry object) and then iterate through the getPart() method to obtain coordinates for each part of the geometry before moving on to the next row.
    • When multipart features are present, the way you work with geometry is impacted because you must not only iterate over each part and then points of a polygon or polyline feature (for example), but you must also iterate over each part of the feature. If the feature has holes, then you must include many if-else statements to take exterior rings, interior rings, and null points into account.
    • To create new features using geometry objects based on a list of coordinate values, the script needs to iterate over the list, creating a point object for each coordinate pair. Then, those point objects need to be added to an array object. Then, the geometry object can be created by using the array as input and adding spatial reference. New features are created using an insert cursor or copying geometry objects to a new features class.
    • The array class is used to hold a number of features that can then be used to create geometries. You may use it when creating polygons or polylines, for example, so that you can hold and define the coordinates of each part of a feature.

Hollinger Week 3

Chapter 6:

Reading Notes:

  • Exists() checks if a dataset exists. I have never used this before, but I can think of a few times it would have been helpful.
  • Catalog paths — paths recognized by Arc Pro.
  • Dynamic properties — “the properties available for use vary with the nature of the object being described”
  • * is a wildcard
  • arcpy.ListWorkspaces(“”, “FILEGDB”) allows you to list all the geodatabases in a workspace.
  • Most list functions in ArcPy return data elements in the current workspace.
  • You can use functions like ListWorkspace(), ListDatasets() and ListFiles() to explore your data sets and workspace.
  • Add brackets around the outside of an expression for list intervention.

Answers to Review Questions:

    • Describe() is a regular function of ArcPy. The properties for use will be determined by the type of object returned (this is true for da.Describe() too). da.Describe() is a function of the ArcPy Data Access module. It will return the same information as Describe(), but as a dictionary. da.Describe() is more transparent, while Describe() is faster.
    • System paths are paths recognized by Windows OS, while catalog paths are the paths recognized by ArcGIS Pro. This means that, for example, if you are exploring data in a folder in ArcGIS Pro it will expect a catalog path not a system path.
    • To list geodatabase feature classes you must specify the geodatabase in the path, without specifying you will just get only shapefile feature classes. Shapefile feature classes will also be listed with .shp while geodatabase feature classes will not.
    • da.Walk() takes the top-level workspace and non-required parameters for optional filtering to return workspace, folder, and filenames at each level.  It can recognize both catalog and system paths. So, for a complex subfolder structure, the function will list the path and then all folders, files, gdb, and workspaces at that path.
    • Some examples given for iterating in a loop are building pyramids for hundreds of rasters, describing all the fields in a dataset, reading several shapefiles, and copying them to another geodatabase. I personally have used them to create a multiring buffer around each of hundreds of polygons so I didn’t have to do it manually.
    • List comprehension is a concise way to create a list by iterating over an object. For example: “[<expression> for <item> in <list>”].  You can also add an if condition to facilitate filtering. It may be used to be more concise when working with lists.

Chapter 7:

Reading Notes:

  • Errors can occur from using a combo of spaces and tabs
  • I would say that I most frequently use print statements and error statements as I am debugging.
  • A single # is used for permanent comments and a double # symbol is used for temporarily commenting out lines of code during the testing process. I didn’t really know this. I usually always use single. I guess that is kind of sloppy though.
  • Built-in Python debugger package is called pdf, but editors also include a debugging environment. I always found debugging packages so much more time-consuming to use than a debugging environment or other methods.
  • Close ArcGIS Pro to release locks while running a script.
  • To check for errors and report meaningfully: try-except to ArcPy messaging function
  • You can specify multiple except statements within a try-except statement. You can also include a finally statement that will always run.
  • I have never used a custom exception before. This is a neat and really useful feature.
  • ArcPy will generate a system error when a geoprocessing error occurs.

Answers to Review Questions:

    • Some of the most common syntax errors have to do with misspelled keywords/variables, missing punctuation, and inconsistent indentation
    • Exceptions are errors that are detected while the script is running. These could be errors or unanticipated events. Handling exceptions allows scripts to be more robust because if the exception is caught then the program can continue running.
    • To debug you might review the content of error messages (this will potentially give you error codes and other useful info to help you figure out what is wrong, but they can also be misleading), print messages to your script (ex: printing out steps as you go so you know what is completed before the error), comment out select code (eliminating the error by commenting out and isolating it, but this does not tell you why an error occurs), or use a python debugger (a tool that allows you to step through your code line by line).
    • First, you should check for syntax issues. Then, set your breakpoints in the script. The debugger will run until a breakpoint is reached, but without it the debugger will go line by line. Run the debugger and at every breakpoint (if you are using IDLE, PyCharm, or a similar program) you will be able to see paths, variable values, etc. You can also step over code or quit the execution of the process while debugging.
    • To use a try-except statement, you would write try: then enter and indent. This is where the code you want to carry out will go. Then, you will use an except statement (aligned with the try statement) followed by a specific exception. On the next line an indented line of code will follow that should be carried out if the specific exception occurs. So, essentially this type of statement says (generalized), try to run this code, unless this exception occurs, then do this instead.
    • Using a custom exception will allow you to make your code and exceptions easier to understand. For example, in the text, a ValueError exception is replaced with the TooShortError. This makes your code more robust and useful because it will be more descriptive in telling what the problem is for you and other users.