Giter Club home page Giter Club logo

tigris's Introduction

tigris

R build status CRAN Badge CRAN Downloads

tigris is an R package that allows users to directly download and use TIGER/Line shapefiles (https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-line-file.html) from the US Census Bureau.

To install the package from CRAN, issue the following command in R:

install.packages('tigris')

Or, get the development version from GitHub:

devtools::install_github('walkerke/tigris')

tigris functions return simple features objects with a default year of 2021. To get started, choose a function from the table below and use it with a state and/or county if required. You'll get back an sf object for use in your mapping and spatial analysis projects:

library(tigris)
library(ggplot2)

manhattan_roads <- roads("NY", "New York")

ggplot(manhattan_roads) + 
  geom_sf() + 
  theme_void()

tigris only returns feature geometries for US Census data which default to the coordinate reference system NAD 1983 (EPSG: 4269). For US Census demographic data (optionally pre-joined to tigris geometries), try the tidycensus package. For help deciding on an appropriate coordinate reference system for your project, take a look at the crsuggest package.

To learn more about how to use tigris, read Chapter 5 of the book Analyzing US Census Data: Methods, Maps, and Models in R.

Python users may also be interested in pygris, a Python port of the tigris package.

Available datasets:

Please note: cartographic boundary files in tigris are not available for 2011 and 2012.

Function Datasets available Years available
nation() cartographic (1:5m; 1:20m) 2013-2022
divisions() cartographic (1:500k; 1:5m; 1:20m) 2013-2022
regions() cartographic (1:500k; 1:5m; 1:20m) 2013-2022
states() TIGER/Line; cartographic (1:500k; 1:5m; 1:20m) 1990, 2000, 2010-2022
counties() TIGER/Line; cartographic (1:500k; 1:5m; 1:20m) 1990, 2000, 2010-2022
tracts() TIGER/Line; cartographic (1:500k) 1990, 2000, 2010-2022
block_groups() TIGER/Line; cartographic (1:500k) 1990, 2000, 2010-2022
blocks() TIGER/Line 2000, 2010-2022
places() TIGER/Line; cartographic (1:500k) 2011-2022
pumas() TIGER/Line; cartographic (1:500k) 2012-2022
school_districts() TIGER/Line; cartographic 2011-2022
zctas() TIGER/Line; cartographic (1:500k) 2000, 2010, 2012-2022
congressional_districts() TIGER/Line; cartographic (1:500k; 1:5m; 1:20m) 2011-2022
state_legislative_districts() TIGER/Line; cartographic (1:500k) 2011-2022
voting_districts() TIGER/Line 2012, 2020
area_water() TIGER/Line 2011-2022
linear_water() TIGER/Line 2011-2022
coastline TIGER/Line() 2013-2022
core_based_statistical_areas() TIGER/Line; cartographic (1:500k; 1:5m; 1:20m) 2011-2022
combined_statistical_areas() TIGER/Line; cartographic (1:500k; 1:5m; 1:20m) 2011-2022
metro_divisions() TIGER/Line 2011-2022
new_england() TIGER/Line; cartographic (1:500k) 2011-2022
county_subdivisions() TIGER/Line; cartographic (1:500k) 2010-2022
urban_areas() TIGER/Line; cartographic (1:500k) 2012-2022
primary_roads() TIGER/Line 2011-2022
primary_secondary_roads() TIGER/Line 2011-2022
roads() TIGER/Line 2011-2022
rails() TIGER/Line 2011-2022
native_areas() TIGER/Line; cartographic (1:500k) 2011-2022
alaska_native_regional_corporations() TIGER/Line; cartographic (1:500k) 2011-2022
tribal_block_groups() TIGER/Line 2011-2022
tribal_census_tracts() TIGER/Line 2011-2022
tribal_subdivisions_national() TIGER/Line 2011-2022
landmarks() TIGER/Line 2011-2022
military() TIGER/Line 2011-2022

tigris's People

Contributors

badbayesian avatar bocinsky avatar dteck avatar hrbrmstr avatar josiahparry avatar landisrm avatar larcat avatar lpiep avatar mpaulacaldas avatar nmpeterson avatar sheffe avatar walkerke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tigris's Issues

places() fails with multiple states

While other shape file functions can accept multiple states, or get all states if the states value is not specififed, the places() function only accepts a single state and must not be null. If given a vector of more than 1 state, it uses the first state's FIPS code for each state.

