#Chris Lowrie
#Refactoring 1
#Bluespace Report and Row Creation Script
#import the necessary bits
import arcpy, math, os
#Get the points to use
table = arcpy.GetParameterAsText(0)
name = arcpy.GetParameterAsText(1)
fields = ["Latitude","Longitude", "HMS", "Day"]
date_fld = "DateTimeS"
arcpy.env.workspace = "C:\Users\project-user\Desktop\BlueSpace"
f = open(name + "_Bluespace_report.txt", "w")
fid_lst = []
#Calculate the distance between two points near Wellington, New Zealand
#Can be made more accurate if desired
def calculate_distance(x0, x1, y0, y1):
#111,200 meters per degree of latitude
#67,097 meters per degree of longitude at 41 degrees latitude (Wellington)
dx = abs(x1-x0)*67097
dy = abs(y1-y0)*111200
distance = math.sqrt(math.pow(dx,2) + math.pow(dy,2))
return distance
def fill_gap(row, rows_to_add):
edit.startOperation()
insert_cursor = arcpy.da.InsertCursor(table, fields)
f.write("Insert")
i = 1
f.write(str(rows_to_add))
while i < rows_to_add+1:
to_insert = (row[0], row[1], row[2]+5*i, row[3])
insert_cursor.insertRow(to_insert)
i += 1
edit.stopOperation()
def process_table():
#Creates an update cursor
rows = arcpy.da.SearchCursor(table, fields)
prev_t = 0
prev_x = 0
prev_y = 0
total_gaps = 0
over_1 = 0
over_5 = 0
over_100_meters = 0
delta_t = 0
first = True
dic = {}
extremes_lst = []
fid_lst = []
#Iterator
for row in rows:
#the time
hms = row[2]
#If not the first pass
if first != True:
if hms - prev_t > 5:
distance = calculate_distance(prev_x, row[1], prev_y, row[0])
delta_t = hms-prev_t
f.write(str(delta_t)+"\n")
delta_t_str = str(delta_t/60) + " minutes " + str(delta_t%60) + " seconds "
rows_to_add = delta_t/5 - 1
fid_lst.append(prev_row)
fill_gap(prev_row, rows_to_add)
first = False
prev_t = hms
prev_x = row[1]
prev_y = row[0]
prev_row = row
workspace = os.path.dirname(table)
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, False)
process_table()
edit.stopEditing(True)
Thanks, much needed formatting tutorial. It now adds rows with the values that I want, but I'd like them to be added directly beneath the row I am sending to fill_gap.