简体   繁体   English

如何将数据框几何体保存到 R 中的 shapefile

[英]How to save dataframe geometry to a shapefile in R

I want to save route_dhdb_sf in SHP file, it looks like this :我想将route_dhdb_sf保存在 SHP 文件中,它看起来像这样:

route_dhdb_sf 表

My problem is the geometry column.我的问题是几何列。
The R code : R代码:

library(sf)
library(ggplot2)
library(stplanr)

# Read shapefile
nl_rails_sf <- sf::st_read("~/netherlands-railways-shape/railways.shp")

# Data frame with station locations
stations_df <- data.frame(station = c("Den Haag", "Den Bosch"), 
                          lat = c(52.080276, 51.690556), 
                          lon = c(4.325, 5.293611)) 

# Create sf object 
stations_sf <- sf::st_as_sf(stations_df, coords = c("lon", "lat"), crs = 4326)  

# Find shortest route
slnetwork <- SpatialLinesNetwork(nl_rails_sf)
find_nodes <- find_network_nodes(sln = slnetwork, 
                                 x = stations_df$lon, 
                                 y = stations_df$lat, 
                                 maxdist = 2e5)
route_dhdb_df <- data.frame(start = find_nodes[1], end = find_nodes[2])
route_dhdb_sf <- sum_network_links(sln = slnetwork, routedata = route_dhdb_df)

How do I save this route_dhdb_sf to a shape file?如何将此route_dhdb_sf保存到形状文件? #Code of mharinga #mharinga代码

Normally you can use:通常你可以使用:

sf::st_write(route_dhdb_sf,"~/netherlands-railways-shape/route_dhdb_sf.shp") 

If you tried it, and if it did not work (you got an error message), you can try to save to other formats eg geosjon by editing the suffix like this:如果您尝试过,如果它不起作用(您收到一条错误消息),您可以尝试通过编辑后缀来尝试保存为其他格式,例如 geosjon:

sf::st_write(route_dhdb_sf,"~/netherlands-railways-shape/route_dhdb_sf.geojson") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM