如何删除在 R 中的 osmdata 对象中使用的绘制其他空间对象(如“$osm_polygons”和“$multipolygons”)的“$osm_points”

How to remove '$osm_points' that are used plot other spatial objects like '$osm_polygons' and '$multipolygons' within osmdata objects in R

我想知道有多少兴趣点(POI,例如商店、大学、体育设施)落在布里斯班各个巴士站的 400 米范围内。为了做到这一点,我一直在使用 R 中的 osmdata 包从 Open Street Map 中提取特征。我的计划是计算落在每个公交车站 400 米缓冲区内的点数,以及计算多边形的数量与所述缓冲区相交。

但是,我注意到 osmdata 中 但不是全部'$osm_points'对象似乎并不代表兴趣点,而是更复杂的空间对象的点,如 '$osm_polygons''$osm_multipolygons' 。这可以在以下布里斯班中央商务区内的商店示例中看到:

## Import libraries
library(osmdata)
library(ggmap)




# Define functions

## Function to make bounding box
makebb <- function(x) {
  x <- matrix(x,nrow=2,ncol=2)
  rownames(x) <- c("x","y")
  colnames(x) <- c("min","max")
  return(x)
}

## Function to extract all values within a specific key
osm_key <- function(x,bb) {
  x <- bb %>% opq() %>% add_osm_feature(x)
  x <- osmdata_sf(x)
  return(x)
}

## Function to extract specific values within a key
osm_keyvalue <- function(x,y,bb) {
  x <- bb %>% opq() %>% add_osm_feature(x,y)
  x <- osmdata_sf(x)
  return(x)
}

## Function to plot OSM objects
osm_plot <- function(x,bb) {
  defined_map <- get_map(bb, maptype = "toner-background")
  if (is.null(x$osm_multipolygons)) {
    ggmap(defined_map) +
      geom_sf(data = x$osm_points, inherit.aes = FALSE, colour = "#238443",
              fill = "#004529", alpha = 1, size = .7, shape = 21) +
      geom_sf(data = x$osm_polygons, inherit.aes = FALSE, colour = "#238443",
              fill = "#004529", alpha = .5, size = .3, shape = 21) +
      labs(x = "", y = "")
  } else {
    ggmap(defined_map) +
      geom_sf(data = x$osm_points, inherit.aes = FALSE, colour = "#238443",
              fill = "#004529", alpha = 1, size = .7, shape = 21) +
      geom_sf(data = x$osm_polygons, inherit.aes = FALSE, colour = "#238443",
              fill = "#004529", alpha = .5, size = .3, shape = 21) +
      geom_sf(data = x$osm_multipolygons, inherit.aes = FALSE, colour = "#238443",
              fill = "#004529", alpha = .5, size = .3, shape = 21) +
      labs(x = "", y = "")
  }
  
}




# Plot shops in CBD
CBD <- makebb(c(153.02,-27.475,153.03,-27.465))
shop <- osm_key("shop",CBD)
osm_plot(shop,CBD)

如您所见,有些点似乎代表独立的商店,我想将其计入POI计数。然而,其他的似乎只是代表较大百货公司的多边形的角,我想排除。

一个更极端的例子是昆士兰大学 (UoQ) 校区:

# Plot University of Queensland campus
UoQ <- makebb(c(153.00,-27.51,153.03,-27.49))
university <- osm_keyvalue("amenity","university",UoQ)
osm_plot(university,UoQ)

在这里,点显然不代表单独的大学,而是 UoQ 校园多边形的角。因此,校园边缘的公交车站可能会错误地注册 400 米半径内的几所大学,而实际上只有一所大学。

有什么方法可以删除用于绘制其他空间对象(如“$osm_polygons”和“$multipolygons”的“$osm_points”,以便仅相关 POI 保留在 osmdata 对象的“$osm_points”部分?

这里最好的方法可能是在 github issue page of osmdata asking the authors to define a new function to perform the opposite operations of osm_points 中创建一个新问题(或者可能只是征求建议)。同时,我认为您可以使用以下方法:

# packages
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(mapview)

# Define functions

## Function to make bounding box
makebb <- function(x) {
  x <- matrix(x, nrow = 2,ncol = 2)
  rownames(x) <- c("x","y")
  colnames(x) <- c("min","max")
  return(x)
}

## Function to extract all values within a specific key
osm_key <- function(x, bb) {
  x <- bb %>% opq() %>% add_osm_feature(x)
  x <- osmdata_sf(x)
  return(x)
}

## Function to extract specific values within a key
osm_keyvalue <- function(x, y, bb) {
  x <- bb %>% opq() %>% add_osm_feature(x,y)
  x <- osmdata_sf(x)
  return(x)
}

# Plot shops in CBD
CBD <- makebb(c(153.02,-27.475,153.03,-27.465))
shop <- osm_key("shop",CBD)

mapview(shop$osm_points, cex = 2) + shop$osm_polygons

# Get all the points that define the polygons
shop_points_defining_polygons <- osm_points(shop, rownames(shop$osm_polygons))
# Exclude all points that define the polygons
shop_proper_points <- dplyr::anti_join(
  shop$osm_points, 
  sf::st_drop_geometry(shop_points_defining_polygons), 
  by = "osm_id"
)

mapview(shop_proper_points, cex = 2) + shop$osm_polygons

reprex package (v0.3.0)

于 2020-08-23 创建