I am new to R and working with census data and I am trying to build a csv file that can be passed to another team that shows the lat/long outline of census blocks.
With this I can get to 3 specific census blocks in Florida.
FL_blocks <- blocks("FL", year = 2010, )
FL_blocks_Alachua <- filter(FL_blocks, COUNTYFP == "001")
NTIA_FL_CB <- FL_blocks_Alachua %>%
filter(FL_blocks_Alachua$GEOID10 %in% c ("120010019071008", "120010019071007", "120010019071009"))
Now I would like to make a table that just shows the lat/long and the corresponding census block that lat/long is associated with. This will give me the list of the lat/long for each census block and I can plot them with the polygon of the census block, but for me to pass this to a team unfamiliar with R, I need the csv data output.
NTIA_FL_CB_Shape_xy <- as.data.frame(st_coordinates(NTIA_FL_CB$geometry))
NTIA_FL_CB_Shape_xy <- NTIA_FL_CB_Shape_xy %>%
rename( Longitude = X, Latitude = Y) %>%
select(Latitude,Longitude )
# save lat/long as csv
st_write(NTIA_FL_CB_Shape_xy, "fl_3cb_ntia_latlong.csv", coords = TRUE)
# plot the 3 census blocks with the outline of the shapefiles marked as green circles
leaflet(NTIA_FL_CB) %>%
addTiles() %>%
addPolygons(popup = ~GEOID10) %>%
addCircleMarkers(data = NTIA_FL_CB_Shape_xy, color = "green")
CodePudding user response:
The trick is to cast to point, then export. Or, if your team is using GIS software, you could write your multipolygon to a shapefile?
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
nc_pt <- st_cast(nc, "POINT")
st_write(nc_pt, "nc_pt.csv", layer_options="GEOMETRY=AS_XY")
# st_write(nc, "nc.shp")