I am working with the R programming language.
Suppose I have the following shapefile:
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))
I can convert and save this file in csv format:
pts = st_cast(nc,"POINT")
write.csv(pts, "pts.csv")
Now, I want to convert this back into a shapefile:
my_data = read.csv("pts.csv")
my_sf <- st_as_sf(my_data, coords = c("x_coord", "y_coord"), crs = 4326)
However, I don't think I have done this correctly:
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))
I get the following error:
Error in to_ring.default(x) :
Don't know how to get polygon data from object of class XY,POINT,sfg
Can someone please show me how to fix this - how can I reliably convert shapefiles into dataframes and vice versa?
Thanks!
Note:
I thought that perhaps this might be an alternate method:
dput(nc, 'nc.txt')
a = read.delim("nc.txt")
But I still run into the same problem - how do I convert "a" into a shapefile format?
CodePudding user response:
You can use st_write()
/ st_read()
with csv-files and apply GDAL options to store geometries as Well Known Text strings when writing. Defining GEOM column when reading the csv is also done through GDAL options.
Works with polygons (and multipolygons) too so there is no need to cast to points.
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()
Created on 2023-01-13 with reprex v2.0.2
Sample from resulting 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