中国省在R中的坐标

问题描述

有谁知道如何访问中国各地区的坐标。下面的代码显示了我正在寻找中国的同一件事。提前非常感谢

require(maps)
 states_map <- map_data("state")

解决方法

这是您要寻找的吗?

 map_data("world",region="China")

      long      lat group order region subregion
1 110.8888 19.99194     1     1  China         1
2 110.9383 19.94756     1     2  China         1
3 110.9707 19.88330     1     3  China         1
4 110.9977 19.76470     1     4  China         1
5 111.0137 19.65547     1     5  China         1
6 110.9127 19.58608     1     6  China         1
,

您可能要检查以下方法:

# load packages
library(sf)
#> Linking to GEOS 3.8.0,GDAL 3.0.4,PROJ 6.3.1
library(osmextract)

# get polygons in china
poly_china <- openstreetmap_fr_zones[which(openstreetmap_fr_zones$parent == "china"),]

# extract the coords and save the coords in a data.frame
# you may want to keep the data in matrix format for better performances
poly_china_coords <- as.data.frame(st_coordinates(poly_china))

# extract the region name
my_times <- vapply(st_geometry(poly_china),function(x) nrow(st_coordinates(x)),numeric(1))
poly_china_coords$region_name <- rep(poly_china$name,times = my_times)

# result
head(poly_china_coords)
#>         X      Y L1 L2 L3 region_name
#> 1 114.875 32.960  1  1  1       Anhui
#> 2 114.860 32.970  1  1  1       Anhui
#> 3 114.870 33.025  1  1  1       Anhui
#> 4 114.880 33.035  1  1  1       Anhui
#> 5 114.895 33.035  1  1  1       Anhui
#> 6 114.890 33.060  1  1  1       Anhui

reprex package(v0.3.0)于2020-11-06创建

您可以从CRAN安装sf,也可以如下安装osmextract

install.packages("remotes")
remotes::install_github("ITSLeeds/osmextract")

数据使用EPSG:4326存储,因此X = long,Y = lat。