创建从给定点(纬度,经度)开始的线形状文件,具有特定的角度/坡度,并在与多边形形状文件重叠时截断它

问题描述 投票:0回答:1

###---------- 已更新 ------------###

我需要创建一个具有特定长度的线形状文件,该文件从指定点开始并形成一定角度。然后,当它与多边形相交时截断它,这样我就可以测量从初始点到多边形的距离。 我需要在 R 中执行此操作。

Polygons_sf 只是亚速尔群岛的多边形形状文件。 sightsLand 是一个点形状文件,其中包含海上的不同点,新线应从这些点开始。

代码如下。 我只是从某个角度(radial_line_sf)的点创建一条新线。现在我只需要在新线与多边形形状文件重叠时截断它(见下图),但我还没有做到这一点。

# Load required packages
library(sf)
library(ggplot2)

# shapefiles
polygon_sf <- read_sf("CAOP_RAA_2022-shp/ArqAcores/ArqAcores_GCentral_AAd_CAOP2022.shp")
polygon_sf <- st_transform(polygon_sf, crs = 4326)
st_crs(polygon_sf)

sights <- read_sf("Sightings.shp")
sightsLand <- subset(sights, sights$RetclLn == "LA")

# Extract lat and lon from the POINT geometry
point_location <- st_coordinates(sightsLand[1, "geometry"])

# Create a single radial line
angle_degrees <- 55  # Specify the angle
line_length <- 0.25  # Adjust the length as needed
angle_rad <- angle_degrees * (pi / 180)  # Convert degrees to radians
end_lat <- point_location[2] + line_length * sin(angle_rad)
end_lon <- point_location[1] + line_length * cos(angle_rad)

#matrix_coords <- rbind(c(point_location[2], end_lon), c(point_location[1], end_lat))
matrix_coords <- rbind(c(point_location[1], point_location[2]), c(end_lon, end_lat))
#matrix_coords <- matrix(c(point_location[2], point_location[1], end_lon, end_lat), ncol = 2)
radial_line_sf <- st_sfc(st_linestring(matrix_coords), crs = 4326)
#radial_line_sf <- st_as_sf(data.frame(matrix_coords), coords = c("X1", "X2"), crs = 4326)

# Plot the polygon, point, and radial line
ggplot() +
  geom_sf(data = polygon_sf, fill = "lightblue", color = "black") +
  geom_sf(data = sightsLand[1,28], color = "red", size = 3, shape = 18) +
  geom_sf(data = radial_line_sf, color = "blue") +
  theme_minimal()

我使用 ggplot 得到以下地图:

r geospatial trigonometry spatial shapefile
1个回答
0
投票

您可以使用

lwgeom::st_split()
将线与多边形分割;如果您仅从生成的几何集合中提取第一项,您将获得从预定义点(黄色部分)开始的线段:

library(ggplot2)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE

# sample geometries, starting point for the linestring is (1,1)
poly <- st_point(c(5,5)) |> st_buffer(2)
line_1 <- st_linestring(matrix(c(1,1,9,9), ncol = 2, byrow = TRUE))

# split line by polygon, terurns collection of 3 linestrings, keep the first
line_2 <- lwgeom::st_split(line_1, poly)[[1]]

ggplot() +
  geom_sf(data = poly, fill = NA, color = "darkgreen", linewidth = 1) +
  geom_sf(data = line_2, color = "gold", linewidth = 4) +
  geom_sf(data = line_1, color = "darkblue", linewidth = 1) +
  scale_x_continuous(breaks = 0:10, expand = expansion(add = 1)) +
  scale_y_continuous(breaks = 0:10, expand = expansion(add = 1)) +
  theme_minimal()

创建于 2023-10-18,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.