微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将经度和纬度与 R

如何解决将经度和纬度与 R

我想找到对应于两组不同坐标的国家。我的数据设置为

lat_1 lon_1 lat_2 lon_2
40.71 74.00 51.50 0.127
37.77 122.4 48.85 2.352

我希望将结果存储在两个新列中。所以对于第一行,一列会说美国,另一列会说英格兰。我尝试使用一个函数将我的坐标转换为国家/地区,但是我必须一次将该函数应用于一组,我不确定它们是否匹配。同样使用该函数,它不会将其添加为额外的列。

到目前为止,我在下面列出了。

library(sp)
library(rworldmap)
library(dplyr)


coords2country = function(points)
{  
  countriessp <- getMap(resolution='low')

  pointssp = SpatialPoints(points,proj4string=CRS(proj4string(countriessp)))  


  indices = over(pointssp,countriessp)


  indices$ADMIN  

}

 df <-read.csv("the_file",header=T,na.strings=c("","NA"))

 coords2country(df)

当我这样做时,我得到了上面描述的东西,而不是我想要的东西。

解决方法

这里是使用最新的 sf 包作为基础的完整代码,可以实现您的需求。有关更多解释,请参阅代码随附的注释。


coords_df <- tibble::tribble(
  ~lat_1,~lon_1,~lat_2,~lon_2,40.71,74,51.5,0.127,37.77,122.4,48.85,2.352
  ) %>% 
  dplyr::mutate(id = dplyr::row_number()) # create id column for each observation to ensure matching


# transform coordinates into a geo object (here,an sf object)
coords_sf <- coords_df %>% 
  tidyr::pivot_longer(cols = 1:4,names_to = "coord_type",values_to = "coord_data") %>% 
  tidyr::separate(col = coord_type,into = c("coord_type","set"),sep = "_") %>% 
  tidyr::pivot_wider(names_from = coord_type,values_from = coord_data) %>% 
  sf::st_as_sf(coords = c("lon","lat"),crs = 4326)

coords_sf
#> Simple feature collection with 4 features and 2 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 0.127 ymin: 37.77 xmax: 122.4 ymax: 51.5
#> Geodetic CRS:  WGS 84
#> # A tibble: 4 x 3
#>      id set        geometry
#> * <int> <chr>   <POINT [°]>
#> 1     1 1        (74 40.71)
#> 2     1 2      (0.127 51.5)
#> 3     2 1     (122.4 37.77)
#> 4     2 2     (2.352 48.85)

# get low resolution world map
world <- rnaturalearth::ne_countries(returnclass = "sf") %>%
  dplyr::select(name) %>% # keep only country name
  sf::st_transform(crs = 4326) %>%
  st_make_valid() # useful as of 1.0 `sf` update,see https://github.com/r-spatial/sf/issues/1649


# join columns,if you want a country only if the point is within its borders
within_sf <- sf::st_join(x = coords_sf,y = world,join = sf::st_within)

within_sf
#> Simple feature collection with 4 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 0.127 ymin: 37.77 xmax: 122.4 ymax: 51.5
#> Geodetic CRS:  WGS 84
#> # A tibble: 4 x 4
#>      id set        geometry name          
#> * <int> <chr>   <POINT [°]> <chr>         
#> 1     1 1        (74 40.71) Kyrgyzstan    
#> 2     1 2      (0.127 51.5) United Kingdom
#> 3     2 1     (122.4 37.77) <NA>          
#> 4     2 2     (2.352 48.85) France

# join columns,if you want the country closest to the point
# (even if the point is not within the border of any country)
nearest_sf <- sf::st_join(x = coords_sf,join = sf::st_nearest_feature)

nearest_sf
#> Simple feature collection with 4 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 0.127 ymin: 37.77 xmax: 122.4 ymax: 51.5
#> Geodetic CRS:  WGS 84
#> # A tibble: 4 x 4
#>      id set        geometry name          
#> * <int> <chr>   <POINT [°]> <chr>         
#> 1     1 1        (74 40.71) Kyrgyzstan    
#> 2     1 2      (0.127 51.5) United Kingdom
#> 3     2 1     (122.4 37.77) China         
#> 4     2 2     (2.352 48.85) France

# now you have a country for each point.
# time to go back to your original format


# again a data frame,not any more an sf object
nearest_df <- dplyr::bind_cols(nearest_sf %>%
                                 sf::st_drop_geometry(),nearest_sf %>% 
                                 sf::st_coordinates() %>% 
                                 tibble::as_tibble() %>% 
                                 dplyr::rename(lon = X,lat = Y)) 

nearest_df
#> # A tibble: 4 x 5
#>      id set   name               lon   lat
#>   <int> <chr> <chr>            <dbl> <dbl>
#> 1     1 1     Kyrgyzstan      74      40.7
#> 2     1 2     United Kingdom   0.127  51.5
#> 3     2 1     China          122.     37.8
#> 4     2 2     France           2.35   48.8

output_df <- dplyr::bind_cols(nearest_df %>% 
                   dplyr::filter(set == 1) %>% 
                   dplyr::transmute(lat_1 = lat,lon_1 = lon,name_1 = name),nearest_df %>% 
                   dplyr::filter(set == 2) %>% 
                   dplyr::transmute(lat_2 = lat,lon_2 = lon,name_2 = name))


output_df
#> # A tibble: 2 x 6
#>   lat_1 lon_1 name_1     lat_2 lon_2 name_2        
#>   <dbl> <dbl> <chr>      <dbl> <dbl> <chr>         
#> 1  40.7   74  Kyrgyzstan  51.5 0.127 United Kingdom
#> 2  37.8  122. China       48.8 2.35  France

reprex package (v2.0.0) 于 2021 年 6 月 18 日创建

作为参考,我还会在这里留下一个指向基于闪亮的解决方案的链接: https::github.com/giocomai/latlon2map / 如果您想快速浏览,这里有一个托管版本:latlon2map.europeandatajournalism.eu。有了这个,你可以加载你的 csv,选择你的第一组经纬度,下载表格,用另一组再做一次,然后在 R 或其他地方合并结果。

以上部分代码改编自同一个包的 ll_match() 函数。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。