如何在R中的sf包中查找区域级几何图形

发布于 2025-01-16 10:03:42 字数 444 浏览 5 评论 0原文

我想使用 sfplotly 包绘制地理空间地图。但是,我没有国家级数据,只有地区级数据(亚洲、欧洲、拉丁美洲等)。这是我将要处理的数据集的模型,我正在寻找一种查找经度、纬度和几何形状的方法。我打算先收集Excel中的所有数据,然后将它们移动到R中进行分析

在此处输入图像描述

我查找了多个包/数据集,例如 rnaturalearth< /代码>, maps::map 等,但我找不到正确的纬度/经度/坐标或区域的几何形状。我在哪里可以找到它们?提前致谢

I want to plot a geospatial map using sf and plotly package. However, I don't have country-level data, only region-level data (Asia, Europe, Latin America, etc.). This is the mock-up of the dataset I will be working on, I'm looking for a way to lookup the longitude, latitude, and geometry. I plan to collect all the data in Excel first, and later move them to R for analysis

enter image description here

I've looked for several packages/datasets e.g., rnaturalearth, maps::map, etc, but I couldn't find the correct latitude/longitude/coordinates or the geometry of the regions. Where can I find them? Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2025-01-23 10:03:42

在这里,您对您的问题有一些想法:

  1. 由于不确定您需要如何定义每个区域(国家的区域分类有很多可能性),也许您应该首先将世界上每个国家映射到您认为所属的区域到。之后,您只需将相应的地区加入到每个国家即可。
  2. 在 Excel 中存储几何图形并不是一个好的选择(字段长度、使用的 crs 等)。存储空间对象的更好替代方法是 .gpkg 格式。

请在此处找到如何生成您提到的数据集。

我将在这里使用联合国地理区域分类。这在 countrycode 包中可用,因此基本上我将每个 ISO-3 国家/地区代码映射到相应的区域。之后,我按地区对国家进行分组,这样我就可以按地区获得单一的几何图形。最后,我提取边界框并将它们附加到数据集:

library(giscoR)
library(countrycode)
library(sf)

world <- gisco_get_countries()

# Add the subregion

world$region <- countrycode(world$ISO3_CODE,
                                origin = "iso3c",
                                destination = "un.regionsub.name")

#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, CPT, XA, XB, XC, XD, XE, XF, XG, XH, XI, XL, XM, XN, XO, XU, XV

unique(world$region)
#>  [1] "Latin America and the Caribbean" "Polynesia"                      
#>  [3] "Western Europe"                  NA                               
#>  [5] "Southern Europe"                 "Western Asia"                   
#>  [7] "Southern Asia"                   "Sub-Saharan Africa"             
#>  [9] "Australia and New Zealand"       "Eastern Europe"                 
#> [11] "Northern America"                "South-eastern Asia"             
#> [13] "Eastern Asia"                    "Micronesia"                     
#> [15] "Northern Europe"                 "Melanesia"                      
#> [17] "Central Asia"                    "Northern Africa"


# Group regions
library(dplyr)
library(ggplot2)


subworld <- world %>% 
  group_by(region) %>%
  # Mock the data field
  summarise(data=n())

ggplot(subworld) +
  geom_sf(aes(fill=region))

# Create final table
bbox <- lapply(st_geometry(subworld), st_bbox) %>%
  bind_rows() %>% 
  mutate(across(where(is.numeric), as.double))


# Final dataset

end <- subworld %>% bind_cols(bbox) %>%
  select(region, data, xmin, xmax, ymin, ymax)

