Lab 05
Lab 05: Arcpy
This lab covers the Arcpy, including topics such as SearchCursor, UpdateCursor and InsertCursor.
There are four questions in total. Please provide your code answers directly below each question.
Make sure to run all cells so that the answers are stored. Once completed, submit the .ipynb file (ensuring that all answers are included) to Canvas by midnight (11:29 PM) on March 10.
This lab is worth a total of 80 points and contributes 8% toward the final grade.
1. SearchCursor (20 points)¶
Use sedf and groupby() to analyze '2024_Vehicle_Crash', grouping by weather conditions (WEATH_COND) to determine the number of accidents under different weather conditions.
import pandas as pd
import arcpy
from arcgis.features import GeoAccessor
2. Insert below new data using InsertCursor (30 points)¶
- Using InsertCursor to add 'points_data' to the '2024_Vehicle_Crash'
points_data = [{'CRASH_SEVE': 'Non-fatal injury', 'NUMB_VEHC': 1, 'SHAPE': (-71.791180, 42.249540)},
{'CRASH_SEVE': 'Unknown', 'NUMB_VEHC': 0, 'SHAPE': (-71.811697, 42.257009)},
{'CRASH_SEVE': 'Non-fatal injury', 'NUMB_VEHC': 2, 'SHAPE': ( -71.772558, 42.258216)},]
3. Update "Vehicle_Crash_insert" using UpdateCursor (20 points)¶
- Add a new field named 'NUM_Class' of type 'Text' to '2024_Vehicle_Crash'.
- Update Values:
- If 'NUMB_VEHC' is greater than or equal to 3, update the 'NUM_Class' to 'More'.
- If 'NUMB_VEHC' is less than 3, update 'NUM_Class' to 'Less'.
4. Statistics (10 points)¶
Using the shapefile: 2024_Vehicle_Crash, analyze the distribution of vehicle types involved in crashes.
Each record in the dataset includes a field NUM_Class (created in Task 3), which classifies the type of vehicle involved in the crash into two categories:
"More" and "Less".
Your goal is to:
- Count the number of crashes involving each type of NUM_Class.
- Calculate the proportion of each vehicle class relative to the total number of crashes.
You can use arcpy.da.SearchCursor to iterate through the dataset or use the groupby function for spatially enabled DataFrame (sedf) to perform the same analysis.