Home > Blockchain >  Why am I unable to build data frame with census block and lat/long outline?
Why am I unable to build data frame with census block and lat/long outline?

Time:09-22

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")

This image shows what I was thinking, If I could pass the lat/longs outlining each census block the team that uses my csv file output should be able to load that into their GIS software to overlay.

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")

https://gis.stackexchange.com/questions/294921/convert-shapefile-into-lat-long-points-and-export-it-as-csv-in-r

  • Related