R中的空间线起点和终点 [英] Spatial line start and end point in R

查看:45
本文介绍了R中的空间线起点和终点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sp 包来访问线串的起点和终点,类似于 ST_StartPointST_EndPoint使用 psql 生成.

I am attempting to use the sp package to access the start and end points of a linestring, similar to what ST_StartPoint and ST_EndPoint would produce using psql.

无论我如何尝试访问该行,我都会收到错误或 NULL 值:

No matter how I try to access the line, I get errors or NULL value:

> onetrip@lines[[1]][1]
Error in onetrip@lines[[1]][1] : object of type 'S4' is not subsettable

> onetrip@lines@Lines@coords
    Error: trying to get slot "Lines" from an object of a basic class ("list") with no slots

> onetrip@lines$Lines
NULL

唯一有效的解决方案是冗长的,需要转换为SpatialLines,我只能轻松地得到第一点:

The only solution that works is verbose and requires conversion to SpatialLines, and I can only easily get the first point:

test = as(onetrip, "SpatialLines")@lines[[1]]
> test@Lines[[1]]@coords[1,]
[1] -122.42258   37.79494

下面的 str() 和简单的 plot(onetrip) 都表明我的数据框不为空.

Both the str() below and a simple plot(onetrip) show that my dataframe is not empty.

这里的解决方法是什么 - 如何在 sp 中返回线串的起点和终点?

What is the workaround here - how would one return the start and endpoints of a linestring in sp?

我有一个更大的SpatialLinesDataFrame的第一条记录的子集:

I have subset the first record of a larger SpatialLinesDataFrame:

> str(onetrip)
Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
  ..@ data       :'data.frame': 1 obs. of  6 variables:
  .. ..$ start_time : Factor w/ 23272 levels "2018/02/01 00:12:40",..: 23160
  .. ..$ finish_time: Factor w/ 23288 levels "1969/12/31 17:00:23",..: 23288
  .. ..$ distance   : num 2.74
  .. ..$ duration   : int 40196
  .. ..$ route_id   : int 5844736
  .. ..$ vehicle_id    : int 17972
  ..@ lines      :List of 1
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3114, 1:2] -122 -122 -122 -122 -122 ...
  .. .. .. ..@ ID   : chr "0"
  ..@ bbox       : num [1:2, 1:2] -122.4 37.8 -122.4 37.8
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs"

推荐答案

既然你也用 sf 标记了问题,我将在 sf 中提供解决方案.请注意,您可以使用

Since you tagged question with sf as well, I'll provide a solution in sf. Note you can transform your sp object to sf using

library(sf)
st_as_sf(sp_obj)

创建线串

line <- st_as_sfc(c("LINESTRING(0 0 , 0.5 1 , 1 1 , 1 0.3)")) %>% 
  st_sf(ID = "poly1")   

将每个顶点转换为点

pt <- st_cast(line, "POINT")

开始和结束只是 data.frame 的第一行和最后一行

Start and end are simply the first and last row of the data.frame

start <- pt[1,]
end <- pt[nrow(pt),]

绘图 - 绿色是起点,红色是终点

plot - green is start point, red is end point

library(ggplot2)
ggplot() +
  geom_sf(data = line) +
  geom_sf(data = start, color = 'green') +
  geom_sf(data = end, color = 'red') +
  coord_sf(datum = NULL)

这篇关于R中的空间线起点和终点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