-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapely_football.py
79 lines (63 loc) · 2.41 KB
/
shapely_football.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
'''
Extend shapely geometries to provide a simpler syntax for the operation commonly performed in football analytic
'''
from shapely.geometry import Point as pt
from shapely.geometry import Polygon as pol
import numpy as np
meters_per_yard = 0.9144 # unit conversion from yards to meters
class Point(pt):
def __init__(self, coords):
super().__init__(coords)
@property
def xy(self):
"""Separate arrays of X and Y coordinate values
Example:
>>> xy = Point(0, 0).xy
"""
# This returns an array.array.
x,y = self.coords.xy
x = list(x)
y = list(y)
return np.array([x[0],y[0]])
def __add__(self, other):
'''
Define the addition of a point with another point or np.array
:param other: Point or array
:return: Depends on other. If other is a Point, returns a Point. If other is anything else, returns a np.array
'''
if isinstance(other, Point):
return Point([self.x + other.x, self.y + other.y])
elif other is None or np.isnan(other).sum():
return np.nan
else:
# The strategy is to delegate all other type control to numpy.
return self.xy + other
def __sub__(self, other):
'''
Define the substraction of a point with another point or np.array
:param other: Point or array
:return: Depends on other. If other is a Point, returns a Point. If other is anything else, returns a np.array
'''
# You can do this with pd.isna(), but I don't want to introduce pd for this
if other is None or np.isnan(other).sum():
return np.nan
elif isinstance(other, Point):
return Point([self.x - other.x, self.y - other.y])
else:
return self.xy - other
def __mul__(self, other):
return(Point(self.xy * other))
def __str__(self):
return(f"Point at {str(self.xy)}")
class SubPitch(pol):
'''
We create the pitch as a Multipolygon composed of rectangles, named SubPitch. These rectangles are a
discretization of the pitch continuity. They are used in functions that calculate something continuosly on the
surface of the pitch
'''
def __init__(self, coords):
super().__init__(coords)
@property
def centroid(self):
x, y = super().centroid.xy
return Point([list(x)[0], list(y)[0]])