# file converts DBF into weights matrix # Import system modules import sys, string, os, win32com.client # set up dictionaries between FID and OBJECTID list_of_objectid = {} target_input1 = "C:/DataFiles/OSUSeedCert/SpatialStats/LinnBoundaries_as_text_csv.txt" # change name of target_output for other sizes of distance offset #distance_offset = int(5280/4) distance_offset = int(0) target_output = "C:/DataFiles/OSUSeedCert/SpatialStats/Linn_new_0_mile_OBJECTID_matrix_newer.txt" # each matrix is named for its spatial offset distance # 0_mile in this case should create a matrix that gives same values for Moran's I or General G as the default simple inverse distance target_in1 = open(target_input1, "r") target_out = open(target_output, "w") target_out.write("OBJECTID"+"\n") param_list = target_in1.readlines() # read in all of file lines_in_file = int(len(param_list)) print param_list[0] print param_list[1] print for FID in range(0,lines_in_file-1): param = param_list[FID+1] second_comma = param.find(",",1) objectid = int(param[1:second_comma]) list_of_objectid[FID] = objectid target_in1.close() # dictionary has now been created to translate FID values into my OBJECTID values print "number of lines in file = ",lines_in_file print target_input2 = "C:/DataFiles/OSUSeedCert/SpatialStats/Linn_distance_matrix.dbf" target_in2 = open(target_input2, "r") skip_seek = 47+(2*57) big_count = long(2785*2785) count = long(0) # big_count = 10 print "big_count =",big_count params = target_in2.read(skip_seek) print "here we go" for count in range(0,big_count): # need to skip those records that were deleted - 4 in this case params = target_in2.read(57) # read in 57 bytes of data # print params fromID = int(params[0:10]) toID = int(params[11:19]) obj_fromID = list_of_objectid[fromID] obj_toID = list_of_objectid[toID] distance = float(params[20:39]) #weight = float(distance_offset/(distance_offset+distance)) ## this version gave arbitrary units of inverse miles, feet, multiple miles, 10s of feet, etc if distance+distance_offset != 0: weight = float(5280/(distance_offset+distance)) # this version will now always give in units of inverse miles else: weight = 1 target_out.write(str(obj_fromID)+" "+str(obj_toID)+" "+str(weight)+"\n") # print obj_fromID," ",obj_toID," ",weight big_count = count*57+47+(2*57) number_rows = count point_count = pow(number_rows,0.5) print "total size = ",big_count," bytes and ",number_rows," rows and ",point_count," points" print target_in2.close() target_out.flush() target_out.close() print "all done now"