R/city.R

city<-function (name = NULL, state = NULL, statefips = FALSE, sp.object = NULL, proj = NULL) 
{
	require(paste("UScensus","2010cdp",sep=""),character.only=TRUE)
    city.aux <- function(name = NULL, state = NULL, statefips = FALSE, 
        sp.object = NULL, proj = NULL) {
        state <- check.state(state, statefips)
        #require(UScensus2010cdp)
        if (is.null(state)) {
            stop("Not a State! \n")
        }
        if (is.null(sp.object) == FALSE) {
            if (class(sp.object)[1] != "SpatialPolygonsDataFrame") {
                stop("Not a SpatialPolygonsDataFrame object! \n")
            }
            assign(paste(state, ".cdp10", sep = ""), sp.object)
        }
        else {
            data(list = paste(state, ".cdp10", sep = ""), envir = parent.frame())
        }
        temp.cdp <- get(paste(state, ".cdp10", sep = ""))
        
        ####Need to fix this permantly 
        if(statefips){
        temp.cdp$name<-iconv(temp.cdp$name,"WINDOWS-1252","UTF-8")
        temp.cdp$name<-iconv(temp.cdp$place,"WINDOWS-1252","UTF-8")
        temp <- which(tolower(temp.cdp$place) %in%name == TRUE)
        }else{
        	temp.cdp$name<-iconv(temp.cdp$name,"WINDOWS-1252","UTF-8")
        temp <- which(tolower(temp.cdp$name) %in% tolower(name) == TRUE)
        }
        
        if (length(temp) == 0) {
            stop(paste(name, "is not in this SpatialPolygonsDataFrame object \n or this city does not exist in this state!"))
        }
        out <- temp.cdp[temp, ]
        if (is.null(proj) == FALSE) {
              #require(rgdal)
  			requireNamespace("rgdal")
            out <- spTransform(out, proj)
        }
        out
    }
    out <- city.aux(name, state, statefips, sp.object, proj)
}

Try the UScensus2010 package in your browser

Any scripts or data that you put into this service are public.

UScensus2010 documentation built on May 2, 2019, 5:13 p.m.