Skip to content

Mapping census tracts with leaflet(): "sf layer has inconsistent datum" error

2 messages · Kevin Zembower, Ben Tupper

#
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
#
Hi,

This is a great place to ask - and nice reproducible code!

You have a two issues as far as I can see.  First it seems you have dropped
the "4" in "WGS84".  And second you want to transform rw_pop, not the
output of leaflet().  The following works for me.

## Start
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 %>%
    st_transform('+proj=longlat +datum=WGS84') %>%
    leaflet() %>%
    fitBounds(-76.616, 39.352, -76.610, 39.346) %>%
    addTiles() %>%
    addPolygons()
)
## End

On Wed, May 31, 2023 at 4:03?PM Kevin Zembower via R-sig-Geo <
r-sig-geo at r-project.org> wrote: