从r中的线创建多边形

发布于 2025-01-24 04:41:55 字数 467 浏览 2 评论 0原文

我有一个数据集,其中包含起点的坐标和轨迹的停止点。

首先,我创建了一个SF对象,如下所示,每个点的几何形状为“ line”:

crswgs84 = crs(“ +proj = longlat +datum = wgs84 +no_defs) +ellps = wgs84 +towgs84 = 0,0,0“)

df $ geom = sprintf(“ linestring(%s%s,%s%s)”,df $ start_lon,df $ start_lat,df $ stop_lon,df $ stop_lon,df $ stop_lat,df $ stop_lat ) df_sf = st_as_sf(df,wkt =“ geom”,crs = crswgs84)

然后,当我绘制几何形状时,我获得了所有线的图,这是我寻找的结果,因为我想从一开始就拥有轨迹,停止。但是,我希望能够连接线的终点以构建多边形。

有可能吗?

谢谢

I have a dataset that contains coordinates of the start point and the stop point of a trajectory.

First, I have created a sf object as follow to have the geometry of each point as "line" :

CRSWGS84 = CRS("+proj=longlat +datum=WGS84 +no_defs
+ellps=WGS84 +towgs84=0,0,0")

df$geom = sprintf("LINESTRING(%s %s, %s %s)", df$start_lon, df$start_lat, df$stop_lon, df$stop_lat)
df_sf = st_as_sf(df, wkt = "geom", crs = CRSWGS84)

Then when I plot the geometry I obtain a graph of all the lines which is the result i am looking for since I want to have the trajectory from the start to the stop. However, I would like to be able to connect the end point of the lines in order to construct a polygon.

Is it possible to do it ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2025-01-31 04:41:55

当然,这不仅可能,而且非常简单。您需要做的就是sf :: ST_CAST()您的行对象类型“ Polygon”

考虑此代码,线对象是从

library(sf)

#creating data example
id <- c("844", "844", "844", "844", "844","855", "855", "855", "855", "855")

lat <- c(-30.6456, -29.5648, -28.6667, -31.5587, -30.6934, -29.3147, -28.0538, 
         -26.5877, -26.6923, -27.40865)
long <- c(-50.4879, -49.8715, -51.8716, -50.4456, -50.9842, -51.9787, -47.2343, 
          -49.2859, -48.19599, -49.64302)

df <- data.frame(id = as.factor(id), lat, long)

#converting to sf
df.sf <- df %>% 
  sf::st_as_sf(coords = c("long", "lat"), crs = 4326)

#creating linestrings
df.line <- df.sf %>% 
  dplyr::group_by(id) %>%
  dplyr::summarize() %>%
  sf::st_cast("LINESTRING") 


plot(df.line)

“>计数sf linestress在r

# cast to polygons
polygon <- df.line %>% 
  st_cast("POLYGON")

plot(polygon)

”在此处输入图像说明”

Sure, it is not only possible but also quite straightforward. All you need to do is sf::st_cast() your line object to geometry type "POLYGON".

Consider this code, with the lines object borrowed from Count number of times that sf linestrings intersect grid cell in r

library(sf)

#creating data example
id <- c("844", "844", "844", "844", "844","855", "855", "855", "855", "855")

lat <- c(-30.6456, -29.5648, -28.6667, -31.5587, -30.6934, -29.3147, -28.0538, 
         -26.5877, -26.6923, -27.40865)
long <- c(-50.4879, -49.8715, -51.8716, -50.4456, -50.9842, -51.9787, -47.2343, 
          -49.2859, -48.19599, -49.64302)

df <- data.frame(id = as.factor(id), lat, long)

#converting to sf
df.sf <- df %>% 
  sf::st_as_sf(coords = c("long", "lat"), crs = 4326)

#creating linestrings
df.line <- df.sf %>% 
  dplyr::group_by(id) %>%
  dplyr::summarize() %>%
  sf::st_cast("LINESTRING") 


plot(df.line)

enter image description here

# cast to polygons
polygon <- df.line %>% 
  st_cast("POLYGON")

plot(polygon)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文