在R中使用deckgl进行空间数据可视化

我想提供一个示例,说明如何使用R中的deckgl软件包绘制3D地图图。我从Mapbox在地图布局上生成条形图。

首先,我定义了在datascienceplus.com上找到的“开放街道地图”地理编码功能。它使用Open Street Map API(OSM-API)将地址字符串地理编码为经度和纬度坐标。

## 文件资料: http://wiki.openstreetmap.org/wiki/Nominatim
# 资源: https://datascienceplus.com/osm-nominatim-with-r-getting-locations-geo-coordinates-by-its-address/
osm_geocoder <- function(address = NULL)
{
  if(suppressWarnings(is.null(address)))
    return(data.frame())
  
  tryCatch(
    d <- jsonlite::fromJSON( 
      gsub('\\@addr\\@', gsub('\\s+', '\\%20', address), 
           'http://nominatim.openstreetmap.org/search/@addr@?format=json&addressdetails=0&limit=1')
    ), error = function(c) return(data.frame())
  )
  
  if(length(d) == 0) 
    return(data.frame())
  
  return(data.frame(lon = as.numeric(d$lon), lat = as.numeric(d$lat)))
}

之后,我用城市名称建立一个列表。我将此列表提供给地理编码功能,从而生成一个地理编码的数据帧。

city_list <- c("Frankfurt, Germany",
               "Dortmund, Germany",
               "Berlin, Germany",
               "Stuttgart, Germany",
               "Karlsruhe, Germany",
               "Zurich, Switzerland", 
               "Wien, Austria", 
               "Bern, Switzerland",
               "Munich, Germany",
               "Marburg, Germany",
               "Giessen, Germany",
               "Pforzheim, Germany",
               "Cologne, Germany", 
               "Essen, Germany", 
               "Heidelberg, Germany",
               "Kassel, Germany",
               "Paderborn, Germany",
               "Leipzig, Germany",
               "Dresden, Germany",
               "Nuremberg, Germany",
               "Augsburg, Germany",
               "Regensburg, Germany",
               "Pilsen, Czech Republic",
               "Linz, Austria",
               "Innsbruck, Austria",
               "Bamberg, Germany",
               "Fulda, Germany",
               "Hanover, Germany",
               "Wolfsburg, Germany",
               "Magdeburg, Germany",
               "Jena, Germany",
               "Chemnitz, Germany",
               "Zwickau, Germany",
               "Cottbus, Germany",
               "Bautzen, Germany",
               "Mainz, Germany",
               "Bonn, Germany",
               "Duesseldorf, Germany",
               "Trier, Germany",
               "Mannheim, Germany",
               "Aachen, Germany",
               "Freiburg, Germany",
               "Ulm, Germany",
               "Kempten, Germany",
               "Ingolstadt, Germany")

# 让我们生成一个可用于add_grid_layer函数的数据框
data_df_columns <- data.frame(matrix(nrow=3500*length(city_list),ncol=4))
colnames(data_df_columns) <- c("ADDRESS","RACKS","SPACES","COORDINATES")

data_df_columns$RACKS <- 2
data_df_columns$SPACES <- 4

for(i in 1:length(city_list)){
  print(i)
  dum_coord <- osm_geocoder(city_list[i])
  for(j in 1:3500){
    data_df_columns$ADDRESS <- paste0(city_list[i]," : ",j)
    dum_long <- rnorm(1, mean=as.numeric(dum_coord[1]),sd=0.75)  
    dum_lat <- rnorm(1,mean=as.numeric(dum_coord[2]),sd=0.75)
    data_df_columns$COORDINATES[(i-1)*3500+j] <- list(c(dum_long, dum_lat))
  }
}

properties_grid <- list(
  pickable = TRUE,
  extruded = TRUE,
  cellSize = 1600,
  elevationScale = 8,
  coverage = 0.5,
  colorRange = list(c(0,255,255),
                    c(0,204,255),
                    c(0,153,255),
                    c(0,51,255),
                    c(0,0,255),
                    c(0,0,204)),
  getPosition = JS("data => data.COORDINATES"),
  getTooltip = JS("object => object.count")
)

一旦有了包含用地理编码功能进行地理编码的人为创建的空间数据点的数据集,就可以使用R中的deckgl包对其进行绘制。底图是在Mapbox中创建的。

deckgl(zoom = 10, pitch = 120, longitude = 8.682092, latitude = 50.11064) %>%
  add_grid_layer(data = data_df_columns,properties = properties_grid) %>%
  add_mapbox_basemap(style = "mapbox://styles/linnartsf/cjq6p9q8f8zwf2rp74qf2o3d5")
3D地图绘制,可视化人工创建的空间数据集(使用OSM-API进行地理编码)

我在R中发布了其他一些有关空间数据可视化的文章,您可能想看看。

You May Also Like

Leave a Reply

Leave a Reply

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据