• Welcome to LiuJason's Blog!

使用R语言计算坐标点到海岸线的距离|利用sf包

R语言 Jason 4 years ago (2021-01-06) 585 Views 0 Comments

去年一个项目中的一段代码,分享出来给需要的同学。

library(tidyverse)
library(sf)
library(data.table)

#Read in registry agency list and convert the coordinates
region_list <- na.omit(read.csv(file = "./data/CI5-XId/converted/registry_info.csv", 
                                header =T)[,c(1,2,5,6)])
region_list_sf <- region_list %>% st_as_sf(coords = c('longitude','latitude')) %>% 
  st_set_crs(4326)

#Read in the global coastline info
#Downloaded from https://www.naturalearthdata.com/
global_coastline <- read_sf("./data/MapShp/GlobalCoastline/ne_10m_coastline.shp", crs = 4326)

#Calculate the distances
distance <- array()
for(i in 1:length(region_list[,1])){
  result <- array()
  result <- st_distance(region_list_sf[i,], global_coastline[,3])
  distance[i] <- min(result)
  print(paste(i,": Done!"))
}

#Combine list with distance
region_info_with_distance <- cbind(region_list, distance)

#Write to file
write.csv(region_info_with_distance, 
          file = "./data/CI5-XId/region_info_with_distance.csv", quote = T, 
          row.names = F)


This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/1128.html
Like (3)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址