Tidycensus软件包:可以通过人口普查区获得纬度和经度吗?

时间:2019-01-24 17:34:22

标签: r geospatial

我有一个脚本,可使用tidycensus软件包提取GeoID。这些数据非常有价值,但我也想参考经纬度(即使它们是近似值)。问题是,我不知道怎么做或是否有可能。或者,是否有任何方法可以利用另一个空间包来合并此类数据。请参见下面的代码示例。

>>> arr = [*range(5)]
>>> print(*arr)
0 1 2 3 4

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您在geometry = TRUE调用中指定get_decennial(),则tidycensus将返回多边形几何以及普查估计数。然后,您可以使用sf::st_centroid()获取每个多边形的质心(中间)。

在此示例中,我获得了人口普查数据和几何图形,然后使用每个县多边形的形心创建第二个sf对象。最后,创建一个快速地图,以便您可以看到多边形及其质心。

library(tidyverse)
library(tidycensus)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3

county <- get_decennial(
  geography = "county",
  state = "SC",
  variables = c(county = "COUNTY", state = "STATE"),
  output = "wide",
  geometry = TRUE
  )
#> Getting data from the 2010 decennial Census

count_centroid <- county %>% 
  st_transform(2273) %>% # convert to projected coord system for better centroid
  st_centroid()
#> Warning in st_centroid.sf(.): st_centroid assumes attributes are constant
#> over geometries of x

head(count_centroid)
#> Simple feature collection with 6 features and 4 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 1506653 ymin: 420377.9 xmax: 1983425 ymax: 1170306
#> epsg (SRID):    2273
#> proj4string:    +proj=lcc +lat_1=34.83333333333334 +lat_2=32.5 +lat_0=31.83333333333333 +lon_0=-81 +x_0=609600 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=ft +no_defs
#> # A tibble: 6 x 5
#>   GEOID NAME                             county state           geometry
#>   <chr> <chr>                             <dbl> <dbl>       <POINT [ft]>
#> 1 45009 Bamberg County, South Carolina        9    45 (1983425 502618.9)
#> 2 45001 Abbeville County, South Carolina      1    45 (1559068 872366.5)
#> 3 45005 Allendale County, South Carolina      5    45 (1890285 420377.9)
#> 4 45007 Anderson County, South Carolina       7    45 (1506653 981064.2)
#> 5 45021 Cherokee County, South Carolina      21    45  (1814307 1170306)
#> 6 45011 Barnwell County, South Carolina      11    45 (1866998 521545.1)

ggplot(county) +
  geom_sf() +
  geom_sf(data = count_centroid) +
  theme_void()

reprex package(v0.3.0)于2019-06-16创建