lat lon集合的边界多边形

时间:2016-10-21 15:03:34

标签: r ggplot2 ggmap maptools rgooglemaps

我有一张表格,其中包含一个名为queryResult的城市中某些地点的纬度和经度,我会执行以下操作:

1 - 获取城市的光栅地图[例如布莱克浦]

cityMapRaster = get_map(location = 'Blackpool', zoom = 12, source = 'google', maptype = 'roadmap')

dataToShow <- ggmap(cityMapRaster) + geom_point(aes(x = Longitude, y = Latitude), data = queryResult, alpha = .5, color = "darkred", size = 1)

print(dataToShow)

这将返回地图上的以下几点

enter image description here

现在我想绘制所有这些纬度和经度的外边界[城市边界线],类似于跟随预期结果

Blackpool city polygon

更新1 :提供输入数据并应用建议的 ahull 解决方案:

ggmap(cityMapRaster) + geom_point(aes(x = Longitude, y = Latitude), data = queryResult, alpha = .5, color = "darkred") + ahull.gg

我应用了@spacedman和@ cuttlefish44建议的ahull解决方案,得到的结果与预期的多边形大不相同:

Applying ahull to data

您可以从以下链接下载包含所有纬度和经度的.csv文件:Blackpool Lat,Lon

Googles suggested area boundary looks like the following :

enter image description here

3 个答案:

答案 0 :(得分:1)

如果您不想要一个简单的凸包(并且您绘制的多边形远离凸面),那么请查看alphahull包中的alpha形状。

我写了一个例子,说明如何使用该包以及构成复杂挪威边界的一些点从alpha形状获取多边形:

http://rpubs.com/geospacedman/alphasimple

enter image description here

你应该能够按照这个来获得数据的多边形,现在甚至可能更简单,因为那是几年前的事情。

答案 1 :(得分:0)

这是一个可重复的示例,说明如何使用chull计算凸包解决方案。我只为queryResult生成一些随机点,因为您没有提供数据。

如果您更喜欢凹形船体边界,请参阅the answer from @Spacedman

library(ggmap)
cityMapRaster = get_map(location = 'Blackpool', zoom = 12, source = 'google', maptype = 'roadmap')
extent = attr(cityMapRaster, "bb")
queryResult = data.frame(Longitude = rnorm(200, as.numeric(extent[2] + extent[4])/2, 0.01),
                         Latitude = rnorm(200, as.numeric(extent[1] + extent[3])/2, 0.02))

boundary = chull(as.matrix(queryResult))

ggmap(cityMapRaster) +
  geom_point(aes(x = Longitude, y = Latitude), 
             data = queryResult, alpha = .5, color = "darkred", size = 2) +
  geom_path(aes(x = Longitude, y = Latitude), data = queryResult[c(boundary, boundary[1]),])

enter image description here

答案 2 :(得分:0)

我认为queryResult是x和y数据集。据我所知,你的边界不是凸包,所以我使用了alphahull包。

  ## example `queryResult`
set.seed(1)
df <- data.frame(Longitude = runif(200, -3.05, -2.97), Latitude = rnorm(200, 53.82, 0.02))

library(alphahull)

ahull.obj <- ahull(df, alpha = 0.03)
plot(ahull.obj)   # to check

  # ahull_track() returns the output as a list of geom_path objs
ahull.gg <- ahull_track(df, alpha=0.03, nps = 1000)
  ## change graphic param
for(i in 1:length(ahull.gg)) ahull.gg[[i]]$aes_params$colour <- "green3"

ggmap(cityMapRaster) + 
  geom_point(aes(x = Longitude, y = Latitude), data = df, alpha = .5, color = "darkred") +
  ahull.gg

enter image description here

  ## if you like not curve but linear
ashape.obj <- ashape(df, alpha = 0.015)
plot(ashape.obj)  # to check
ashape.df <- as.data.frame(ashape.obj$edge[,c("x1", "x2", "y1", "y2")])

ggmap(cityMapRaster) + 
  geom_point(aes(x = Longitude, y = Latitude), data = df, alpha = .5, color = "darkred") +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2), data = ashape.df, colour="green3", alpha=0.8)