Boundary polygon of lat lon collection

耗尽温柔 提交于 2020-01-04 11:02:41

问题


I have a table containing all the latitudes and longitudes of some locations in a city called queryResult and I do the following:

1 - Get the Raster map of the city[Blackpool for instance]

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)

and this will return the following points on the map

Now I want to draw the outer boundary [city border line] of all these latitude and longitudes similar to the following expected result

Update 1 : Providing input data and applying suggested ahull solution:

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

I applied the ahull solution suggested by @spacedman and @cuttlefish44 and got the following result which is far different than the expected polygon:

You can download the .csv file containing all latitudes and longitudes from the following link : Blackpool Lat,Lon

Googles suggested area boundary looks like the following :


回答1:


If you don't want a simple convex hull (and the polygon you've drawn is far from convex) then look at alpha-shapes in the alphahull package.

I wrote an example of how to get a polygon from an alpha-shape using that package and some points that make up a complex Norway boundary:

http://rpubs.com/geospacedman/alphasimple

You should be able to follow that to get a polygon for your data, and it might even be simpler now since that was a few years ago.




回答2:


Here's a reproducible example of how to use chull to calculate a convex hull solution to this. I just generate some random points for queryResult, as you did not provide data.

If you prefer a concave hull boundary, then see 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]),])




回答3:


I suppose queryResult is x and y datasets. As far as I see, your boundary isn't convex hull, so I used alphahull package.

  ## 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

  ## 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)


来源:https://stackoverflow.com/questions/40179966/boundary-polygon-of-lat-lon-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!