将数据帧转换为形状文件

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

假设我有以下形状文件:

library(sf)  
library(leaflet)
library(leafgl)
library(colourvalues)
library(leaflet.extras)


nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>% 
  st_transform(st_crs(4326)) %>% 
  st_cast('POLYGON')

# code for plot/visualization - works
leaflet(data = nc) %>% addPolygons( stroke = FALSE) %>% addTiles(group = "OSM") %>%  addProviderTiles(provider = providers$OpenStreetMap) %>% addPolygons(data = nc, weight=1, popup = ~NAME,
                label = ~NAME, group = "name", col = 'blue') %>% 
    addSearchFeatures(targetGroups  = 'name', options = searchFeaturesOptions(zoom=10, openPopup=TRUE))

enter image description here

我可以将此文件转换并保存为 csv 格式:

pts = st_cast(nc,"POINT")
write.csv(pts, "pts.csv")

现在,我想将其转换回形状文件:

my_data = read.csv("pts.csv")
my_sf <- st_as_sf(my_data, coords = c("x_coord", "y_coord"), crs = 4326)

但是,我认为我做得不对:

leaflet(data = my_sf) %>% addPolygons( stroke = FALSE) %>% addTiles(group = "OSM") %>%  addProviderTiles(provider = providers$OpenStreetMap) %>% addPolygons(data = my_sf, weight=1, popup = ~NAME,
                label = ~NAME, group = "name", col = 'blue') %>% 
    addSearchFeatures(targetGroups  = 'name', options = searchFeaturesOptions(zoom=10, openPopup=TRUE))

我收到以下错误:

 Error in to_ring.default(x) : 
  Don't know how to get polygon data from object of class XY,POINT,sfg

有人可以告诉我如何解决这个问题 - 如何可靠地将形状文件转换为数据帧,反之亦然?

注:

我想这也许是另一种方法:

dput(nc, 'nc.txt')
a = read.delim("nc.txt")

但我仍然遇到同样的问题 - 如何将“a”转换为 shapefile 格式?

r geospatial r-leaflet
1个回答
3
投票

您可以在 csv 文件中使用

st_write()
/
st_read()
并应用 GDAL 选项 在写入时将几何图形存储为众所周知的文本字符串。读取 csv 时定义 GEOM 列也是通过 GDAL 选项完成的。 也适用于多边形(和多边形),因此无需转换为点。

library(sf)  
library(ggplot2)

nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>% 
  st_transform(st_crs(4326)) %>% 
  st_cast('POLYGON')

st_write(nc, "nc.csv", layer_options = "GEOMETRY=AS_WKT")
nc_r <- st_read("nc.csv", options = "GEOM_POSSIBLE_NAMES=WKT") %>% st_set_crs(st_crs(4326))

ggplot() +
  geom_sf(data = nc, fill = "gray95", color = NA) +
  geom_sf(data = nc_r, fill = NA, color = "red") +
  theme_void()

创建于 2023-01-13,使用 reprex v2.0.2

生成的 CSV 示例:

WKT,AREA,PERIMETER,CNTY_,CNTY_ID,NAME,FIPS,FIPSNO,CRESS_ID,BIR74,SID74,NWBIR74,BIR79,SID79,NWBIR79
"POLYGON ((-81.4725795258847 36.234442085901,-81.5406662655851 36.2725922530127,-81.5618079701738 36.2736794519427,-81.6328930941132 36.3407711908437,-81.7409056714829 36.3918682608663,-81.6981108960788 36.4718609196702,-81.7026275343611 36.5194216957795,-81.6698275935862 36.589729047258,-81.3451181345896 36.5729442074843,-81.3473613275856 36.5379945946803,-81.3245945636018 36.513760233656,-81.3131425637387 36.4807789513687,-81.2660525812077 36.4372883204196,-81.2626574707298 36.4051233850847,-81.2405087118766 36.3795005067122,-81.2397076336137 36.3654474229537,-81.2640614459358 36.3524968049871,-81.3288140981986 36.3635860212039,-81.3611942759508 36.3532446189095,-81.3655126794677 36.3391343240985,-81.3539538789202 36.299801700754,-81.3672752624154 36.2787831341042,-81.4062092537836 36.2851383583202,-81.4121527764642 36.2673774711126,-81.4308606459085 36.2608048993933,-81.4527119558761 36.2396719077509,-81.4725795258847 36.234442085901))",0.114,1.442,1825,1825,Ashe,"37009",37009,"5",1091,1,10,1364,0,19
"POLYGON ((-81.2397076336137 36.3654474229537,-81.2405087118766 36.3795005067122,-81.2626574707298 36.4051233850847,-81.2660525812077 36.4372883204196,-81.3131425637387 36.4807789513687,-81.3245945636018 36.513760233656,-81.3473613275856 36.5379945946803,-81.3451181345896 36.5729442074843,-80.9032480426982 36.5652913699116,-80.9333547348259 36.4983943021582,-80.9655823739946 36.4673013309186,-80.9494763523345 36.41481585846,-80.9561980841925 36.4038793356529,-80.9777594517854 36.3914589472921,-80.9826500931076 36.3719166255106,-81.0025863311759 36.3667630976206,-81.0244528261178 36.3779170990789,-81.0426112687674 36.4104177433147,-81.0840630068357 36.4300021008903,-81.0983761918057 36.4312342478025,-81.1131242886965 36.4229336438147,-81.1291922834301 36.4264126083276,-81.1382105282358 36.4177076449314,-81.1531798474363 36.424821946587,-81.1764883926017 36.4155257382867,-81.2397076336137 36.3654474229537))",0.061,1.231,1827,1827,Alleghany,"37005",37005,"3",487,0,10,542,3,12
"POLYGON ((-80.4561372469537 36.2426419018237,-80.4761802535541 36.2548105906187,-80.5366755869653 36.2568210021794,-80.5448087209088 36.2767409891564,-80.5539490134887 36.2785148055681,-80.5903957842538 36.2683603351409,-80.6241111085137 36.2731820786618,-80.6672414784133 36.2461860486854,-80.6964552999604 36.2591748963368,-80.7238380450725 36.2585569727322,-80.7341685487261 36.2648434923156,-80.7523652336625 36.2583815473557,-80.7660985595122 36.2619215423113,-80.7825022983166 36.2486619321941,-80.874187603624 36.2339681463458,-80.8706701187362 36.3247066347047,-80.88872938057 36.3545103355607,-80.9243749703149 36.372835856588,-80.9561980841925 36.4038793356529,-80.9494763523345 36.41481585846,-80.9655823739946 36.4673013309186,-80.9333547348259 36.4983943021582,-80.9032480426982 36.5652913699116,-80.8379612911977 36.5635174612032,-80.6108500712286 36.5573755304548,-80.4351015327773 36.5511230525697,-80.4528030601279 36.2571718031137,-80.4561372469537 36.2426419018237))",0.143,1.63,1828,1828,Surry,"37171",37171,"86",3188,5,208,3616,6,260
© www.soinside.com 2019 - 2024. All rights reserved.