I currently have a dataframe containing player name, time, latitude, longitude and speed in m/s. I want to map out a players heat map, but my issue is with converting latitude and longitude to a position x and position y on a football pitch image.
Currently can map out the lat and lon path like image 1 below but the values are not in the direction of a flat football pitch but in the direction similar to image 2
I ideally would like to convert the lat and lon values to a position x and position y on a football pitch and output a plot similar to the below.
The attempt I have taken so far is below, I took the max and min(lat and lon) values from google maps for the pitch for the top left, bottom left, top right and bottom right. Calculated the pitch length and pitch width, then added two new columns to the df using the below formulas. But this has not resolved my problem any ideas I would be very much grateful thanks.
#pitch dimensions taken from google
top_left_lat <- 51.662233
top_left_lon <- -0.273183
top_right_lat <- 51.662518
top_right_lon <- -0.272164
bottom_left_lat <- 51.661337
bottom_left_lon <- -0.272539
bottom_right_lat <- 51.661630
bottom_right_lon <- -0.271528
#calculate pitch length
pitch_length <- acos(cos(deg2rad(90 - top_left_lat)) * cos(deg2rad(90 - bottom_left_lat))
+ sin(deg2rad(90 - top_left_lat)) * sin(deg2rad(90 - bottom_left_lat))
* cos(deg2rad(top_left_lon - bottom_left_lon))) * 6371
pitch_length
#calculate pitch width
pitch_width <- acos(cos(deg2rad(90 - top_left_lat)) * cos(deg2rad(90 - top_right_lat))
+ sin(deg2rad(90 - top_left_lat)) * sin(deg2rad(90 - top_right_lat))
* cos(deg2rad(top_left_lon - top_right_lon))) * 6371
pitch_width
#convert lat lon to pos x and y on a pitch
a <- mutate(a, posX = (pitch_width/360)*(180 + a$Lon))
a <- mutate(a, posY = (pitch_length/180)*(90 - a$Lat))