shape.places <- places(state = datasets::state.abb, cb = T)
Using FIPS code '19' for state 'AL'Using FIPS code '19' for state 'AK'Using FIPS code '19' for state 'AZ'Using FIPS code '19' for state 'AR'Using FIPS code '19' for state 'CA'Using FIPS code '19' for state 'CO'Using FIPS code '19' for state 'CT'Using FIPS code '19' for state 'DE'Using FIPS code '19' for state 'FL'Using FIPS code '19' for state 'GA'Using FIPS code '19' for state 'HI'Using FIPS code '19' for state 'ID'Using FIPS code '19' for state 'IL'Using FIPS code '19' for state 'IN'Using FIPS code '19' for state 'IA'Using FIPS code '19' for state 'KS'Using FIPS code '19' for state 'KY'Using FIPS code '19' for state 'LA'Using FIPS code '19' for state 'ME'Using FIPS code '19' for state 'MD'Using FIPS code '19' for state 'MA'Using FIPS code '19' for state 'MI'Using FIPS code '19' for state 'MN'Using FIPS code '19' for state 'MS'Using FIPS code '19' for state 'MO'Using FIPS code '19' for state 'MT'Using FIPS code '19' for state 'NE'Using FIPS code '19' for state 'NV'Using FIPS code '19'... <truncated>
  |=================================================================================================================================| 100%
