首页 > 解决方案 > 将 GPS 经纬度跟踪数据转换为足球场上的位置 x 和位置 y?

问题描述

我目前有一个数据框,其中包含玩家姓名、时间、纬度、经度和速度(以 m/s 为单位)。我想绘制一个球员热图,但我的问题是将纬度和经度转换为足球场图像上的位置 x 和位置 y。

目前可以绘制出如下图 1 的经纬度路径,但值不是在平坦足球场的方向上,而是在类似于图 2 的方向上

在此处输入图像描述

在此处输入图像描述

理想情况下,我想将 lat 和 lon 值转换为足球场上的位置 x 和位置 y,并输出类似于下图的图。

在此处输入图像描述

到目前为止我所做的尝试如下,我从谷歌地图中获取了左上角、左下角、右上角和右下角的间距的最大值​​和最小值(纬度和经度)。计算间距长度和间距宽度,然后使用以下公式将两个新列添加到 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))  

标签: rdataframegoogle-mapsggplot2gps

解决方案


不是最好的解决方案,但我能够在以下方面进行必要的转换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)) 

在此处输入图像描述


推荐阅读