-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_player_movement.py
332 lines (282 loc) · 16.4 KB
/
map_player_movement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#-------------------------------------------------------------------------------
# Name: map_player_movement.py
# Purpose: This script creates maps of NBA player movement given a gameid and
# series of events. These maps will be analagous to terrain maps of
# road density maps that are layers common to GIS.
#
# Author: Gregory Brunner, [email protected], [email protected]
#
# Created: 10/11/2015
# Copyright:(c) Gregory Brunner, 2015
# Licence: What do I put here?
#-------------------------------------------------------------------------------
from __future__ import division
import requests
import os
import datetime
import math
import time
import re
import arcpy
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
fields = ('SHAPE@XY', 'TEAM_ID', 'PLAYER_ID', 'EVENT_ID','LOC_X', 'LOC_Y',
'RADIUS', 'MOMENT', 'GAME_CLOCK', 'SHOT_CLOCK', 'GAME_TIME', 'UNIX_TIME', 'QUARTER','VELOCITY')
def main(gameid, eventid):
event_url = 'http://stats.nba.com/stats/locations_getmoments/?eventid=%s&gameid=%s' % (eventid, gameid)
response = requests.get(event_url)
if response.status_code == 400:
print('Data not available for this filter.')
return -1,-1,-1,-1
response.json().keys()
home = response.json()["home"]
visitor = response.json()["visitor"]
moments = response.json()["moments"]
gamedate = response.json()['gamedate']
date = create_datestamp(gamedate)
team_dict = {}
team_dict[home['teamid']] = home['name']
team_dict[visitor['teamid']] = visitor['name']
team_dict[1] = 'Basketball'
d = {}
player_team_dict = {}
d[1] = 'Basketball'
player_team_dict[1] = 1
for h in home['players']:
d[h['playerid']] = h['firstname'] + ' ' + h['lastname']
player_team_dict[h['playerid']] = home['teamid']
for v in visitor['players']:
d[v['playerid']] = v['firstname'] + ' ' + v['lastname']
player_team_dict[v['playerid']] = visitor['teamid']
coords = []
for moment in moments:
quarter = moment[0]
comp_time = moment[1]
for player in moment[5]:
player.extend((moments.index(moment), moment[2], moment[3]))
clock_time = create_timestamp(quarter, date, player[6])
ct = datetime.datetime.strftime(clock_time, '%Y/%m/%d %H:%M:%S.%f')[:-5]
comp_time_string = datetime.datetime.fromtimestamp(comp_time/1000.0).strftime('%Y/%m/%d %H:%M:%S.%f')[:-5]
velocty = 1.00 #placeholder for velocity (ft/s) calculations
if player[1] == -1:
player_data = (1, 1, eventid, player[2], player[3], player[4], player[5], player[6], player[7], ct, comp_time_string, quarter, velocty)
else:
player_data = (player[0], player[1], eventid, player[2], player[3], player[4], player[5], player[6], player[7], ct, comp_time_string, quarter, velocty)
if quarter <= 2:
coord = ([10*(player[3]-25), 10*(player[2]-5.25)])
else:
coord = ([-10*(player[3]-25), 10*(83.5 -(player[2]-5.25))])
coords.append((coord,)+player_data)
return coords, d, team_dict, player_team_dict
def create_feature_class(output_gdb, output_feature_class):
feature_class = os.path.basename(output_feature_class)
if not arcpy.Exists(output_gdb):
arcpy.CreateFileGDB_management(os.path.dirname(output_gdb),os.path.basename(output_gdb))
if not arcpy.Exists(output_feature_class):
#"PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]]", transform_method="", in_coor_system="GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"
arcpy.CreateFeatureclass_management(output_gdb,feature_class,"POINT","#","DISABLED","DISABLED", "PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]]","#","0","0","0")
arcpy.AddField_management(output_feature_class,"TEAM_ID","LONG", "", "", "")
arcpy.AddField_management(output_feature_class,"PLAYER_ID","LONG", "", "", "")
arcpy.AddField_management(output_feature_class,"EVENT_ID","SHORT", "", "", "")
arcpy.AddField_management(output_feature_class,"LOC_X","DOUBLE", "", "", "")
arcpy.AddField_management(output_feature_class,"LOC_Y","DOUBLE", "", "", "")
arcpy.AddField_management(output_feature_class,"RADIUS","DOUBLE", "", "", "")
arcpy.AddField_management(output_feature_class,"MOMENT","LONG", "", "", "")
arcpy.AddField_management(output_feature_class,"GAME_CLOCK","DOUBLE", "", "", "")
arcpy.AddField_management(output_feature_class,"SHOT_CLOCK","DOUBLE", "", "", "")
#arcpy.AddField_management(output_feature_class,"TIME", "DATE")
arcpy.AddField_management(output_feature_class,"GAME_TIME", "TEXT", "", "", 30)
arcpy.AddField_management(output_feature_class,"UNIX_TIME", "TEXT", "", "", 30)
arcpy.AddField_management(output_feature_class,"QUARTER","SHORT", "", "", "")
arcpy.AddField_management(output_feature_class,"VELOCITY","DOUBLE", "", "", "")
def populate_feature_class(rowValues, output_feature_class):
c = arcpy.da.InsertCursor(output_feature_class,fields)
for row in rowValues:
c.insertRow(row)
del c
def create_team_subtype(gdb, fc, subtype_dict):
# Process: Set Subtype Field...
arcpy.SetSubtypeField_management(os.path.join(gdb,fc), "TEAM_ID")
# use a for loop to cycle through the dictionary
for code in subtype_dict:
arcpy.AddSubtype_management(os.path.join(gdb,fc), code, subtype_dict[code])
def create_player_domain(gdb, fc, line_fc, player_dict):
domName = "Players"
inField = "PLAYER_ID"
# Process: Create the coded value domain
arcpy.CreateDomain_management(gdb, domName, "Player Names", "TEXT", "CODED")
# Process: Add valid material types to the domain
#use a for loop to cycle through all the domain codes in the dictionary
for code in player_dict:
arcpy.AddCodedValueToDomain_management(gdb, domName, code, player_dict[code])
# Process: Constrain the material value of distribution mains
arcpy.AssignDomainToField_management(fc, inField, domName)
##print(line_fc)
#arcpy.AssignDomainToField_management(line_fc, inField, domName)
def create_datestamp(gamedate):
date = datetime.datetime.strptime(gamedate,'%Y-%m-%d')
return date
def create_timestamp(quarter, gamedate, seconds):
m,s = divmod(720-seconds, 60)
ms = round((s-int(s))*100)
t = datetime.time(int(quarter), int(m), math.floor(s), ms*10000)
dt = datetime.datetime.combine(gamedate, t)
return dt
def unique(lst):
return set(lst)
def get_unique_events(fc, field):
init_event_num = -1
events = []
with arcpy.da.SearchCursor(fc, field) as cursor:
for row in cursor:
if row[0] != init_event_num:
events.append(row[0])
init_event_num = row[0]
unique_events = unique(events)
return unique_events
def create_line_features(in_fc, gdb, unique_event_list, dictionary):
for event in unique_event_list:
event_lines = os.path.join(gdb, 'event_'+str(event))
if arcpy.Exists(event_lines) == False:
print('Creating lines for event ' + str(event))
layer = 'in_memory\\event_lyr_' + str(event)
query = '"EVENT_ID" = ' + str(event)
arcpy.MakeFeatureLayer_management(in_fc,layer,query,"#","#")
arcpy.PointsToLine_management(layer, event_lines, "PLAYER_ID", "GAME_TIME", "NO_CLOSE")
arcpy.AddField_management(os.path.join(gdb, 'event_'+str(event)),"TEAM_ID","LONG", "", "", "")
arcpy.AddField_management(os.path.join(gdb, 'event_'+str(event)),"QUARTER","SHORT", "", "", "")
arcpy.AddField_management(os.path.join(gdb, 'event_'+str(event)), "START_TIME", "TEXT", "", "", 30)
#Get Start time of lines
with arcpy.da.SearchCursor(layer, ('GAME_TIME', 'QUARTER')) as cursor:
first_time = cursor.next()
print(first_time[0])
#Add start time of lines to line features
with arcpy.da.UpdateCursor(os.path.join(gdb, 'event_'+str(event)), ('START_TIME', 'PLAYER_ID','TEAM_ID', 'QUARTER')) as cursor:
for row in cursor:
row[0] = first_time[0]
row[2] = dictionary[row[1]]
row[3] = first_time[1]
cursor.updateRow(row)
def append_lines(gdb, output_line_fc):
arcpy.env.workspace = gdb
fc_list = arcpy.ListFeatureClasses('*',"Polyline")
arcpy.CreateFeatureclass_management(gdb, os.path.basename(output_line_fc), "POLYLINE", fc_list[0],"DISABLED","DISABLED","PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]];-20037700 -30241100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision","#","0","0","0")
arcpy.Append_management(fc_list, output_line_fc, 'NO_TEST', '', '')
for fc in fc_list:
arcpy.Delete_management(fc)
def create_line_density_raster(gdb, in_fc, team_dict):
for team in team_dict:
t = team_dict[team]
t.replace(' ', '_')
print('Creating line density raster for ' + t)
layer = 'in_memory\\team_lyr_' + str(t)
query = '"TEAM_ID" = ' + str(team)
arcpy.MakeFeatureLayer_management(os.path.join(gdb,in_fc),layer,query,"#","#")
arcpy.gp.LineDensity_sa(layer, "NONE", os.path.join(gdb, t), "5", "5", "SQUARE_MAP_UNITS")
def create_line_density_raster_per_quarter(gdb, in_fc, team_dict, quarters):
for team in team_dict:
t = team_dict[team]
t.replace(' ', '_')
for quarter in quarters:
out_fc = os.path.join(gdb, t + '_Quarter_'+ str(quarter))
if arcpy.Exists(out_fc) == False:
print('Creating line density raster for ' + t + ' during quarter ' + str(quarter))
print(out_fc)
layer = 'in_memory\\team_lyr_' + str(t)+ str(quarter)
query = '"TEAM_ID" = ' + str(team) + 'AND "QUARTER" = ' + str(quarter)
arcpy.MakeFeatureLayer_management(in_fc,layer,query,"#","#") #os.path.join(gdb,in_fc)
arcpy.gp.LineDensity_sa(layer, "NONE", out_fc, "5", "5", "SQUARE_MAP_UNITS")
def find_mean_center_of_movement(gdb, in_fc, team_dict):
print('Creating mean center of movement for entire game')
layer = 'in_memory\\mean_center_team_lyr'
#query = '"TEAM_ID" = ' + str(team)
arcpy.MakeFeatureLayer_management(in_fc,layer,"#","#","#")
arcpy.MeanCenter_stats(layer, os.path.join(gdb, 'MeanCenterOfAction'), '#','#','#')
def find_mean_center_of_movement_per_quarter(gdb, in_fc, team_dict, quarters):
for quarter in quarters:
print('Creating mean center of movement for quarter ' + str(quarter))
layer = 'in_memory\\mean_center_team_lyr_' + str(quarter)
query = '"QUARTER" = ' + str(quarter)
arcpy.MakeFeatureLayer_management(in_fc,layer,query,"#","#")
arcpy.MeanCenter_stats(layer,os.path.join(gdb, 'MeanCenterOfAction_Quarter_'+ str(quarter)), '#', '#', '#')
def create_point_density_raster(in_points, gdb, player_list, player_dictionary):
# Create the Neighborhood Object
radius = 10
neighborhood = arcpy.sa.NbrCircle(radius, "MAP")
for player in player_list:
query = '"PLAYER_ID" = ' + str(player)
ras_name = 'player_movement_rasters_'+player_dictionary[player]
print('Creating ' + str(ras_name))
ras_name.replace(' ', '_')
ras_name = re.sub('[^0-9a-zA-Z]+', '_', ras_name)
out_ras = os.path.join(gdb,ras_name)
if arcpy.Exists(out_ras) == True:
player_lyr = 'in_memory\\'+str(player)+'movement_map'
print('Making feature layer.')
arcpy.MakeFeatureLayer_management(in_points, player_lyr, query, '#', '#')
print('Getting point density raster '+ ras_name)
arcpy.gp.PointDensity_sa(player_lyr, "NONE", out_ras, 2.5, "Circle 10 MAP", "SQUARE_KILOMETERS") #"Circle 10 MAP"
if __name__ == '__main__':
t0 = time.clock()
game_id = '0021500234'#'0021500177'#'0021400015'
first_event_num = 1 #346 #1
last_event_num = 550 #564 #350 #122
output_gdb = r"C:\PROJECTS\R&D\NBA\Player_Movement_Maps_OKC_DET.gdb"
output_line_fc = os.path.join(output_gdb, 'Game_as_lines_' + game_id + '_Event_' + str(first_event_num) + '_' + str(last_event_num))
output_point_fc = os.path.join(output_gdb, 'Game_as_points_' + game_id + '_Event_' + str(first_event_num) + '_' + str(last_event_num))
output_point_fc_sorted = output_point_fc+'_Sorted'
first_event_data, player_dictionary, team_dictionary, player_team_dictionary = main(game_id, first_event_num)
## print('Creating feature class.')
## create_feature_class(output_gdb, output_point_fc)
##
## for event in range(first_event_num, last_event_num+1):
## print('Getting Event ' + str(event))
## print('Getting data.')
## event_data, player_dict, team_dict, player_team_dict = main(game_id, event)
## if event_data != -1:
## print('Populating features.')
## populate_feature_class(event_data, output_point_fc)
##
## print('Creating player name domain.')
## create_player_domain(output_gdb, output_point_fc, output_line_fc, player_dictionary)
## print('Creating team subtype.')
## create_team_subtype(output_gdb, output_point_fc, team_dictionary)
##
## arcpy.CopyFeatures_management(output_point_fc, output_point_fc+'_NoFilteringOrSorting')
##
## print('Deleting Identical Records.')
## #arcpy.DeleteIdentical_management(output_feature_class, "Shape;TEAM_ID;PLAYER_ID;LOC_X;LOC_Y;RADIUS;GAME_CLOCK;SHOT_CLOCK;TIME", "", "0")
## arcpy.DeleteIdentical_management(output_point_fc, "TEAM_ID;PLAYER_ID;GAME_CLOCK;GAME_TIME", "", "0")
##
## #Sort so we don't get artifacts in lines and playback.
## arcpy.Sort_management(output_point_fc, output_point_fc_sorted, "QUARTER ASCENDING;GAME_CLOCK DESCENDING", "UR")
event_list = get_unique_events(output_point_fc_sorted, 'EVENT_ID')
print(event_list)
quarter_list = get_unique_events(output_point_fc_sorted, 'QUARTER')
print(quarter_list)
player_list = get_unique_events(output_point_fc_sorted, 'PLAYER_ID')
print(player_list)
print(player_dictionary)
create_point_density_raster(output_point_fc_sorted, output_gdb, player_list, player_dictionary)
## print('Creating Line Features')
## create_line_features(output_point_fc_sorted, output_gdb, event_list, player_team_dictionary)
##
## print('Putting all the lines in one feature class.')
## append_lines(output_gdb, output_line_fc)
##
## print('Creating team subtype for line features.')
## create_team_subtype(output_gdb, output_line_fc, team_dictionary)
##
## print('Running line density analysis.')
## create_line_density_raster(output_gdb, output_line_fc, team_dictionary)
##
## print('Running line density analysis per quarter.')
## create_line_density_raster_per_quarter(output_gdb, output_line_fc, team_dictionary, quarter_list)
##
## print('Finding mean center of action.')
## find_mean_center_of_movement(output_gdb, output_point_fc_sorted, team_dictionary)
##
## print('Finding mean center per quarter.')
## find_mean_center_of_movement_per_quarter(output_gdb, output_point_fc_sorted, team_dictionary, quarter_list)
t1 = (time.clock() - t0)/60
print("This process took " + str(t1) + " minutes to run.")