Warning messages:
1: In if (grepl("^[[:digit:]]+$", state)) { :
  the condition has length > 1 and only the first element will be used
2: In if (grepl("^[[:alpha:]]+", state)) { :
  the condition has length > 1 and only the first element will be used
3: In if (nchar(state) == 2 & state %in% fips_state_table$abb) { :
  the condition has length > 1 and only the first element will be used
4: In fips_state_table$abb == state :
  longer object length is not a multiple of shorter object length
5: In fips_state_table$abb == state :
  longer object length is not a multiple of shorter object length

tilde expansion for cache directory

On Windows (with options(tigris_use_cache = TRUE)) , this gives me an error:

Sys.setenv(TIGRIS_CACHE_DIR = "~/.tigris_cache")
ca_districts <- school_districts(state = "06")
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open data source

But this works fine:

Sys.setenv(TIGRIS_CACHE_DIR = path.expand("~/.tigris_cache"))
ca_districts <- school_districts(state = "06")

It looks like this happens because rgdal::readOGR does not expand the path (so I can recreate the same errors by trying readOGR("~/.tigris_cache", "tl_2016_06_unsd")). The problem only occurs when importing to class sp, so this works fine:

Sys.setenv(TIGRIS_CACHE_DIR = "~/.tigris_cache")
ca_districts <- school_districts(state = "06", class = "sf")

GET requests rejected

At the moment on my computer, GET requests by tigris are being rejected, meaning that functions will fail when tigris_use_cache == TRUE. download.file(), used otherwise, is working fine.

I've put in a note to the Census Bureau to see if there is a reason for this behavior - otherwise I'll need to modify the code to use download.file() instead.

URL not constructing properly for PUMA files

Using 0.5.1 on macOS, R 3.4:

gapuma <-  pumas("Georgia")
## Using FIPS code '13' for state 'Georgia'
## trying URL 'http://www2.census.gov/geo/tiger/TIGER%s/PUMA/tl_%s_%s_puma10.zip2015201513'
## Error in download.file(url, tiger_file, mode = "wb") : 
##   cannot open URL 'http://www2.census.gov/geo/tiger/TIGER%s/PUMA/tl_%s_%s_puma10.zip2015201513'
## In addition: Warning message:
##   In download.file(url, tiger_file, mode = "wb") :
##   cannot open URL 'https://www2.census.gov/geo/tiger/TIGER%s/PUMA/tl_%s_%s_puma10.zip2015201513': HTTP status was '400 Bad Request'

Traveling to the URL confirms that the page isn't there. I did find the file I needed at https://www2.census.gov/geo/tiger/TIGER2012/PUMA/tl_2012_13_puma10.zip. It seems as though the url constructor isn't building the string correctly.

It works, however, if I turn cb = TRUE

'roads' function fails to download specific county.

I am using your package to calculate county road density for every U.S. county. I have processed 2328 counties but the function breaks on one specific county in South Dakota. South Dakota counties before and after appear to work. Code and session info below.

Great package!

st.val="South Dakota"
fips.val="113"

#Read Roads for state and county
in.roads <- roads( st.val, fips.val, year=2015)
Using FIPS code '46' for state 'South Dakota'
Previous download failed. Re-download attempt 1 of 3...
Downloading: 150 kB
Previous download failed. Re-download attempt 2 of 3...
Downloading: 150 kB
Previous download failed. Re-download attempt 3 of 3...
Downloading: 150 kB
Error: Download failed; check your internet connection or the status of the Census Bureau website
at http://www2.census.gov/geo/tiger/.

sessionInfo(package="tigris")
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
attached base packages:
character(0)
other attached packages:
[1] tigris_0.5.3

[ENHANCEMENT] Caching

I haven't pushed it to my fork yet, but I'm thinking that we control the caching & refresh with two options:

  • options(tigris_refresh=FALSE)
  • options(tigris_use_cashe=TRUE)

users can set these right after they load the package but these wld be the .onLoad defaults.

If that's cool, I'll try to push the PR tonight/tomorrow.

Comprehensive school district shapefile for a state.

Hi, I want to implement this, and think I know how.

Two quick questions:

  1. Any problem with me introducing a new potential value for the "tigris" attribute? It would be "sdall" or similar.

  2. Any issue with renaming (with an explicit call out maybe) the UNSDLEA, SCSDLEA, ELSDLEA columns to "SDLEA" if the user uses the "all" argument for school_district(type = ...)?

Example of what I'm trying to do, and an example of the sort of workaround I want to implement in school_district()

> ky_unified <- school_districts(state = 'ky', type = 'unified')
Using FIPS code '21' for state 'KY'
> ky_sec <- school_districts(state = 'ky', type = 'secondary')
Using FIPS code '21' for state 'KY'
> temp <- rbind_tigris(ky_unified, ky_sec)
Error: Objects must all be Spatial*DataFrame objects and all the same type of tigris object.
> attr(ky_unified, "tigris") <- "sdall"
> attr(ky_sec, "tigris") <- "sdall"
> temp <- rbind_tigris(ky_unified, ky_sec)
Error in match.names(clabs, names(xi)) : 
  names do not match previous names
> names(ky_unified)[2] <- "SDLEA"
> names(ky_sec)[2] <- "SDLEA"
> temp <- rbind_tigris(ky_unified, ky_sec)
> head(as.data.frame(temp))
STATEFP SDLEA   GEOID                              NAME LSAD LOGRADE HIGRADE MTFCC SDTYP FUNCSTAT      ALAND
4015f6da0fef11e799e7e75995afe6670      21 00030 2100030      Adair County School District   00      PK      12 G5420  <NA>        E 1049678094
4015f6da0fef11e799e7e75995afe6671      21 00070 2100070      Allen County School District   00      PK      12 G5420  <NA>        E  891830403
4015f6da0fef11e799e7e75995afe6672      21 00078 2100078   Fort Campbell Dependent Schools   00      KG      12 G5420     B        E  146020316
4015f6da0fef11e799e7e75995afe6673      21 00079 2100079       Fort Knox Dependent Schools   00      KG      12 G5420     B        E  439748749
4015f6da0fef11e799e7e75995afe6674      21 00081 2100081 Muhlenberg County School District   00      PK      12 G5420  <NA>        E 1209734539
4015f6da0fef11e799e7e75995afe6675      21 00120 2100120   Anderson County School District   00      PK      12 G5420  <NA>        E  522745701
AWATER    INTPTLAT     INTPTLON
4015f6da0fef11e799e7e75995afe6670 18430783 +37.1055589 -085.2813796
4015f6da0fef11e799e7e75995afe6671 19492346 +36.7507703 -086.1924580
4015f6da0fef11e799e7e75995afe6672   197739 +36.6776414 -087.7370236
4015f6da0fef11e799e7e75995afe6673  6974504 +37.9058675 -085.8884215
4015f6da0fef11e799e7e75995afe6674 31906703 +37.2138159 -087.1340919
4015f6da0fef11e799e7e75995afe6675  6311536 +38.0053963 -084.9864171

Thanks!

Silent Data truncation with GDAL 2.x

GDal 2.x introduced int64, which R doesn't support (as of yet).
rgdal::readOGR changes the way it interprets numeric fields with values outside int32 range ( outside -2^32 to +2^31). with Gdal 1.x as there was no int64 the fields were interpreted as Real and converted to numeric, but if Gdal 2.x, any numeric field beyond int32 is handled using the 'interger64' argument of readOGR.

The current default is 'allow.loss' which silently truncates anything outside int32 range. This means the ALAND, AWATER fields of most shapefiles have truncated data.

https://gist.github.com/bhaskarvk/686deca51e3e81cb785ed8f2430ac2fc

Proposed workaround:-
Add integer64='no.loss' to the readOGR call, and then convert the chars for these fields to numeric. I doubt any of these fields will have AREA > 2^53 (MAX double), so the char to numeric (double) conversion should not result in any loss of data.

rgdal will probably be changed to make 'no.loss' the default option at some point in time, but for now this is a pressing issue especially for anyone using the ALAND/AWATER or for that matter any numeric field with values beyond the int32 range .

Inconsistent datum between 1990/2000 data and later data

Given the way that the data are prepared from the Census Bureau - and how I am handling coordinate systems for the historic, generalized data - there are some slight datum inconsistencies that are causing problems when a user wants to combine data from different years in tidycensus. Take a look at this.

How to set cache dir?

Maybe I'm just a little dense but I've been looking at the docs for ~10 minutes now and come up empty. Can I somehow set the cache directory?

Error when trying to download tracts

Attempts to download tract boundaries for years prior to 2011 are returning the following error:

library(tigris)
#> 
#> Attaching package: 'tigris'
#> The following object is masked from 'package:graphics':
#> 
#>     plot
tigris::tracts(state = "53", county = "033", year = 2010)
#> Warning in unzip(file_loc, exdir = cache_dir, overwrite = TRUE): error 1 in
#> extracting from zip file
#> Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open layer

Census website down for maintenance

An FYI for tigris users: the Census TIGER/Line website has been under maintenance for a couple days, so you currently aren't able to download data. If you are getting an error from tigris, this is why. I'll close the issue when the site is back up.

Feature Request? Downloading individual blocks or blockgroups

Hi, your package is really useful. I've been using it to download census blocks by county and state. Often, however, I need only a few blocks and downloading an entire county can be time consuming. Is there a way to download specific blocks or block groups instead. Thanks so much.

[DISCUSSION] railroads

so, the resultant shapefile from a call to rails() results in a 353.3MB SpatialLinesDataFrame. I've got a killer system and even it struggles to do anything useful with it. Since even the d/l take a bit, perhaps we should take advantage of the know user-wait on first grab to split it into chunks based on state? Perhaps even add state and county params to the function like the others to give the illusion of similar operation to the user. We'd have to add a message saying what's going on to the user for the initial processing and this one would almost require (IMO) mandatory caching vs the optional one now.

congressional_districts references wrong url for cd115

Hi. This is a great package that I use quite a bit -- thanks! Found what I'm guessing is an easy to correct issue:

library(tigris)
cd115 <- congressional_districts(year=2016, refresh=TRUE)

yields the error:

downloaded 0 bytes

Error in download.file(url, tiger_file, mode = "wb") : 
  cannot download all files
In addition: Warning message:
In download.file(url, tiger_file, mode = "wb") :
  URL 'https://www2.census.gov/geo/tiger/TIGER2016/CD/tl_2016_us_cd114.zip': status was '404 Not Found'

...because the url should reference cd_115, not cd_114.

This url is valid: https://www2.census.gov/geo/tiger/TIGER2016/CD/tl_2016_us_cd115.zip

For reference:

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tigris_0.5.3 cbdb_0.1.0   RJDBC_0.2-5  rJava_0.9-8  DBI_0.6-13  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.11.2  lattice_0.20-33 assertthat_0.1  dplyr_0.5.0     sf_0.5-4        rappdirs_0.3.1 
 [7] grid_3.3.1      R6_2.2.1        magrittr_1.5    units_0.4-6     httr_1.2.1      stringi_1.1.1  
[13] uuid_0.1-2      sp_1.2-3        rgdal_1.1-10    tools_3.3.1     stringr_1.2.0   udunits2_0.13  
[19] foreign_0.8-66  maptools_0.8-39 rgeos_0.3-19    tibble_1.2     

TODO list before CRAN submission

  • Add all remaining geographies
  • Give option to download very-generalized cartographic boundary files when applicable
  • Remove warning message (if possible) associated with download progress bar
  • Fully document the functions with descriptions from the Census Bureau
  • Update the package for the imminent 2015 TIGER/Line shapefiles release
  • Finish vignettes. I'm thinking three: one for basic usage, one for visualization, and one for spatial analysis.

Multi-row features in 1990 and 2000 CB files

The 1990 and 2000 cartographic boundary files give a separate row to each part of multipart polygons (e.g. counties with offshore islands). I was keeping this as-is to maintain fidelity to the source files (but correcting in tidycensus) but I am now cleaning this up to keep things consistent throughout the package and because it is likely confusing to users.

This all needs to be corrected before the 0.6 CRAN release.

Example code for ?states does not work

This is the example code when I type ?states

library(tigris)
library(leaflet)

states <- states(detailed=FALSE)

leaflet(states) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = "white",
              color = "black",
              weight = 0.5) %>%
  setView(-98.5795, 39.8282, zoom=3)

When I run it I get the following errors:

> states <- states(detailed=FALSE)
Error in load_tiger(url, tigris_type = "state", ...) : 
  unused argument (detailed = FALSE)
> 
> leaflet(states) %>%
+     addProviderTiles("CartoDB.Positron") %>%
+     addPolygons(fillColor = "white",
+                 color = "black",
+                 weight = 0.5) %>%
+     setView(-98.5795, 39.8282, zoom=3)
Error in polygonData.default(data) : 
  Don't know how to get path data from object of class function

Bug: argument `class = 'sf'` does not consistently return sf object

Great package! I noticed that tigris does not consistently return sf objects when I use the class = 'sf' option. I think it happens when I use a vector of three digit FIPS codes for the county argument. Here is an easy way to reproduce the problem:

test1 <- tigris::blocks(state = "36", county = c("061", "047"), year = 2010, class = "sf")
class(test1)
# ERROR: test1 is class `SpatialPolygonsDataFrame` and not `sf`
test2 <- tigris::blocks(state = "36", county = "061", year = 2010, class = "sf")
class(test2)
## test2 is class `sf` so it works well here

County Maps

Great work on the package, really good idea. But I was wondering, do any of these functions pull the cb_2013_us_county_ files? I looked through the code and couldn’t find it anywhere. I use that one a lot. Is it in there and I just missed it?

ERROR: lazy loading failed for package ‘tigris’

When installing tigris from github using devtools::install_github('walkerke/tigris') I get the following error:

Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object '/home/user_name/R/x86_64-pc-linux-gnu-library/3.4/sf/libs/sf.so': /home/user_name/R/x86_64-pc-linux-gnu-library/3.4/sf/libs/sf.so: undefined symbol: _ZN17GDALDriverManager14GetDriverCountEv ERROR: lazy loading failed for package ‘tigris’ Error: Command failed (1)

Here is my R sessioninfo:

R version 3.4.2 (2017-09-28)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 17.10

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] devtools_1.12.0 lazyeval_0.2.0  rgdal_1.2-15    sp_1.2-5       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13    knitr_1.15.1    magrittr_1.5    maptools_0.9-2  units_0.4-6     rappdirs_0.3.1  lattice_0.20-35 R6_2.2.2       
 [9] udunits2_0.13   httr_1.3.1      tools_3.4.2     grid_3.4.2      e1071_1.6-8     DBI_0.6-1       git2r_0.18.0    withr_2.0.0    
[17] rgeos_0.3-26    class_7.3-14    digest_0.6.12   yaml_2.1.14     curl_3.0        memoise_1.1.0   compiler_3.4.2  classInt_0.1-24
[25] foreign_0.8-69

Thanks.

Generalized vs detailed counties seems odd

I'm using the cb argument to download a generalized and detailed version of a county file. When I look at the object sizes they seem to be about what you'd expect with a bigger object for the detailed layer. BUT.. when I plot them the "generalized" version seems a lot more detailed. Any thoughts about this?

library(tigris)
library(sp)
dtl <- counties(state="Wisconsin", cb=FALSE)
object.size(dtl) #1938576 bytes 
gen <- counties(state="Wisconsin", cb=TRUE)
object.size(gen) #500248 bytes

par(mfrow=c(1,2))
plot(dtl, main="Detailed?")
plot(gen, main="Generalized?")

Here is the plot

capture

"insets" option?

When rendering maps of the 50 states it is common to render AK and HI as insets, rather than in their "real" place. I searched but did not see an option to get maps from the tigris package in this way. Can you please confirm whether or not this is an option or not?

If it's not an option, can you please let me know whether it's on your roadmap?

Thanks for the great package.

Unable to download a couple lower house state legistative districts

I can't seem to download from AL, AK and TX. They all share the following error. I've tried both CRAN version: 0.3.3 and Dev version: 0.3.9.9000.

> library(tigris)
> state_legislative_districts("TX", house = 'lower')
Using FIPS code '48' for state 'TX'
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open layer
In addition: Warning message:
In unzip(file_loc, exdir = cache_dir, overwrite = TRUE) :
  error 1 in extracting from zip file

Error in ogrInfo: Cannot open layer

> library(tigris)

Attaching package:tigrisThe following object is masked frompackage:graphics:

    plot

> me <- counties("Maine", cb = TRUE)
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open layer
In addition: Warning message:
In unzip(file_loc, exdir = cache_dir, overwrite = TRUE) :
  error 1 in extracting from zip file
> session_info()
Session info -------------------------------------------------------------------
 setting  value                       
 version  R version 3.2.4 (2016-03-10)
 system   x86_64, darwin13.4.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       Australia/Melbourne         
 date     2016-03-25                  

Packages -----------------------------------------------------------------------
 package     * version date       source        
 devtools    * 1.10.0  2016-01-23 CRAN (R 3.2.3)
 digest        0.6.9   2016-01-08 CRAN (R 3.2.3)
 foreign       0.8-66  2015-08-19 CRAN (R 3.2.4)
 fortunes      1.5-2   2013-12-14 CRAN (R 3.2.0)
 htmltools     0.3     2015-12-29 CRAN (R 3.2.3)
 htmlwidgets   0.6     2016-02-25 CRAN (R 3.2.3)
 httr          1.1.0   2016-01-28 CRAN (R 3.2.3)
 inline        0.3.14  2015-04-13 CRAN (R 3.2.0)
 lattice       0.20-33 2015-07-14 CRAN (R 3.2.4)
 magrittr      1.5     2014-11-22 CRAN (R 3.2.0)
 maptools      0.8-39  2016-01-30 CRAN (R 3.2.3)
 memoise       1.0.0   2016-01-29 CRAN (R 3.2.3)
 R6            2.1.2   2016-01-26 CRAN (R 3.2.3)
 rappdirs      0.3     2014-09-02 CRAN (R 3.2.0)
 rgdal         1.1-3   2015-12-21 CRAN (R 3.2.4)
 rgeos         0.3-17  2016-02-13 CRAN (R 3.2.4)
 sp            1.2-2   2016-02-05 CRAN (R 3.2.4)
 stringi       1.0-1   2015-10-22 CRAN (R 3.2.0)
 stringr       1.0.0   2015-04-30 CRAN (R 3.2.0)
 tigris      * 0.2.2   2016-02-15 CRAN (R 3.2.3)
 uuid          0.1-2   2015-07-28 CRAN (R 3.2.0)

add curl to the package dependency

we (@bi-sdal ) had a huge issue with downloading data in tigris.
Mainly because files were not being downloaded properly.
A few things we had to to do to get the tigris working (we tested the package with va <- states('51'))

  1. must set options(tigris_use_cache = TRUE), otherwise we get the following error:
> va <- states('51')
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open data source
In addition: Warning message:
In unzip(tiger_file, exdir = tmp) : error 1 in extracting from zip file
  1. install.packages(c("curl", "httr"))
    • we tried to install the github version of the package, but was running in to SSL CA cert problems (see r-lib/devtools#1079)
    • if we do not install these 2 packages we get the following error:
> options(tigris_use_cache = TRUE)
> va <- states('51')              
Previous download failed.  Re-download attempt 1 of 3...
Previous download failed.  Re-download attempt 2 of 3...
Previous download failed.  Re-download attempt 3 of 3...
Error: Download failed; check your internet connection or the status of the Census Bureau website
                 at http://www2.census.gov/geo/tiger/.

Here's the sessionInfo() without installing curl and httr

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS: /usr/lib64/microsoft-r/3.4/lib64/R/lib/libRblas.so
LAPACK: /usr/lib64/microsoft-r/3.4/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tigris_0.5.3         RevoUtilsMath_10.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.12     bindr_0.1        magrittr_1.5     maptools_0.9-2  
 [5] units_0.4-5      rappdirs_0.3.1   uuid_0.1-2       lattice_0.20-35 
 [9] R6_2.2.0         rlang_0.1.1      stringr_1.2.0    udunits2_0.13   
[13] httr_1.2.1       dplyr_0.7.1      tools_3.4.0      rgdal_1.2-8     
[17] grid_3.4.0       DBI_0.7          rgeos_0.3-23     assertthat_0.2.0
[21] tibble_1.3.3     sf_0.5-2         bindrcpp_0.2     curl_2.6        
[25] glue_1.1.1       sp_1.2-5         stringi_1.1.5    compiler_3.4.0  
[29] RevoUtils_10.0.4 foreign_0.8-67   pkgconfig_2.0.1 

cc: @dads2busy

Unable to Download Shape Files

I am new to R - so if this is stupid, my apologies. A recurring error happens when the app attempts to download the share files:

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open data source
In addition: Warning message:
In unzip(file_loc, exdir = cache_dir, overwrite = TRUE) :
error 1 in extracting from zip file
This occurs when I run your example code:

library(tigris)
library(magrittr)
ua <- urban_areas(cb = TRUE)
plot(ua)

I really hope I can resolve this. Your package would be tremendously useful to me!

Plot and sp

I see that you're importing sp but I'm surprised that the following won't work:

library(tigris)
x<-counties(c("RI", "DE"))
plot(x)
#Error in as.double(y) : 
#  cannot coerce type 'S4' to vector of type 'double'

Instead it seems you need to explicitly load sp to get the sp plotting methods.

library(tigris)
library(sp)
x<-counties(c("RI", "DE"))
plot(x)

I think that this could be confusing for relative beginners, perhaps load the sp plotting methods in the package?

attaching tigris pkg overwrites default .Rprofile options

I want to permanently set the obj class that tigris returns as sf, so I added the option to my .Rprofile options: options(... , tigris_class = 'sf').

This works great:

> getOption('tigris_class')
[1] "sf"

But when I attach the package to use it, my already set tigris_* options get overwritten which causes me the additional step to reset the option in the script:

> library(tigris)
As of version 0.5.1, tigris does not cache downloaded data by default. To enable caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
Attaching package: ‘tigris’
The following object is masked from ‘package:graphics’:
    plot

> getOption('tigris_class')
[1] "sp"
> options(tigris_class = 'sf')
> getOption('tigris_class')
[1] "sf"

Thanks!

Session info ------------------
 setting  value                       
 version  R version 3.3.3 (2017-03-06)
 system   x86_64, mingw32             
 ui       RStudio (1.0.143)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/Chicago             
 date     2017-06-29                  

Packages ------------------------
 package    * version  date       source        
...
 sf           0.5-1    2017-06-23 CRAN (R 3.3.3)
 tidycensus   0.1.2    2017-06-20 CRAN (R 3.3.3)
 tigris     * 0.5.3    2017-05-26 CRAN (R 3.3.3)
...

congressional_districts 114 - url not found

Census seems to have moved the files referenced in the package so that when one tries to use the "congressional_districts" function, the following error message is returned:

co_cd <- congressional_districts("Colorado", cb = TRUE, resolution = '20m')
trying URL 'http://www2.census.gov/geo/tiger/GENZColorado/shp/cb_Colorado_us_cd114_20m.zip'
Error in download.file(url, tiger_file, mode = "wb") :
cannot open URL 'http://www2.census.gov/geo/tiger/GENZColorado/shp/cb_Colorado_us_cd114_20m.zip'
In addition: Warning message:
In download.file(url, tiger_file, mode = "wb") :
cannot open URL 'http://www2.census.gov/geo/tiger/GENZColorado/shp/cb_Colorado_us_cd114_20m.zip': HTTP status was '404 Not Found'

validate_county function

@hrbrmstr I wrote a validate_county function based on your validate_state function, and it is working after a few tests; take a look when you get the chance and let me know if there is anything you think should be cleaned up

Download fails in Win10 R 3.4.1

I just installed R 3.4.1 and tigris 0.5.3 on Windows 10. When I try to run any download functions (states(), counties()) I get an error that the URL cannot be opened. For example

> spdfCounty = counties(state = "NY", refresh = TRUE)
trying URL 'http://www2.census.gov/geo/tiger/TIGER2015/COUNTY/tl_2015_us_county.zip'
Error in download.file(url, tiger_file, mode = "wb") : 
  cannot open URL 'http://www2.census.gov/geo/tiger/TIGER2015/COUNTY/tl_2015_us_county.zip'
In addition: Warning message:
In download.file(url, tiger_file, mode = "wb") :
  InternetOpenUrl failed: 'A redirect request will change a non-secure to a secure connection'

This had been working for me in R 3.3.1 (but a few months ago, just upgraded the version on this computer). It is also working on a Linux computer (R 3.4.1, tigris 0.5.3).

FWIW, I've confirmed that the URL works in a browser. I've tried calling download.file directly, and that fails. However, if I change the URL to ftp://ftp.census.gov/... that works!

options(tigris_use_cache=FALSE) fails on CentOS

This seems to be a very similar issue to #14 and #16, but my solution is opposite!

In those issues people were unable to write to a cache folder, so setting tigris_use_cache=FALSE solved their issues, but for me FALSE is the default setting and isn't working, but setting tigris_use_cache=TRUE does work for me. Moreover, I'm importing tigris with a package I'm distributing, and I'd like to have both options, cache or not, working, or be able to advise users of my package how to configure their systems so that the portions of my package that use tigris will work.

> options(tigris_use_cache=FALSE) # default on 0.5.3
> me <- counties("Maine", cb = TRUE)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  :
  Cannot open data source
In addition: Warning message:
In unzip(tiger_file, exdir = tmp) : error 1 in extracting from zip file
> options(tigris_use_cache=TRUE)
> me <- counties("Maine", cb=TRUE)
Using FIPS code '23' for state 'Maine'

my sessionInfo()

R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS: /home/cmarmstrong/miniconda2/lib/R/lib/libRblas.so
LAPACK: /home/cmarmstrong/miniconda2/lib/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] tigris_0.5.3         rlandscan_0.0.0.9000 RevoUtils_10.0.8
[4] RevoUtilsMath_10.0.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.14     bindr_0.1        magrittr_1.5     maptools_0.9-2
 [5] units_0.4-6      rappdirs_0.3.1   uuid_0.1-2       lattice_0.20-35
 [9] R6_2.2.2         rlang_0.1.6      stringr_1.2.0    udunits2_0.13
[13] httr_1.3.1       dplyr_0.7.4      tools_3.4.3      rgdal_1.2-16
[17] grid_3.4.3       e1071_1.6-8      DBI_0.7          rgeos_0.3-26
[21] class_7.3-14     assertthat_0.2.0 tibble_1.4.1     sf_0.5-5
[25] bindrcpp_0.2     glue_1.2.0       sp_1.2-5         stringi_1.1.6
[29] pillar_1.0.1     compiler_3.4.3   classInt_0.1-24  foreign_0.8-69
[33] pkgconfig_2.0.1

I don't know much about R and temporary directories, but I checked tempdir() and the folder it returns, and it belongs to me so I'd think my R session could write to it also.

`year = ` argument not working for blocks()

It seems that the only year arguments that succeed in the blocks() function are 2014 and 2015.

> wa_blk <- tigris::blocks(state = 53,county = 33,year = 2012) 
Downloading: 140 kB     
error 1 in extracting from zip fileError in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open layer

[ENHANCEMENT] adding `tigris` attribute to Spatial* objects

one more for the potential PR. Adding a tigris attr that will contain the type TIGER object being returned by the various tigris functions. That way, we can make an rbind_tigris function that will let folks retrieve, say, all the area water shapefiles for a state then rbind them together, auto-creating unique IDs per county. It'll also help for (perhaps) plotting and summary functions.

Error in ogrInfo: Cannot open layer due to corrupted cache?

Possibly related to/dupe of #16:

I ran

tigris::tracts(state='CA')

then aborted the download before it could complete. Upon re-executing the command I got

> tigris::tracts(state='CA')
Using FIPS code '06' for state 'CA'
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open layer
In addition: Warning message:
In unzip(file_loc, exdir = cache_dir, overwrite = TRUE) :
  error 1 in extracting from zip file

I'm assuming this is because it's trying to unzip the partially downloaded zip from the first try instead of downloading the zip anew? Forcing a re-download using refresh=TRUE as suggested in #16 fixes things. But perhaps tigris should be smarter about recognizing such corrupted files?

Availability of commuting zones in tigris?

I use tigris with my class. The zctas functions works wonderfully. But now we want to work with commuting zones.

  1. I do not see commuting zones as available in tigris. Am I missing something?

  2. I see commuting zone information in your US Boundaries package. However, I can't even load the data there into R. Any advice?

Thanks!

Block Data query errror

Hey All : big fan of the tigris package but having trouble with block data for certain states:

tigris::blocks( state= '06', county = '099', year = 2015)

immediately results in :
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open layer
In addition: Warning message:
In unzip(file_loc, exdir = cache_dir, overwrite = TRUE) :
error 1 in extracting from zip file

many other state and county blocks queries return data as expected. any ideas ?

i am using R version 3.3.1 (2016-06-21) and tigris_0.3.9.9000

Installing tigris without sf

HI, I work in an environment where we can't get rgdal 2.0+ version on our servers. Is there a way to install tigris without the sf package which only works with rgdal 2.0+?

Problems Working With tmap

I just wanted to start off by saying thank you for creating this package! It's so incredibly useful!
I also raised this issue on the tmap Github page here: https://github.com/mtennekes/tmap/issues/165.

I'll start from the beginning. I want to create my own interactive maps of the United States using data I've obtained from various sources. After searching around, tmap seemed like a good solution for me. After poking around for some tutorials, I found this one for creating interactive choropleths: https://github.com/mtennekes/tmap/blob/master/demo/USChoropleth/US_choropleth.R.

The demo worked! I now wanted to add tigris to my workflow. So I created the following R script which pretty much follows the above demo code with only slight changes adding in tigris:

library(readxl)
library(grid)
library(tmap)
library(tmaptools)
library(tigris)
#options(tigris_class = "sf")

# function to obtain Food Environment Atlas data (2014)
get_food_envir_data <- function() {
  dir <- tempdir()
  download.file("https://www.ers.usda.gov/webdocs/DataFiles/48731/February2014.xls?v=41688", destfile = file.path(dir, "DataDownload.xls"), mode="wb")
  read_excel(file.path(dir, "DataDownload.xls"), sheet = "HEALTH")
}

# obtain Food Environment Atlas data
FEA <- get_food_envir_data()

# obtain US county shape
US_c <- counties()

# create FIPS variable
US_c$FIPS <- paste0(US_c$STATEFP, US_c$COUNTYFP)

# append data to shape
US_c <- append_data(US_c, FEA, key.shp = "FIPS", key.data = "FIPS", ignore.duplicates = TRUE)

# interactive choropleth
ttm()
qtm(US_c, fill = "PCT_OBESE_ADULTS10")

However, this causes my RStudio application to crash. The reason being my RAM is totally overloaded as R quickly uses up all of 8 GB of RAM.

I tried to figure out why it was when I used tigris I was crashing R. I quickly found out that the shape files obtained in the tmap demo were nearly 10x smaller than the shape files obtained through tigris. Yet, when you draw a static map using the two different shape files, the maps of the U.S. counties seem to be the same!

So my question is, am I doing something wrong when I call the U.S. counties? Why are my shape files so big? Any help would be greatly appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.