在R语言中计算两个坐标之间的间隔点

3

我有一系列坐标,从中采集了数据,下面是其中一个在地图上的实例:

library(ggplot2)
library(ggmap)
library(dismo)
lon <- c(-64.723946, -64.723754, -64.723808, -64.724004)
lat <- c(32.350843, 32.350783, 32.350618, 32.350675)
df <- as.data.frame(cbind(lon,lat))

mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 18, maptype = "satellite", scale = 2)
ggmap(mapgilbert) +
  geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
  guides(fill=FALSE, alpha=FALSE, size=FALSE) +
  scale_x_continuous(limits = c(-64.725, -64.723), expand = c(0,0)) +
  scale_y_continuous(limits = c(32.350, 32.351), expand = c(0,0))

这是它生成的图像:

map

我想在地图上的南北和东西两点之间画一条线,形成一个十字架。但我希望这条线由点组成,而不仅仅是一条线,所以我需要计算出这些点的坐标。这些线大约有30米长,我需要每米一个点。我感觉 sp 包可能可以实现这样的功能,但我无法弄清楚如何做到。谢谢,Kez

尝试查看 oce::geodDist()。它可能也会有所帮助。 - CephBirk
1个回答

2
重新排列你的数据框,你真正需要的只是seq。这里是 Hadley 风格的代码,但你可以手动构建它。
library(dplyr)
library(tidyr)

              # group rows by opposite pairs
df2 <- df %>% group_by(group = lon %in% range(lon)) %>% 
    # summarize lat and lon for each group into a list of a sequence from the first to the second
    summarise_each(funs(list(seq(.[1], .[2], length.out = 10)))) %>% 
    # expand list columns with tidyr::unnest
    unnest()

head(df2)
# Source: local data frame [6 x 3]
# 
#   group       lon      lat
#   (lgl)     (dbl)    (dbl)
# 1 FALSE -64.72395 32.35084
# 2 FALSE -64.72393 32.35082
# 3 FALSE -64.72392 32.35079
# 4 FALSE -64.72390 32.35077
# 5 FALSE -64.72388 32.35074
# 6 FALSE -64.72387 32.35072

# all I changed here is data = df2
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 18, maptype = "satellite", scale = 2)
ggmap(mapgilbert) +
    geom_point(data = df2, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
    guides(fill=FALSE, alpha=FALSE, size=FALSE) +
    scale_x_continuous(limits = c(-64.725, -64.723), expand = c(0,0)) +
    scale_y_continuous(limits = c(32.350, 32.351), expand = c(0,0))

plot with x of points


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接