Hi I am trying to read and plot on a custom shape file in R which is not a map.
This is the code I use and the error I get in return:
library(rgdal)
mySHP<- choose.files()
myFile<- readOGR(mySHP)
Error in ogrListLayers(dsn = dsn) : Cannot open data source
If your file is a shapefile, you need to specify the dsn which is the directory where is saved the shapefile and layer which is the name of the shapefile without the extension. You cannot really do it with choose.files. At least not that simply.
myFile <- readOGR(dsn='path.to.folder', layer='nameOfShapefile')
Related
I downloaded Chelsa bio files on my local directory.
And when I tried to create raster for my R project, I keep getting error with following message.
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file.
My code was simple and I was able to create worldclim climate raster file with the same code.
and, cannot figure out the difference.
download.file(url = "https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V2/GLOBAL/climatologies/1981-2010/bio/CHELSA_bio1_1981-2010_V.2.1.tif",
destfile = "Chelsa/bio1.tif")
bio1 <- raster("Chelsa/bio1.tif")
Could anyone advice on this?
I also tried
bio1 <- stack("Chelsa/bio1.tif")
But, similar error message popped out.
I also changed my directory - instead of putting it under subdirectory (named Chelsa), I directly put the file into my home directory. But, all didn't work.
I tried to open an shp file on my Mac, using this code:
library(tidyverse)
library(sf)
library(rgeos)
sf_trees_raw <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-28/sf_trees.csv')
temp_shapefile <- tempfile()
download.file("https://www2.census.gov/geo/tiger/TIGER2017//ROADS/tl_2017_06075_roads.zip", temp_shapefile)
sf_roads <- unzip(temp_shapefile, "tl_2017_06075_roads.shp") %>%
read_sf()
But I receive this error message:
Error: Cannot open "/Users/name/Documents/Playground/Trees_SF/tl_2017_06075_roads.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
I tried other shp files, and I receive the same error message:
map <- read_sf("per_admbnda_adm1_2018.shp")
Error: Cannot open "/Users/name/Documents/Playground/Trees_SF/per_admbnda_adm1_2018.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
I tried copying the shx and dbf files but it doesn't fix the problem.
Any help will be greatly appreciated.
Try unzipping the whole download first, then reading the shapefile file. Although for sf you only need to point at the .shp file, all the other ones (.cpg, .prj, .shx, etc) need to be unzipped and in the same directory.
temp_shapefile <- tempfile()
download.file("https://www2.census.gov/geo/tiger/TIGER2017//ROADS/tl_2017_06075_roads.zip", temp_shapefile)
unzip(temp_shapefile)
sf_roads <- read_sf('tl_2017_06075_roads.shp')
I have a map data whose format is rds. Now I want to use this data in another software which asks for shp format. How to convert rds format data into shp format in R?
If it is spatial object saved as a R-specific binary file of "Serialization Interface for Single Objects" type (see ?readRDS) probably created at some point by saveRDS(), read your file with
library(rgdal)
library(sp)
x <- readRDS("path/to/the/rds_file.rds")
and then write it with:
rgdal::writeOGR(x, "path/to/destination", "filename", driver = "ESRI Shapefile")
Be sure not to put ".shp" at the end of your output filename.
Also be sure not to put a / at the end of the destination folder. Otherwise you might face the error
Creation of output file failed
When the error
Error: inherits(obj, "Spatial") is not TRUE
you might have forgotten the x as the first argument in the writeOGR function.
I'm pretty new to using rgdal so I'm hoping this is something simple that I'm missing, but I've been googling around about it for a few hours and I can't figure out the issue.
Basically I'm trying to make a leaflet map in a shiny app, but I'm getting snarled right at the beginning, trying to load country data like so:
library(rgdal)
countries <- readOGR("https://raw.githubusercontent.com/datasets/geo boundaries-world-110m/master/countries.geojson", "OGRGeoJSON")
but every time I get the following error:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file
I've gone to the address and I see the raw geojson file there, so it's not a missing file. I've also downloaded the file manually into a data folder and then tried to access it with
countries <- readOGR("data/countries.geojson", "OGRGeoJSON")
and I get the same error. Any ideas would be much appreciated.
I'm running R on Windows 7.
I have a script to create a randomly distributed square polygons in KML format which takes in shapefile with a single polygon as an input which works absolutely well. The problem arise when I tried to create the user defined function out of it. I used readShapePoly() function to read the shapefile and it works well when used out of the function. But when the function is created in which shapefile should be given as an input, it simply wont take. It shows this error message
Error in getinfo.shape(filen) : Error opening SHP file
I avoid writing extensions and I do have all the extension files to create the shapefile.
Part of the script to read the shapefile using it as the input file:
library(maptools)
library(sp)
library(ggplot2)
Polytokml <- function(shapefile_name){
###Input Files
file1 <- "shapefile_name"
Shape_file <- readShapePoly(file1) #requires maptools
return(Shape_file)
}
The function is created but it doesn't work if the function is called.
>Polytokml(HKH.shp)
Error in getinfo.shape(filen) : Error opening SHP file
This works well out of the function.
###Input Files
file1 <- "shapefile.shp"
Shape_file <- readShapePoly(file1) #requires maptools
This is just an example out of the whole script in which different arguments are taken as an input. So just to make things simple I have added script to read the shapefile which has been a problem now. Do let me know if it is not clear.
Thank you so much in advance :)