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.