[英]Spatial line start and end point in R
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
. 我正在尝试使用
sp
包访问线串的起点和终点,类似于使用psql
将产生ST_StartPoint
和ST_EndPoint
。
No matter how I try to access the line, I get errors or NULL value: 无论我如何尝试访问该行,都会出现错误或NULL值:
> 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
The only solution that works is verbose and requires conversion to SpatialLines
, and I can only easily get the first point: 唯一
SpatialLines
解决方案是冗长的,需要转换为SpatialLines
,我只能很容易地SpatialLines
第一点:
test = as(onetrip, "SpatialLines")@lines[[1]]
> test@Lines[[1]]@coords[1,]
[1] -122.42258 37.79494
Both the str()
below and a simple plot(onetrip)
show that my dataframe is not empty. 下面的
str()
和简单的plot(onetrip)
表明我的数据plot(onetrip)
不是空的。
What is the workaround here - how would one return the start and endpoints of a linestring in sp
? 这里的解决方法是什么-如何在
sp
返回线串的起点和终点?
I have subset the first record of a larger SpatialLinesDataFrame
: 我有一个较大的
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"
Since you tagged question with sf as well, I'll provide a solution in sf. 由于您也用sf标记了问题,因此我将在sf中提供解决方案。 Note you can transform your sp object to sf using
注意,您可以使用以下命令将sp对象转换为sf
library(sf)
st_as_sf(sp_obj)
Create linestring 创建线串
line <- st_as_sfc(c("LINESTRING(0 0 , 0.5 1 , 1 1 , 1 0.3)")) %>%
st_sf(ID = "poly1")
Convert each vertex to point 将每个顶点转换为点
pt <- st_cast(line, "POINT")
Start and end are simply the first and last row of the data.frame 开始和结束只是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)
Always provide some example data: 始终提供一些示例数据:
library(raster)
lns <- spLines(rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60)))
Here are two solutions. 这是两个解决方案。
You can do: 你可以做:
crds <- coordinates(as(lns, 'SpatialPoints'))
pts <- crds[c(1, nrow(crds)), ]
Or do: 或执行:
pts <- geom(lns)[c(1, nrow(g)), c('x', 'y')]
And to look at it 并看一下
plot(lns)
points(pts, col=c('red', 'blue'), pch=20, cex=2)
In sf
a LINESTRING
is a matrix. 在
sf
, LINESTRING
是矩阵。
Unlist the geometry of an sf
object and convert to matrix, then fetch whichever rows you want 取消列出
sf
对象的几何图形并转换为矩阵,然后获取所需的任何行
library(sf)
sfc_line <- st_as_sfc(c("LINESTRING(0 0 , 0.5 1 , 1 1 , 1 0.3)"))
sf_line <- st_sf(geometry = sfc_line)
m <- matrix( unlist( st_geometry(sfc_line) ), ncol = 2)
m[c(1, nrow(m)), ]
# [,1] [,2]
# [1,] 0 0.0
# [2,] 1 0.3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.