head(end)
#> Simple feature collection with 6 features and 6 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -180 ymin: -59.51912 xmax: 180 ymax: 81.85799
#> Geodetic CRS:  WGS 84
#> # A tibble: 6 x 7
#>   region               data   xmin   xmax  ymin   ymax                  geometry
#>   <chr>               <int>  <dbl>  <dbl> <dbl>  <dbl>        <MULTIPOLYGON [°]>
#> 1 Australia and New ~     6 -178.  179.   -53.2 -9.21  (((143.5617 -13.77157, 1~
#> 2 Central Asia            5   46.6  87.3   35.2 55.4   (((50.3399 45.05235, 50.~
#> 3 Eastern Asia            7   73.6 146.    18.2 53.5   (((120.8954 22.336, 120.~
#> 4 Eastern Europe         10 -180   180.    41.2 81.9   (((68.7333 76.49168, 68.~
#> 5 Latin America and ~    48 -118.    3.43 -59.5 32.7   (((-64.01772 -54.74885, ~
#> 6 Melanesia               5 -180   180.   -22.7 -0.826 (((178.2331 -17.34448, 1~

Created on 2022-03-23 by reprex 包 (v2.0.1)

here you have some thoughs about your question:

  1. As it is uncertain how you need to define each region (there are many possibilities of regional classification of countries), maybe you should start by mapping each country of the world to the region you considers it belongs to. After, you can just join the corresponding region to each country.
  2. To store a geometry in excel is not a good choice (lenght of field, what crs is in use, etc). A better alternative for storing spatial objects is the .gpkg format.

Please find here how to produce a dataset as the one you mention.

I would use here the geographic regions classification of the UN. This is available on the countrycode package so basically I map each ISO-3 country code to the corresponding region. After, I group the countries by region so I can have a single geometry by region. By last, I extract the bounding boxes and append them to the dataset:

library(giscoR)
library(countrycode)
library(sf)

world <- gisco_get_countries()

# Add the subregion

world$region <- countrycode(world$ISO3_CODE,
                                origin = "iso3c",
                                destination = "un.regionsub.name")

#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, CPT, XA, XB, XC, XD, XE, XF, XG, XH, XI, XL, XM, XN, XO, XU, XV

unique(world$region)
#>  [1] "Latin America and the Caribbean" "Polynesia"                      
#>  [3] "Western Europe"                  NA                               
#>  [5] "Southern Europe"                 "Western Asia"                   
#>  [7] "Southern Asia"                   "Sub-Saharan Africa"             
#>  [9] "Australia and New Zealand"       "Eastern Europe"                 
#> [11] "Northern America"                "South-eastern Asia"             
#> [13] "Eastern Asia"                    "Micronesia"                     
#> [15] "Northern Europe"                 "Melanesia"                      
#> [17] "Central Asia"                    "Northern Africa"


# Group regions
library(dplyr)
library(ggplot2)


subworld <- world %>% 
  group_by(region) %>%
  # Mock the data field
  summarise(data=n())

ggplot(subworld) +
  geom_sf(aes(fill=region))

# Create final table
bbox <- lapply(st_geometry(subworld), st_bbox) %>%
  bind_rows() %>% 
  mutate(across(where(is.numeric), as.double))


# Final dataset

end <- subworld %>% bind_cols(bbox) %>%
  select(region, data, xmin, xmax, ymin, ymax)

head(end)
#> Simple feature collection with 6 features and 6 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -180 ymin: -59.51912 xmax: 180 ymax: 81.85799
#> Geodetic CRS:  WGS 84
#> # A tibble: 6 x 7
#>   region               data   xmin   xmax  ymin   ymax                  geometry
#>   <chr>               <int>  <dbl>  <dbl> <dbl>  <dbl>        <MULTIPOLYGON [°]>
#> 1 Australia and New ~     6 -178.  179.   -53.2 -9.21  (((143.5617 -13.77157, 1~
#> 2 Central Asia            5   46.6  87.3   35.2 55.4   (((50.3399 45.05235, 50.~
#> 3 Eastern Asia            7   73.6 146.    18.2 53.5   (((120.8954 22.336, 120.~
#> 4 Eastern Europe         10 -180   180.    41.2 81.9   (((68.7333 76.49168, 68.~
#> 5 Latin America and ~    48 -118.    3.43 -59.5 32.7   (((-64.01772 -54.74885, ~
#> 6 Melanesia               5 -180   180.   -22.7 -0.826 (((178.2331 -17.34448, 1~

Created on 2022-03-23 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文