# --------------------------------------------------------------------------- # soil_selector.py # Created on: Fri Mar 24 2006 02:40:26 PM # (generated by ArcGIS/ModelBuilder) # Usage: soil_select_test # --------------------------------------------------------------------------- # Import system modules import sys, string, os, win32com.client # Create the Geoprocessor object gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # Load required toolboxes... gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx") gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx") gp.OverwriteOutput = 1 # no separate Boolean type in Python 2.1, must use 0 and 1 soil_name = {} max_cases = {} county_cases = {} musym = {} comma = {} county = {} county_select_string = {} musym_by_county_name = "C:\\GISResources\\ssurgo\\musym_by_county.csv" musym_file = open(musym_by_county_name,"r") for counties in range(6): # change to range(6) header_line = musym_file.readline() comma[1] = header_line.find(",") comma[2] = header_line.find(",", comma[1] + 1) comma[3] = header_line.find(",", comma[2] + 1) comma[4] = header_line.find(",", comma[3] + 1) county[counties] = header_line[comma[3] + 1:comma[4]] county_select_string[counties] = "\"areasymbol\" = 'OR"+county[counties][:3]+"' AND ( " print header_line print county[counties] print county_select_string for soil_num in range(340): # need to set to range(340) process_line = musym_file.readline() comma[1] = process_line.find(",") comma[2] = process_line.find(",", comma[1] + 1) comma[3] = process_line.find(",", comma[2] + 1) comma[4] = process_line.find(",", comma[3] + 1) comma[5] = process_line.find(",", comma[4] + 1) comma[6] = process_line.find(",", comma[5] + 1) comma[7] = process_line.find(",", comma[6] + 1) comma[8] = process_line.find(",", comma[7] + 1) comma[9] = process_line.find(",", comma[8] + 1) comma[10] = process_line.find(",", comma[9] + 1) comma[11] = process_line.find(",", comma[10] + 1) soil_name[soil_num] = process_line[:comma[1]] max_cases[soil_num] = int(process_line[comma[1] + 1:comma[2]]) county_cases[soil_num] = int(process_line[comma[2] + 1:comma[3]]) for multi_musym in range(8): index = county[counties]+", "+str(soil_num)+", "+str(multi_musym) skip_space = process_line[comma[3+multi_musym] + 1:comma[4+multi_musym]] if skip_space != " " and skip_space != "": musym[index] = skip_space print ".", ignore_line = musym_file.readline() # final line at end of each county's data is a summary line print print "done reading in County "+county[counties] values_here = musym.values() indexes_here = musym.keys() values_here.sort() indexes_here.sort() print values_here print indexes_here musym_file.close() print "file closed" print soil_name[141] print soil_name[142] #print soil_name[235] #print soil_name[282] print print #stop_here = 1/0 print # now ready to actually run the selections by musym and create files for each specific soil name western_oregon_soils = "C:\\GISResources\\ssurgo\\western_oregon_soils.shp" for soil_num in range(141,142): this_soil = soil_name[soil_num] soil_file_name = "C:\\GISResources\\ssurgo\\"+this_soil+".shp" params = "( " for counties in range(6): params += county_select_string[counties] for multi_musym in range(8): index = county[counties]+", "+str(soil_num)+", "+str(multi_musym) if musym.has_key(index): params += "\"musym\" = '"+musym[index]+"' OR " if params[-4:] == " OR ": # should always have a dangling 'OR' unless no key existed for this soil in this county fix = params[:-3]+")) " params = fix else: params += "\"musym\" = '999999ZZZ' )) " # a very unlikely soil name! params += "OR ( " if params[-6:] == " OR ( ": fix = params[:-6]+" " params = fix print soil_file_name print params print gp.Select_analysis(western_oregon_soils, soil_file_name, params) soil_dissolved = soil_file_name[:-4]+"_dissolve.shp" gp.Dissolve_management(soil_file_name, soil_dissolved, "areasymbol;mukey", "", "MULTI_PART") # Script arguments... #test_Woodburn_Dissolve_shp = '#' #if test_Woodburn_Dissolve_shp == '#': # test_Woodburn_Dissolve_shp = "C:\\GISResources\\ssurgo\\test_Woodburn_Dissolve6.shp" # provide a default value if unspecified #western_oregon_soils = '#' #sys.argv[2] #if western_oregon_soils == '#': # western_oregon_soils = "western_oregon_soils" # provide a default value if unspecified # Local variables... #test_Woodburn_shp = "C:\\GISResources\\ssurgo\\test_Woodburn6.shp" #new_table_out = "C:\\GISResources\\ssurgo\\test_Woodburn_Dissolve_temporary.dbf" #western_oregon_soils = "C:\\GISResources\\ssurgo\\western_oregon_soils.shp" # Process: Select... #gp.Select_analysis(western_oregon_soils, test_Woodburn5_shp, "( \"areasymbol\" = 'OR003' AND ( \"musym\" = '177' OR \"musym\" = '179' OR\ #\"musym\" = '180' OR \"musym\" = '178' )) OR ( \"areasymbol\" = 'OR637' AND ( \"musym\" = '139' )) OR ( \"areasymbol\" = 'OR639' AND ( \"musym\" = '106A' OR \"musym\" = '106C')) OR\ #( \"areasymbol\" = 'OR643' AND ( \"musym\" = 'WuC' OR \"musym\" = 'WuD' OR \"musym\" = 'WuA'))") #print "ready for Select_analysis" #params = "( \"areasymbol\" = 'OR003' AND ( \"musym\" = '177' OR \"musym\" = '179' OR \"musym\" = '180' OR \"musym\" = '178' ))" ## missing one final space??? # last one I tried Friday params = "\"( \"areasymbol\" = 'OR003')\"" params = "( \"areasymbol\" = 'OR003' AND ( \"musym\" = '177' OR \"musym\" = '179' OR \"musym\" = '180' OR \"musym\" = '178' )) " #print params #print #gp.Select_analysis(western_oregon_soils, test_Woodburn_shp, params) #gp.TableSelect_analysis(western_oregon_soils, new_table_out, params) #gp.Select_analysis(western_oregon_soils, test_Woodburn_shp, param) print "ready for Dissolve" print # OR ( \"areasymbol\" = 'OR637' AND ( \"musym\" = '139' )) OR ( \"areasymbol\" = 'OR639' AND ( \"musym\" = '106A' OR \"musym\" = '106C')) OR\ #( \"areasymbol\" = 'OR643' AND ( \"musym\" = 'WuC' OR \"musym\" = 'WuD' OR \"musym\" = 'WuA'))") # Process: Dissolve... #gp.Dissolve_management(new_table_out, test_Woodburn_Dissolve_shp, "areasymbol;mukey", "", "MULTI_PART")