저는 Python을 처음 사용하고 ArcGIS 워크 플로우를위한 스크립트를 만들기 시작했습니다. 타임 스탬프 필드에서 "Hours"이중 숫자 필드를 생성하기 위해 코드 속도를 높이는 방법이 궁금합니다. DNR Garmin이 생성 한 트랙 포인트 로그 (빵 부스러기 트레일) 모양 파일부터 시작하여 각 트랙 포인트 레코드가 촬영 된 시간에 대한 LTIME 타임 스탬프 필드 (텍스트 필드, 길이 20)를 사용합니다. 스크립트는 각 연속 타임 스탬프 ( "LTIME") 사이의 시간 차이를 계산하여 새 필드 ( "Hours")에 넣습니다.
그런 식으로 돌아가서 특정 지역 / 폴리곤에서 보낸 시간을 요약 할 수 있습니다. 주요 부분은 다음과 print "Executing getnextLTIME.py script..."
같습니다 코드 는 다음과 같습니다.
# ---------------------------------------------------------------------------
#
# Created on: Sept 9, 2010
# Created by: The Nature Conservancy
# Calculates delta time (hours) between successive rows based on timestamp field
#
# Credit should go to Richard Crissup, ESRI DTC, Washington DC for his
# 6-27-2008 date_diff.py posted as an ArcScript
'''
This script assumes the format "month/day/year hours:minutes:seconds".
The hour needs to be in military time.
If you are using another format please alter the script accordingly.
I do a little checking to see if the input string is in the format
"month/day/year hours:minutes:seconds" as this is a common date time
format. Also the hours:minute:seconds is included, otherwise we could
be off by almost a day.
I am not sure if the time functions do any conversion to GMT,
so if the times passed in are in another time zone than the computer
running the script, you will need to pad the time given back in
seconds by the difference in time from where the computer is in relation
to where they were collected.
'''
# ---------------------------------------------------------------------------
# FUNCTIONS
#----------------------------------------------------------------------------
import arcgisscripting, sys, os, re
import time, calendar, string, decimal
def func_check_format(time_string):
if time_string.find("/") == -1:
print "Error: time string doesn't contain any '/' expected format \
is month/day/year hour:minutes:seconds"
elif time_string.find(":") == -1:
print "Error: time string doesn't contain any ':' expected format \
is month/day/year hour:minutes:seconds"
list = time_string.split()
if (len(list)) <> 2:
print "Error time string doesn't contain and date and time separated \
by a space. Expected format is 'month/day/year hour:minutes:seconds'"
def func_parse_time(time_string):
'''
take the time value and make it into a tuple with 9 values
example = "2004/03/01 23:50:00". If the date values don't look like this
then the script will fail.
'''
year=0;month=0;day=0;hour=0;minute=0;sec=0;
time_string = str(time_string)
l=time_string.split()
if not len(l) == 2:
gp.AddError("Error: func_parse_time, expected 2 items in list l got" + \
str(len(l)) + "time field value = " + time_string)
raise Exception
cal=l[0];cal=cal.split("/")
if not len(cal) == 3:
gp.AddError("Error: func_parse_time, expected 3 items in list cal got " + \
str(len(cal)) + "time field value = " + time_string)
raise Exception
ti=l[1];ti=ti.split(":")
if not len(ti) == 3:
gp.AddError("Error: func_parse_time, expected 3 items in list ti got " + \
str(len(ti)) + "time field value = " + time_string)
raise Exception
if int(len(cal[0]))== 4:
year=int(cal[0])
month=int(cal[1])
day=int(cal[2])
else:
year=int(cal[2])
month=int(cal[0])
day=int(cal[1])
hour=int(ti[0])
minute=int(ti[1])
sec=int(ti[2])
# formated tuple to match input for time functions
result=(year,month,day,hour,minute,sec,0,0,0)
return result
#----------------------------------------------------------------------------
def func_time_diff(start_t,end_t):
'''
Take the two numbers that represent seconds
since Jan 1 1970 and return the difference of
those two numbers in hours. There are 3600 seconds
in an hour. 60 secs * 60 min '''
start_secs = calendar.timegm(start_t)
end_secs = calendar.timegm(end_t)
x=abs(end_secs - start_secs)
#diff = number hours difference
#as ((x/60)/60)
diff = float(x)/float(3600)
return diff
#----------------------------------------------------------------------------
print "Executing getnextLTIME.py script..."
try:
gp = arcgisscripting.create(9.3)
# set parameter to what user drags in
fcdrag = gp.GetParameterAsText(0)
psplit = os.path.split(fcdrag)
folder = str(psplit[0]) #containing folder
fc = str(psplit[1]) #feature class
fullpath = str(fcdrag)
gp.Workspace = folder
fldA = gp.GetParameterAsText(1) # Timestamp field
fldDiff = gp.GetParameterAsText(2) # Hours field
# set the toolbox for adding the field to data managment
gp.Toolbox = "management"
# add the user named hours field to the feature class
gp.addfield (fc,fldDiff,"double")
#gp.addindex(fc,fldA,"indA","NON_UNIQUE", "ASCENDING")
desc = gp.describe(fullpath)
updateCursor = gp.UpdateCursor(fullpath, "", desc.SpatialReference, \
fldA+"; "+ fldDiff, fldA)
row = updateCursor.Next()
count = 0
oldtime = str(row.GetValue(fldA))
#check datetime to see if parseable
func_check_format(oldtime)
gp.addmessage("Calculating " + fldDiff + " field...")
while row <> None:
if count == 0:
row.SetValue(fldDiff, 0)
else:
start_t = func_parse_time(oldtime)
b = str(row.GetValue(fldA))
end_t = func_parse_time(b)
diff_hrs = func_time_diff(start_t, end_t)
row.SetValue(fldDiff, diff_hrs)
oldtime = b
count += 1
updateCursor.UpdateRow(row)
row = updateCursor.Next()
gp.addmessage("Updated " +str(count+1)+ " rows.")
#gp.removeindex(fc,"indA")
del updateCursor
del row
except Exception, ErrDesc:
import traceback;traceback.print_exc()
print "Script complete."