# --------------------------------------------------------------------------- # soil_data_transfer.py # Created on: Fri Mar 28 2006 02:40:26 PM # (generated by ArcGIS/ModelBuilder) # Usage: soil_select_test # --------------------------------------------------------------------------- # Import system modules import sys, string, os, win32com.client def process_soil_names(): # only need to get dictionary list of soil names and total number of cases that should be found global soil_name, max_cases comma = {} musym_by_county_name = "C:\\GISResources\\ssurgo\\musym_by_county.csv" musym_file = open(musym_by_county_name,"r") header_line = musym_file.readline() for soil_num in range(340): # need to set to range(340) to do all soils process_line = musym_file.readline() comma[1] = process_line.find(",") comma[2] = process_line.find(",", comma[1] + 1) soil_name[soil_num] = process_line[:comma[1]] #print process_line #print str(soil_num)+" = soil num, and max_cases string version = "+process_line[comma[1] + 1:comma[2]] max_cases[soil_num] = int(process_line[comma[1] + 1:comma[2]]) ignore_line = musym_file.readline() # final line at end of each county's data is a summary line musym_file.close() return def read_DBF(soil_to_use): # function opens a DBF file and extracts values of interest to a global dictionary global DBF_output, DBF_original, status_of_soils records_found = 0 first_size = 136 * 2 - 41 regular_move = 136 OID_pos = 42 orig_area_pos = 92 new_calc_pos = 111 need_to_close_later = 0 #temp = 0 target_input1 = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\Soils\\clip_"+soil_to_use+".dbf" target_input2 = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\Soils\\clip_"+soil_to_use+"_wArea.dbf" if os.path.isfile(target_input2): # wArea clipped file with new area calculated does exist target_in = open(target_input2, "r") need_to_close_later = 1 this_seek = target_in.read(first_size) this_seek = target_in.read(regular_move) while this_seek != "": # need to process string into OBJECTID,SHAPE_Leng, SHAPE_Area, OIDinTEXT, Calc_Area, F_AREA OBJECTID = int(this_seek[:4]) OIDinTEXT = this_seek[OID_pos:OID_pos + 50].strip() #print str(OBJECTID)+" = OBJECTID and OIDinTEXT = "+OIDinTEXT orig_area = float(this_seek[orig_area_pos:orig_area_pos + 19]) new_calc = float(this_seek[new_calc_pos:new_calc_pos + 22]) #print str(orig_area)+" = orig area, while new calc = "+str(new_calc) DBF_output[soil_to_use,OIDinTEXT] = new_calc # index as a tuple including soil name existing_size = DBF_original.get(OIDinTEXT,0) if orig_area > existing_size: DBF_original[OIDinTEXT] = orig_area # index by OID records_found += 1 this_seek = target_in.read(regular_move) else: if os.path.isfile(target_input1): # clipped file exists but not newly recalculated wArea file print "no wArea file found even though clipped file did exist for soil = "+soil_to_use else: # neither file exists print "cannot find either the clipped file or wArea recalculated file for soil = "+soil_to_use if records_found == 0: status_of_soils[soil_to_use] = 0 # add to list of soils lacking any data else: status_of_soils[soil_to_use] = 1 # add to list of soils with non-zero areas if need_to_close_later == 1: target_in.close() return records_found # Create the Geoprocessor object #gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # Set the necessary product code #gp.SetProduct("ArcInfo") # 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.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Statistics Tools.tbx") #gp.OverwriteOutput = 1 # no separate Boolean type in Python 2.1, must use 0 and 1 ## No GP calls are made. This batch file does not use ArcGIS geoprocessor. ## All the work is done within Python, and it is very fast compared to calling the GP! # create global variables calc_area_list = {} # start with empty dictionaries soil_name = {} max_cases = {} DBF_output = {} DBF_original = {} status_of_soils = {} rec_count_by_soils = {} soil_number_translater = {} count_good_soils = 0 # general files for storing data no_data_soils = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\Soils\\no_data_soils.csv" data_exists_soils = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\Soils\\data_exists_soils.csv" matrix_soils_by_fields = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\Soils\\matrix_soils_by_fields.csv" soil_dictionary = "C:\\DataFiles\\OSUSeedCert\\SpatialStats\\Soils\\soil_python_dictionary.txt" no_data = open(no_data_soils,"w") data_exists = open(data_exists_soils,"w") matrix_soils = open(matrix_soils_by_fields,"w") soil_saved_dictionary = open(soil_dictionary,"w") ## main program section here print print "running soil_data_transfer now" process_soil_names() # generates global dictionary of soil_names indexed by soil_num for soil_num in range(0,340): # full run is 0 to 340 soils this_soil = soil_name[soil_num] # this_soil = "Hazelair_silt_loam" recs_for_this_soil = read_DBF(this_soil) print "number of rows found for soil "+this_soil+" is = "+str(recs_for_this_soil) if recs_for_this_soil == 0: output = this_soil+","+str(soil_num)+","+str(0)+"\n" no_data.write(output) no_data.flush() else: output = this_soil+","+str(soil_num)+","+str(recs_for_this_soil)+"\n" data_exists.write(output) data_exists.flush() soil_number_translater[count_good_soils] = soil_num count_good_soils += 1 soil_saved_dictionary.write(str(DBF_output)) soil_saved_dictionary.close() no_data.close() data_exists.close() #now run a big loop to write each line into a CSV file for Excel params = "OID_number,original_area," for soils_to_export in range(count_good_soils): real_soil_number = soil_number_translater[soils_to_export] params += soil_name[real_soil_number]+"," params += "\n" matrix_soils.write(params) # this gets the header names written to file for OID_number in range(2852): # this dumps some empty lines for OID numbers that weren't used existing_size = DBF_original.get(str(OID_number),0) params = str(OID_number)+","+str(existing_size)+"," for inner_loop in range(count_good_soils): real_soil_number = soil_number_translater[inner_loop] index = (soil_name[real_soil_number],str(OID_number)) clipped_area = DBF_output.get(index,0) params += str(clipped_area)+"," params += "\n" matrix_soils.write(params) matrix_soils.flush() matrix_soils.close()