[R-sig-Geo] Mapping census tracts with leaflet(): "sf layer has inconsistent datum" error

Kevin Zembower kev|n @end|ng |rom zembower@org
Wed May 31 22:02:39 CEST 2023


Hello, all. Newbie to sf, tidycensus and the tidyverse here.

First off, is this the appropriate list to ask this question? If not, 
let me know and I'll go away.

I'm trying to map census blocks for my neighborhood to a base map. I'm 
using tidycensus to get the geometry of the census blocks, and leaflet 
to map them to the OSM base maps. Mostly, this is going really well, and 
I'm very pleased with the speed of development (I just started this 
morning) and results.

However, I get this error:

  Warning message:
sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'

I think I need to use st_transform, but can't get it to work.

Here's a reproducible example, with some commented out lines of what 
I've tried:

## Reproducible example:
library(tidyverse)
library(tidycensus)
library(leaflet)
library(sf)

rw_blocks <- c(3000, 3001, 3002, 3005, 3006, 3007, 3008, 3009, 3010, 3011)

rw_pop <- get_decennial(
     geography = "block",
     variables = "P1_001N",
     year = 2020,
     state = "MD",
     county = "Baltimore city",
     geometry = TRUE
) %>%
     filter(substr(GEOID, 6, 11) == "271101" &
            substr(GEOID, 12, 15) %in% rw_blocks
            ) ## %>% st_transform('+proj=longlat +datum=WGS8')

(rw_pop_map <- rw_pop %>%
     leaflet() %>%
      ## st_transform('+proj=longlat +datum=WGS8') %>%
     fitBounds(-76.616, 39.352, -76.610, 39.346) %>%
     addTiles() %>%
     addPolygons()
     )
## Error occurs when executing above block

Can anyone offer me a hint as to how to resolve this error?

Thanks so much for any advice and guidance.

-Kevin



More information about the R-sig-Geo mailing list