""" @author: James Larsson @collaborators: None """ import arcpy # Import the ArcPy module import time import datetime arcpy.env.workspace = arcpy.GetParameterAsText(0) # Get 'Workspace' parameter from ArcGIS arcpy.env.overwriteOutput = True # Allow output to overwrite input_pts = arcpy.GetParameterAsText(1) # Snapped points flowdir = arcpy.GetParameterAsText(2) # D8 Flow direction raster output_fc = arcpy.GetParameterAsText(3) # Output vector print("Creating cursor") fields = ["OBJECTID"] # Select subset of fields from species observations shapefile to include cursor = arcpy.da.SearchCursor(input_pts, "*") # Create search cursor for observations shapefile count = 0 # Set count to 0 total_rows = arcpy.management.GetCount(input_pts)[0] global_start_time = time.time() print("Beginning create watersheds") for row in cursor: # For each row in feature class start_time = time.time() if(count > 0): print("Estimated time remaining: %s " % datetime.timedelta(seconds = (((start_time-global_start_time)/count)*(int(total_rows)-count)))) # timedelta from https://www.geeksforgeeks.org/python-program-to-convert-seconds-into-hours-minutes-and-seconds/ print("Running on row %s" % count) where_clause = " \"OBJECTID\" = %s " % row[0] # Where clause to select schools with zero hospitals in 3-mile buffer arcpy.management.MakeFeatureLayer(input_pts, "lyr") # Feature layer from Schools.shp arcpy.management.SelectLayerByAttribute("lyr", "NEW_SELECTION", where_clause) arcpy.management.CopyFeatures("lyr", "current_pt") print("Creating watershed raster...") watershed = arcpy.sa.Watershed(flowdir, "current_pt") print("Converting to polygon...") arcpy.conversion.RasterToPolygon(watershed, "current_watershed") arcpy.management.Dissolve("current_watershed", "dissolved") arcpy.management.JoinField("dissolved", "OBJECTID", "current_pt","OBJECTID") print("Attaching to output") if count == 0: # If this is the first observation processed, create a new feature class at location output_fc arcpy.management.CopyFeatures("dissolved", output_fc) # Copy new shape to output feature class arcpy.AddMessage(" Created new feature class for %s with basin ID %s" % (output_fc, row[0])) else: arcpy.management.Append("dissolved", output_fc) # Append new shape to output feature class arcpy.AddMessage(" Appended to feature class %s with basin ID %s" % (output_fc, row[0])) print("Cleaning up") arcpy.management.Delete(watershed) print("Time elapsed for point: %.2f sec" % (time.time()-start_time)) count += 1 # Increment count