I am trying to get home range data from a set of points using the package adehabitathr. Previously I was able to get all of the information in the form of SpatialPolygonsDataFrames. I wanted to export them as shapefiles, and attempted to use code I found on here
dir.create("esrishapes") #create new directory
writeOGR(obj = homerangeAll, dsn = "esrishapes", layer = "homerangeAll", driver = "ESRI
Shapefile")
But I was getting a projection error about not using PROJ6, adehabitathr uses PROJ4 so I attempted to download previous versions of sp and rgdal instead
require(devtools)
install_version("sp",version = "1.4-2")
install_version("rgdal",version = "1.5-8")
This didn't work even though the packages did install. Now I can't use any aspect of adehabitathr as it returns the error
Error in CRS(slot(obj, "projargs")) : NA
I don't have a strong understanding of coordinate systems so I have no idea where to begin fixing this.
Related
I've been trying to get bathymetry lines from marmap and recently got the following error message when getNOAA.bathy function is called
Querying NOAA database ...This may take seconds to minutes, depending on grid size
Error in if (ncol(x) == 3 & !exists("bathy", inherits = FALSE)) { : argument is of length zero
This happens even with something as simple as
map <- getNOAA.bathy(lon1=10,lon2=19,lat1=67,lat2=71,resolution=10, keep=TRUE)
I updated the package to the latest version (1.0.4) as I read that there could be issues related to server access. I've also tried running the above script in R rather than RStudio, but the error persists...
The function works fine if I use previously downloaded data, but now I needed use use another set of coordinates for a new map.
Any help is much appreciated!
You need to (re)install both rgdal and raster packages. This is already documented here on the GitHub Issue pages of the marmap package.
For anyone else looking, the reported error can also result from the server being down - R doesn't give any indication of this. You can check https://www.ncei.noaa.gov/alerts for scheduled outages.
For several months now, the same error message seem to have plagued Windows users, event with all packages up to date.
marmap v1.0.9 is now available on GitHub:
remotes::install_github("ericpante/marmap")
This version should solve this infamous Error message:
Error in if (ncol(x) == 3 & !exists("bathy", inherits = FALSE)) { :
argument is of length zero
I have been able to confirm that the error was due to a limitation in the length of urls that the raster package can handle on Windows. The geotif files from NOAA's servers are now downloaded with utils::download.file() in a temporary file on the user's disk and then imported in R using raster::raster().
marmap v1.0.9 will be available on CRAN servers in the next few days.
I'm very new to gbd table and today at work I was asked to use R to read the data available on https://frap.fire.ca.gov/media/9792/fthrt14_2.zip and plot it using R tools like ggplot and plot_ly.
My problem is that I don't even understand how to read this data with R.
These are the stackoverflow questions I read so far:
- rgdal - read a table within a ESRI geodatabase (gdb)
- How to read a table from an ESRI file geodatabase (.gdb) using R
- How do I get rgdal to open a geodatabase (.gdb) file?
however none of them seems to be working.
These are my steps:
- I unzipped the file in my C:\temp folder. This means I have an R variable file_gdb = "C:/temp/fthrt14_2.gdb"
- then, I have fc_list = ogrListLayers(file_gdb ) and if I print its content I see:
character(0)
attr(,"driver")
[1] "OpenFileGDB"
attr(,"nlayers")
[1] 0
at this point I try fc = readOGR(dsn=fgdb,layer="OpenFileGDB") but I get the following error:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv
Cannot open layer
and from here I'm quite lost.
Any help would be much appreciated. Many thanks from a complete newbie.
I am needed to use sf package command, because readShapePoly commnad will be erased. That's neer future I know... So I want to change my code from route
thata readShapePloy to route sf::st_read. But I cannot write correct code. So I want to correct code and I am very happy, if u show correct sf package command. My now command below, thx...(I am sorry to my poor English skill,Plz over come it...)
In R, I wrote code again and again for ex, on sf::st_read command. But that show error again and again...code below nd error message below too
usa_state <- readShapePoly("usa_state.shp", IDvar = "STATE_CODE")
That is ok, but I know to change that code neer future, cuz this command is erased in neer future. So Plz show me command thata route of sf package.I tried below code but I know this is not understandable in R.
usa_state = sf::st_read("usa_state.shp", layer = "STATE_CODE")
bad code...Plz shw me correct coding!Error occured now am I...
Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
SQL execution failed, cannot open layer.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 1: SQL Expression Parsing Error: syntax error, unexpected
identifier, expecting SELECT or '('. Occurred around : "STATE_CODE"
You're almost there with usa_state = sf::st_read("usa_state.shp", layer = "STATE_CODE").
I'm guessing that STATE_CODE is a field in the usa_state.shp shapefile. You don't need to supply any field names to the st_read() function. Just use:
library(sf)
usa_state = st_read("usa_state.shp")
You'll need to make sure that the usa_state.shp file (and its associated files) are in your current working directory, or you'll need to use the full path:
usa_state = st_read("/path/to/usa_state.shp")
The sf package is well worth getting to know. It's made all of my spatial work in R much easier.
I am trying to use the acs package in R to download Census data for a basic map, but I am unable to download the data and I'm receiving a confusing error message.
My code is as follows:
#Including all packages here in case this is somehow the issue
install.packages(c("choroplethr", "choroplethrMaps", "tidycensus", "tigris", "leaflet", "acs", "sf"))
library(choroplethr)
library(choroplethrMaps)
library(tidycensus)
library(tigris)
library(leaflet)
library(acs)
library(sf)
library(tidyverse)
api.key.install("my_api_key")
SD_geo <- geo.make(state="CA", county = 73, tract = "*", block.group = "*")
median_income <- acs.fetch(endyear = 2015, span = 5, geography = SD_geo, table.number = "B19013", col.names="pretty")
Everything appears to work until the final command, when I receive the following error message:
trying URL 'http://web.mit.edu/eglenn/www/acs/acs-variables/acs_5yr_2015_var.xml.gz'
Content type 'application/xml' length 735879 bytes (718 KB)
downloaded 718 KB
Error in if (url.test["statusMessage"] != "OK") { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In (function (endyear, span = 5, dataset = "acs", keyword, table.name, :
XML variable lookup tables for this request
seem to be missing from ' https://api.census.gov/data/2015/acs5/variables.xml ';
temporarily downloading and using archived copies instead;
since this is *much* slower, recommend running
acs.tables.install()
This is puzzling to me because 1) it appears like something is in fact being downloaded at first? and 2) 'Error in if (url.test["statusMessage"] != "OK") { :
missing value where TRUE/FALSE needed' makes no sense to me. It doesn't align with any of the arguments in the function.
I have tried:
Downloading the tables using acs.tables.install() as recommended in the second half of the error message. Doesn't help.
Changing the endyear and span to be sure that I'm falling within the years of data supported by the API. I seem to be, according to the API documentation. Have also used the package default arguments with no luck.
Using 'variable =' and the code for the variable as found in the official API documentation. This returns only the two lines with the mysterious "Error in if..." message.
Removing colnames = "pretty"
I'm going to just download the datafile as a CSV and read it into R for now, but I'd like to be able to perform this function from the script for future maps. Any information on what's going on here would be appreciated. I am running R version 3.3.2. Also, I'm new to using this package and the API. But I'm following the documentation and can't find evidence that I'm doing anything wrong.
Tutorial I am working off of:
http://zevross.com/blog/2015/10/14/manipulating-and-mapping-us-census-data-in-r-using-the-acs-tigris-and-leaflet-packages-3/#get-the-tabular-data-acs
And documentation of the acs package: http://eglenn.scripts.mit.edu/citystate/wp-content/uploads/2013/02/wpid-working_with_acs_R2.pdf
To follow up on Brandon's comment, version 2.1.1 of the package is now on CRAN, which should resolve this issue.
Your code runs for me. My guess would be that the Census API was temporarily down.
As you loaded tidycensus and you'd like to do some mapping, you might also consider the following code:
library(tidycensus)
census_api_key("your key here") # use `install = TRUE` to install the key
options(tigris_use_cache = TRUE) # optional - to cache the Census shapefile
median_income <- get_acs(geography = "block group",
variables = "B19013_001",
state = "CA", county = "San Diego",
geometry = TRUE)
This will get you the data you need, along with feature geometry for mapping, as a tidy data frame.
I emailed Ezra Haber Glenn, the author of the package, about this as I was having the same issue. I received a response within 30 minutes and it was after midnight, which I thought was amazing. Long story short, the acs package version 2.1.0 is configured to work with the changes the Census Bureau is making to their API later this summer, and it is currently presenting some problems windows users in the mean time. Ezra is going to be releasing an update with a fix, but in the mean time I reverted back to version 2.0 and it works fine. I'm sure there are a few ways to do this, but I installed the devtools package and ran:
require(devtools)
install_version("acs", version = "2.0", repos = "http://cran.us.r-project.org")
Hope this helps anyone else having a similar issue.
I'm trying to replicate the maps shown in this short example (Choroplethr v3.6.0 is now on CRAN) for California, but I'm having trouble with the tract_choropleth function for areas that are not in NY. I get the error message below. I'm able to import the underlying tract data for all areas, but I'm having trouble mapping the non-NY ones. Thanks in advance.
# Load all packages (DONE)
# Install api key (DONE)
# Get Tract-Level Demographics
ca_stats = get_tract_demographics("california")
# Create choropleth map with the function tract_choropleth
ca_stats$value = ca_stats$per_capita_income
tract_choropleth(ca_stats, "california", title = "2013 Per Capita Income\nCensus Tracts", legend="Dollars")
Error message returned:
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 the author of the tigris package that choroplethr uses to get Census tract boundaries. That error shows up in the event of a corrupted download from the Census Bureau website. Ari's solution will work; you can also try:
library(tigris)
options(tigris_refresh = TRUE)
This will direct tigris to re-download the shapefile from the Census Bureau website, and allow you to still use caching. Once it is fixed, set options(tigris_refresh = FALSE) to control future re-downloads.
Thank you for using choroplethr.
I have never gotten this error myself, but it's possible that it relates to a caching issue with the tigris package. I say that because it looks very similar to this issue.
Here is the code which solved that user's problem:
library(tigris)
options(tigris_use_cache = FALSE)
That code causes tigris to not use its cache.
Please type that code into your R session and try the choroplethr code again.