0

The NHGIS website has a fantastic wealth of US boundary shapefiles. These files can be filtered and downloaded here.

My problem is that after bringing these shapefile(s) into R and then converting them into GeoJSON files, the GeoJSON files do not display when I add them to a Leaflet object/map.

I have gone out and found other shapefile data of the same census year, used the process mentioned above, and then successfully displayed the GeoJSON file on a Leaflet map.

So I am confused as to why the NHGIS shapefiles are not converting correctly, while other shapefiles from other sources are. Both sources have all the necessary files for shapefiles (.DBF, .PRJ, .SBN, .SBX, etc...).

My code (that does not display on a Leaflet) is listed below, along with an image of a successful map with counties in 1790.

A helpful shapefile or GeoJSON converter can be found here; however, after using this GUI converter I still can't get the NHGIS shapefiles to convert correctly.

I am using a shapefile from NHGIS Data Finder and filtering to 1790 and requesting the STATE shapefile.

library(tidyverse)
library(magrittr)
library(rgdal)
library(spdplyr)
library(geojsonio)
library(rmapshaper)

# Import Shapefile into R.
us = rgdal::readOGR(dsn="nhgis0004_shape/nhgis0004_shape/nhgis0004_shapefile_tl2000_us_county_1790", 
                layer="US_state_1790", stringsAsFactors = FALSE)
# Filter to get only up to 1790    us %<>%
dplyr::mutate(START_YEAR = lubridate::year(us[["START_DATE"]]),
             END_YEAR = lubridate::year(us[["END_DATE"]]))
us %<>%
   dplyr::filter(END_YEAR > 1790 & START_YEAR <= 1790)

# Convert SP Data Frame to GeoJSON.
us_json <- geojson_json(us)

# Simplify the geometry information of GeoJSON.
us_json_simplified <- ms_simplify(us_json)

# Keep only the polygons inside the bbox (boundary box).
us_json_clipped <- ms_clip(us_json_simplified, bbox = c(-170, 15, -55, 72))

# Save it to a local file system.
geojson_write(us_json_clipped, file = "~/Downloads/us.geojson")

leaflet::leaflet() %>% 
   leaflet::addTiles() %>%
   leaflet::setView(lng = -98.583, lat = 39.833, zoom = 3) %>% 
   leaflet::addGeoJSON(us_json_clipped)

enter image description here

  • Showing the output of summary(spatialthing) in questions can be very useful in helping us diagnose problems without having to download data! – Spacedman Nov 11 '17 at 13:53
0

The source is in a different CRS:

library(sf)

us <- st_read("~/data/nhgis0001_shape/nhgis0001_shapefile_tl2000_us_state_1790/US_state_1790.shp",
              layer="US_state_1790", stringsAsFactors = FALSE)
## Reading layer `US_state_1790' from data source `/Users/bob/data/nhgis0001_shape/nhgis0001_shapefile_tl2000_us_state_1790/US_state_1790.shp' using driver `ESRI Shapefile'
## Simple feature collection with 16 features and 7 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 93886.94 ymin: -701250.3 xmax: 2258225 ymax: 1406222
## epsg (SRID):    NA
## proj4string:    +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs

So, you'll have to transform it first:

us <- st_transform(us, 4326)

Then do your munging so your bbox works as expected:

us_json <- geojsonio::geojson_json(us)
us_json_clipped <- rmapshaper::ms_clip(us_json, bbox = c(-170, 15, -55, 72))
us_json_simplified <- rmapshaper::ms_simplify(us_json)
geojsonio::geojson_write(us_json_simplified, file = "~/Downloads/us.geojson")

The resultant file is 915,868 bytes on my system and in the coordinate system you expect.

You can get similar results with (mostly) just sf functions:

us <- st_read("~/data/nhgis0001_shape/nhgis0001_shapefile_tl2000_us_state_1790/US_state_1790.shp",
              layer="US_state_1790", stringsAsFactors = FALSE)

us <- st_transform(us, 4326)
us_bbox <- st_set_crs(st_as_sf(as(raster::extent(-170, -55, 15, 72), "SpatialPolygons")), st_crs(us))
us_clipped <- st_intersection(us, us_bbox)
us_simplified <- st_simplify(us_clipped, preserveTopology = TRUE, dTolerance = 0.01)
geojsonio::geojson_write(us_simplified, file = "~/Data/us.geojson")
  • Great intuition! Def run with those hunches! – hrbrmstr Nov 13 '17 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.