I’m hoping for some assistance getting my Python script up and running (GIS usage). /u/MarineBiomancer Python Education

It’s been awhile since I’ve used Python, as I haven’t really touched it since my GIS Python course in college 2 years ago haha; so, I’m more than a little rusty (especially since that class was a really accelerated crash course. However, since Python is so useful in my field, I wanted to use some current projects at work, that have a good amount of time budgeted for training, to try and expand my skills with it.

So my current project is adding a classification to an existing data layer’s attribute field, based off of time criteria (event happens during a certain time in a given month, it gets classified as X, etc.). I wanted to create a function where you could set your criteria parameters, rather than hunting through the code for it if you wanted to modify them, but we are a bit aways from that haha. So, right now I’m just working on setting up a basic function with a if/else/elif logic loop to work through my data, with the parameters already set within the arcpy tools used within the function. However, I’m running into some syntax errors with the !s on either side of my field name, even though that’s how the guides I’ve been reading have it formatted.

This is where I’m at right now:

# Import module

import arcpy

# Define layer being modified

data_layer = “W:\o-drive\d-multiyear\d-RPC_Crash_Analysis\MyProject.gdb\RPCWaze_Crashes_2022”

def period_function(DayPeriod):

if arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”6:37:00 AM”, end_time=”7:09:00 AM”, months=[“JANUARY”]):

return “Dawn”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”7:09:00 AM”, end_time=”8:09:00 AM”, months=[“JANUARY”]):

return “Early Morning”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”8:09:00 AM”, end_time=”11:22:00 AM”, months=[“JANUARY”]):

return “Morning”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”11:22:00 AM”, end_time=”12:22:00 PM”, months=[“JANUARY”]):

return “Noon”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”12:22:00 PM”, end_time=”3:34:00 PM”, months=[“JANUARY”]):

return “Afternoon”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”3:34:00 PM”, end_time=”4:34:00 PM”, months=[“JANUARY”]):

return “Late Afternoon”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”4:34:00 PM”, end_time=”5:06:00 PM”, months=[“JANUARY”]):

return “Dusk”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”5:06:00 PM”, end_time=”6:37:00 AM”, months=[“JANUARY”]):

return “Night”

period_function(!Day_Period!)

print(“Done”)

submitted by /u/MarineBiomancer
[link] [comments]

​r/learnpython It’s been awhile since I’ve used Python, as I haven’t really touched it since my GIS Python course in college 2 years ago haha; so, I’m more than a little rusty (especially since that class was a really accelerated crash course. However, since Python is so useful in my field, I wanted to use some current projects at work, that have a good amount of time budgeted for training, to try and expand my skills with it. So my current project is adding a classification to an existing data layer’s attribute field, based off of time criteria (event happens during a certain time in a given month, it gets classified as X, etc.). I wanted to create a function where you could set your criteria parameters, rather than hunting through the code for it if you wanted to modify them, but we are a bit aways from that haha. So, right now I’m just working on setting up a basic function with a if/else/elif logic loop to work through my data, with the parameters already set within the arcpy tools used within the function. However, I’m running into some syntax errors with the !s on either side of my field name, even though that’s how the guides I’ve been reading have it formatted. This is where I’m at right now: # Import module import arcpy # Define layer being modified data_layer = “W:\o-drive\d-multiyear\d-RPC_Crash_Analysis\MyProject.gdb\RPCWaze_Crashes_2022″ def period_function(DayPeriod): if arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”6:37:00 AM”, end_time=”7:09:00 AM”, months=[“JANUARY”]): return “Dawn” elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”7:09:00 AM”, end_time=”8:09:00 AM”, months=[“JANUARY”]): return “Early Morning” elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”8:09:00 AM”, end_time=”11:22:00 AM”, months=[“JANUARY”]): return “Morning” elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”11:22:00 AM”, end_time=”12:22:00 PM”, months=[“JANUARY”]): return “Noon” elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”12:22:00 PM”, end_time=”3:34:00 PM”, months=[“JANUARY”]): return “Afternoon” elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”3:34:00 PM”, end_time=”4:34:00 PM”, months=[“JANUARY”]): return “Late Afternoon” elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”4:34:00 PM”, end_time=”5:06:00 PM”, months=[“JANUARY”]): return “Dusk” elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”5:06:00 PM”, end_time=”6:37:00 AM”, months=[“JANUARY”]): return “Night” period_function(!Day_Period!) print(“Done”) submitted by /u/MarineBiomancer [link] [comments] 

It’s been awhile since I’ve used Python, as I haven’t really touched it since my GIS Python course in college 2 years ago haha; so, I’m more than a little rusty (especially since that class was a really accelerated crash course. However, since Python is so useful in my field, I wanted to use some current projects at work, that have a good amount of time budgeted for training, to try and expand my skills with it.

So my current project is adding a classification to an existing data layer’s attribute field, based off of time criteria (event happens during a certain time in a given month, it gets classified as X, etc.). I wanted to create a function where you could set your criteria parameters, rather than hunting through the code for it if you wanted to modify them, but we are a bit aways from that haha. So, right now I’m just working on setting up a basic function with a if/else/elif logic loop to work through my data, with the parameters already set within the arcpy tools used within the function. However, I’m running into some syntax errors with the !s on either side of my field name, even though that’s how the guides I’ve been reading have it formatted.

This is where I’m at right now:

# Import module

import arcpy

# Define layer being modified

data_layer = “W:\o-drive\d-multiyear\d-RPC_Crash_Analysis\MyProject.gdb\RPCWaze_Crashes_2022”

def period_function(DayPeriod):

if arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”6:37:00 AM”, end_time=”7:09:00 AM”, months=[“JANUARY”]):

return “Dawn”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”7:09:00 AM”, end_time=”8:09:00 AM”, months=[“JANUARY”]):

return “Early Morning”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”8:09:00 AM”, end_time=”11:22:00 AM”, months=[“JANUARY”]):

return “Morning”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”11:22:00 AM”, end_time=”12:22:00 PM”, months=[“JANUARY”]):

return “Noon”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”12:22:00 PM”, end_time=”3:34:00 PM”, months=[“JANUARY”]):

return “Afternoon”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”3:34:00 PM”, end_time=”4:34:00 PM”, months=[“JANUARY”]):

return “Late Afternoon”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”4:34:00 PM”, end_time=”5:06:00 PM”, months=[“JANUARY”]):

return “Dusk”

elif arcpy.ca.SelectLayerByDateAndTime(in_layer=data_layer, selection_type=”NEW_SELECTION”, time_type=”SINGLE_TIME_FIELD”, date_field=”Incident_Start”, selection_options=[“TIME”, “MONTH”], start_time=”5:06:00 PM”, end_time=”6:37:00 AM”, months=[“JANUARY”]):

return “Night”

period_function(!Day_Period!)

print(“Done”)

submitted by /u/MarineBiomancer
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *