14

I have read a shapefile using readShapePoly in the maptools package, but cannot read that same file with readOGR. I am hoping someone may be able to help me read the shapefile with readOGR.

I downloaded the file orcounty.shp from here: http://geography.uoregon.edu/geogr/topics/maps.htm

I also downloaded the associated files: orcounty.shx, orcounty.sbx, orcounty.sbn, and orcounty.dbf and put all five files in the folder: c:/users/mark w miller/gis_in_R/shapefile_example/

The following code reads the shapefile and displays some attributes:

library(maptools)

setwd('c:/users/mark w miller/gis_in_R/shapefile_example/')

# Oregon county census data (polygons)
orcounty.poly <- readShapePoly('orcounty.shp', proj4string=CRS("+proj=longlat"))
orcounty.line <- readShapeLines('orcounty.shp', proj4string=CRS("+proj=longlat"))

# see projection
summary(orcounty.poly)

Object of class SpatialPolygonsDataFrame
Coordinates:
         min        max
x -124.55840 -116.46944
y   41.98779   46.23626
Is projected: FALSE 
proj4string : [+proj=longlat]
Data attributes:

However, when I try to read that same shapefile using the following code I receive an error:

library(rgdal)

# read shapefile
oregon.map <- readOGR(dsn="c:/users/mark w miller/gis_in_R/shapefile_example/", layer="orcounty")

# convert to dataframe
oregon.map_df <- fortify(oregon.map)

The error message says:

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) : 
  Cannot open file

I can read Natural Earth http://www.naturalearthdata.com/ shapefiles using:

library(rgdal)

setwd("c:/users/mark w miller/gis_in_R/")

# read shapefile
wmap <- readOGR(dsn="ne_110m_physical", layer="ne_110m_land")

So, apparently there is a difference between the Natural Earth shapefiles and the Oregon shapefile orcounty.shp.

Thank you for any advice on how to read orcounty.shp with readOGR. My question is similar to the question here: rgdal / readOGR - unable to read shapefile from .zip

Community
  • 1
  • 1
Mark Miller
  • 12,483
  • 23
  • 78
  • 132
  • 1
    I can open it normally. Try `readOGR(dsn = 'c:/users/mark w miller/gis_in_R/shapefile_example', layer = 'orcounty')` – Paulo E. Cardoso Mar 10 '14 at 00:12
  • @PauloCardoso That worked! I cannot believe the solution was so simple. If you want to post that as an answer I can accept it, or I could just delete the question. – Mark Miller Mar 10 '14 at 00:16

4 Answers4

18

Try to remove your last '/' from file path.

readOGR(dsn = 'c:/users/mark w miller/gis_in_R/shapefile_example',
        layer = 'orcounty')
Paulo E. Cardoso
  • 5,778
  • 32
  • 42
3

For anyone ending up here with this error on a Linux box, I found the problem was using a home path shortcut. i.e.

# Works
readOGR(dsn="/home/user/dir", layer="file")

# Doesn't work
readOGR(dsn="~/dir", layer="file")

I have no idea why.

MikeRSpencer
  • 1,276
  • 10
  • 24
  • 1
    The tilde expression (`~`) is usually expanded by the Unix shell. If you're passing it into an R library call, it will probably be read literally. – Josip Rodin Dec 02 '15 at 15:15
  • 1
    OK, thanks. Often it works in R, for reading files, setting working directories. In this instance it didn't. – MikeRSpencer Dec 02 '15 at 15:27
1

I used the file ne_110m_land

Try with this:

setwd('D:/JMSR/codes.R/mapas')
unzip("ne_110m_land.zip")
ogrInfo(".", "ne_110m_land")
wmap <- readOGR(".", "ne_110m_land")
EdJo1924
  • 21
  • 1
0

raster::shapefile wraps around readOGR to take care of paths and tildes; just pass the full file name.

 library(raster)
 x <- shapefile("c:/users/orcounty.shp')

or

 y <- shapefile("~/users/orcounty.shp")
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63