[英]Geosphere package distance from point to polygon
I'm trying to use the geosphere package in R to get the distance to a polygon from a set of points that lie outside of that polygon.我正在尝试使用 R 中的地理圈 package 从位于该多边形外部的一组点获取到该多边形的距离。
The polygon is a shapefile of the Antarctic coastline, found here: https://data.bas.ac.uk/items/e6cf8946-e493-4c36-b4f5-58f7a2ee2a74/ and the points are animal tracking data.多边形是南极海岸线的形状文件,可在此处找到: https://data.bas.ac.uk/items/e6cf8946-e493-4c36-b4f5-58f7a2ee2a74/点是动物跟踪数据。
I have tried using the syntax specified in the geosphere documentation ( https://www.rdocumentation.org/packages/geosphere/versions/1.5-14/topics/dist2Line ) which is as follows:我尝试使用 geosphere 文档 ( https://www.rdocumentation.org/packages/geosphere/versions/1.5-14/topics/dist2Line ) 中指定的语法,如下所示:
dist2Line(p, line, distfun=distGeo)
#my attempt so far:
#libraries
library(rgdal)
library(sf)
library(rgeos)
library(tidyverse)
library(geosphere)
#my points
points <-read.csv("Analyses/example_points.csv") #this is the table included below of 4 example locations.
|ID|LON |LAT |
|--|----------|----------|
|a |-2.515478 |-69.53887 |
|b |-2.601405 |-69.79783 |
|c |-0.153548 |-69.45126 |
|d |26.06987 |-69.55020 |
#my line
line <- <- readOGR('Environmental_Data/COAST/add_coastline_high_res_polygon_v7_5.shp/') #this is the shapefile linked above
#convert points to spatial object
coordinates(points) <- ~LON+LAT
distance <- geosphere::dist2Line(p = points, line = line, distfun = distGEO)
However, I get an error: " Error in.spDistPoint2Line(p, line, distfun): Points are projected. They should be in degrees (longitude/latitude) ".但是,我得到一个错误:“ Error in.spDistPoint2Line(p, line, distfun): Points are projected. They should be in degrees (longitude/latitude) ”。
The package documentation states that p can be: " longitude/latitude of point(s). Can be a vector of two numbers, a matrix of 2 columns (first one is longitude, second is latitude) or a SpatialPoints object*" - which is what I'm providing it with. package 文档指出 p 可以是:“点的经度/纬度。可以是两个数字的向量,2 列的矩阵(第一个是经度,第二个是纬度)或一个 SpatialPoints对象*” - 其中是我提供的。 I have seen the same issue encountered on a Reddit post (unanswered) but not on here.我在 Reddit 帖子(未答复)上看到过同样的问题,但在这里没有。
My desired output is as below (distances under distance to coast are made up for now.).我想要的 output 如下所示(现在弥补了到海岸距离下的距离。)。 I have ~3000 locations I need to find the distance to the coastline for.我有大约 3000 个位置需要找到到海岸线的距离。
ID ID | LON伦敦 | LAT土地增值税 | Dist_to_coast (km)海岸距离(公里) |
---|---|---|---|
a一种 | -2.515478 -2.515478 | -69.53887 -69.53887 | 40 40 |
b b | -2.601405 -2.601405 | -69.79783 -69.79783 | 24 24 |
c c | -0.153548 -0.153548 | -69.45126 -69.45126 | 74 74 |
d d | 26.06987 26.06987 | -69.55020 -69.55020 | 23 23 |
Is there an alternative/better means of doing this?是否有替代/更好的方法来做到这一点?
Thank you.谢谢你。
You have loaded sf
, any particular reason for not using sf::st_distance()
for the task?您已加载sf
,是否有任何特殊原因不使用sf::st_distance()
来完成任务? Would still need to transform though, as there are 4 sample points vs ~140MB shapefile with ~17000 polygons, points were transformed:仍然需要转换,因为有 4 个样本点与 ~140MB shapefile 和 ~17000 个多边形,点被转换:
library(ggplot2)
library(dplyr)
library(sf)
coastline <- st_read("add_coastline_high_res_polygon_v7_6.shp/")
p <- readr::read_delim(
"ID|LON |LAT
a |-2.515478 |-69.53887
b |-2.601405 |-69.79783
c |-0.153548 |-69.45126
d |26.06987 |-69.55020" , delim = "|", trim_ws = T) %>%
st_as_sf(coords = c("LON", "LAT"), crs = "WGS84") %>%
# transform points to match crs of the shapefile
st_transform(st_crs(coastline))
# number of different surface polygons
table(coastline$surface)
#>
#> ice shelf ice tongue land rumple
#> 325 37 17233 64
# create a single multipolygon, can take a while;
# you may need to filter first to define any surface types you might want to
# include / exclude ("land" also includes islands)
system.time({
ucoastline <- st_union(coastline)
})
#> user system elapsed
#> 103.40 11.72 116.08
p$dist_ucoastline <- st_distance(p,ucoastline)
# or perhaps select land polygon with max area to
# ignore ice and all the islands:
land_max <- coastline %>%
slice_max(st_area(.))
p$land_max <- st_distance(p,land_max)
ggplot() +
geom_sf(data = st_simplify(ucoastline,dTolerance = 1000), fill = "lightblue", color = NA) +
geom_sf(data = st_simplify(land_max,dTolerance = 1000), fill = "gray70") +
geom_sf(data = p, shape =4, color="red", size = 5) +
theme_bw()
Result:结果:
# convert coordinates back to WGS84,
# geometries to coordinate columns
bind_cols(
st_transform(p, crs = "WGS84") %>% st_coordinates(),
st_drop_geometry(p)
)
#> # A tibble: 4 × 5
#> X Y ID dist_ucoastline[,1] land_max[,1]
#> <dbl> <dbl> <chr> [m] [m]
#> 1 -2.52 -69.5 a 40742. 180479.
#> 2 -2.60 -69.8 b 39750. 157043.
#> 3 -0.154 -69.5 c 6629. 186878.
#> 4 26.1 -69.6 d 45683. 121500.
Created on 2022-11-23 with reprex v2.0.2创建于 2022-11-23,使用reprex v2.0.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.