代码之家  ›  专栏  ›  技术社区  ›  Vlad

R googleway与ABS的人口普查数据

  •  1
  • Vlad  · 技术社区  · 7 年前

    census data googleway 制作热图。

    我的方法是使用googleway提供的数据 melbourne 其中包含列 SA2_NAME 并在转换后与人口普查数据中的ESRI形状文件连接 rgdal (代码如下)。问题是 对于多段线不是唯一的-某些SA2区域由多个“子”区域组成。所以这似乎不是一个好办法。

    更好的方法是转换ESRI形状数据 sa2_shape 墨尔本 数据。这是怎么做到的?

    下面的代码生成一个用于连接的“桥接”数据帧 墨尔本 数据来源 googleway SA2_MAIN 作为关键字段-如上所述,这种“hack”方法的问题是多段线不是唯一的

    library(tidyverse)
    library(googleway)
    library(rgdal)
    
    shape_path <- "abs_data/sa2_esri_shapefile"
    shape_file <- "SA2_2016_AUST"
    sa2_shape <- readOGR(shape_path, shape_file)
    sa2_df <- data.frame(sa2_shape$SA2_MAIN, sa2_shape$SA2_NAME)
    names(sa2_df) <- c("SA2_MAIN", "SA2_NAME")
    sa2_df <- sa2_df %>% semi_join(melbourne, by = "SA2_NAME")
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Vlad    7 年前

    根据SYMBOLXAU注释-已使用 sf 加载数据,只要 geometry 不是空列表-请参阅下面的代码。

    library(tidyverse)
    library(googleway)
    library(sf)
    
    shape_path <- "abs_data/sa2_esri_shapefile"
    shape_file <- "SA2_2016_AUST"
    
    shape_file_path <- paste0(shape_path, "/", shape_file, '.shp')
    sa2_shape <- sf::st_read(shape_file_path)
    sa2_shape <- sa2_shape %>% 
      filter(STATE_NAME == "Victoria",
             AREA_SQKM > 0)# This is important - otherwise google_map() will crash!
    google_map() %>% 
      googleway::add_polygons(data = sa2_shape, 
                              polyline = "geometry", 
                              fill_colour = "SA2_NAME")
    

    Victoria SA2 map

    > sa2_shape %>% head()
    Simple feature collection with 6 features and 6 fields
    geometry type:  MULTIPOLYGON
    dimension:      XY
    bbox:           xmin: 143.6849 ymin: -37.68153 xmax: 143.951 ymax: -37.46847
    epsg (SRID):    4283
    proj4string:    +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
       SA2_MAIN SA2_MAIN16         SA2_NAME STATE_CODE STATE_NAME AREA_SQKM                       geometry
    1 201011001  201011001        Alfredton          2   Victoria   52.7111 MULTIPOLYGON (((143.7072 -3...
    2 201011002  201011002         Ballarat          2   Victoria   12.3787 MULTIPOLYGON (((143.8675 -3...
    3 201011003  201011003 Ballarat - North          2   Victoria   92.3577 MULTIPOLYGON (((143.853 -37...
    4 201011004  201011004 Ballarat - South          2   Victoria   32.8541 MULTIPOLYGON (((143.8675 -3...
    5 201011005  201011005        Buninyong          2   Victoria   51.5855 MULTIPOLYGON (((143.8533 -3...
    6 201011006  201011006        Delacombe          2   Victoria   34.1608 MULTIPOLYGON (((143.7072 -3...
    
    推荐文章