我目前有一个数据帧,其中包含播放器名称,时间,纬度,经度和速度(以m / s为单位)。我想绘制一个球员的热图,但是我的问题是将纬度和经度转换为足球场图像上的位置x和位置y。
目前可以像下面的图像1那样绘制纬度和经度路径,但是值不是在平坦的足球场上,而是在与图像2类似的方向上
理想情况下,我想将lat和lon值转换为足球场上的位置x和位置y,并输出类似于以下内容的图。
我到目前为止所做的尝试如下,我从Google地图中获取了最大和最小(纬度和经度)值,以获取左上,左下,右上和右下的音高。计算出节距长度和节距宽度,然后使用以下公式将两个新列添加到df中。但这并不能解决我的问题,我非常感谢您的任何想法。
#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))
不是最好的解决方案,但是我能够在进行以下必要的转换sf
:
首先,我获取您的俯仰坐标并将其转换为平面坐标(27700),并随机抽取一个样本来表示您的gps数据:
library(dplyr)
library(sf)
pts <- data.frame(lat = c(top_left_lat,bottom_left_lat, bottom_right_lat, top_right_lat),
lng = c(top_left_lon,bottom_left_lon, bottom_right_lon, top_right_lon )) %>%
st_as_sf(coords = c('lng', 'lat'), crs = 4326) %>%
st_transform(27700)
poly <- pts %>%
st_union() %>%
st_convex_hull()
centroid <- st_centroid(poly)
set.seed(2020)
path <- st_sample(poly, 20) %>%
st_union() %>%
st_cast("LINESTRING")
st_transform(poly, 4326) %>%
ggplot() +
geom_sf(fill = NA, col = "black") +
geom_sf(data = st_transform(path,4326), col = 'red')
然后计算俯仰角,然后将数据旋转该角度(rot
在sf
小插图中找到函数):
# get angle of pitch
se <- st_coordinates(pts[1,])
ne <- st_coordinates(pts[2,])
dy <- ne[2] - se[2]
dx <- ne[1] - se[1]
angle = atan(dy/dx)
# rotate
rot <- function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2)
poly2 <- (poly - centroid) * rot(angle) + centroid
path2 <- (path - centroid) * rot(angle)+ centroid
最后转换为在(0,0)处左下角:
# translate to 0,0
xmin <- st_bbox(poly2)[1]
ymin <- st_bbox(poly2)[2]
pitch <- poly2 - c(xmin, ymin)
positions <- path2 - c(xmin, ymin)
## fetch the x,y coordinates:
position_xy <- positions %>%
st_coordinates() %>%
as.data.frame()
pitch_xy <- pitch %>%
st_cast("LINESTRING") %>%
st_coordinates %>%
as.data.frame
position_xy %>%
ggplot() +
geom_path(aes(x = X, y= Y), col= 'red') +
geom_path(data = pitch_xy ,aes(x = X, y = Y))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句