utz.ryan

Script somehow deleting all points in a shapefile (pour point automation)

Discussion created by utz.ryan on Feb 10, 2012
Latest reply on Feb 10, 2012 by stacyrendall
Hello,

I am very new to Python scripting- and used to program in Arc/INFO (how I miss it so these days). All I am trying to do is write a script that will 1) import a list of integers from a CSV file, 2) use this list to select points in a shapefile with matching values, and 3) run the hydro tool "Snap pour point" & save the pour point. I thought that I had made great progress yesterday and had the code completed (thanks to Curtis). However, the code below produces nonsensical, non-existent pour points and, for some reason, the code deleted all of the entries in the point shapefile I pointed it to!!! The shapefile still exists, but is completely empty. And the SnapPourPoint command seems reasonably easy enough; thus I have no idea why it results in completely empty entities...

Please help a newcomer!! This is driving me crazy and I am really not becoming a Python fan... -CSV file: "E:/Weathering_GIS/Sheds.csv", point shapefile [that gets emptied for some reason]: "E:/Weathering_GIS/Working.gdb/USGS_Points", flow accumulation grid: "FAC_NORTHEAST"

Thanks,
Ryan

Code:

import csv, arcpy
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")


env.workspace = 'E:/Weathering_GIS/Working.gdb'

try:
   f = open('E:/Weathering_GIS/Sheds.csv')
   sheds = csv.reader(f)
   for row in sheds:
      arcpy.MakeFeatureLayer_management('E:/Weathering_GIS/Working.gdb/USGS_Points', 'Shedpoints')
      where = "GageCode = %s" % int(row[0])
      arcpy.SelectLayerByAttribute_management("Shedpoints","NEW_SELECTION",where)  
      snapped = SnapPourPoint("Shedpoints","FAC_NORTHEAST",0.0001)
      out = "X%s" % str(row[0])
      snapped.save("E:/Weathering_GIS/Working.gdb/"+out)

except:
    del sheds
    f.close()
    raise

Outcomes