2

How can I read ESRI polygon/polyline shapefile in R environment using rgdal or other library? can I get a syntax for it?

2
  • 1
    This link might help as it mentions how to do it using rgdal: nceas.ucsb.edu/scicomp/usecases/ReadWriteESRIShapeFiles
    – Joseph
    Commented Oct 13, 2014 at 13:00
  • 2
    In the future, please make some effort to search the site. This is addressed in many posts. That said, look at "readOGR" for vector and "readGDAL" for rasters using the rgdal package. The help documents are very clear with specific examples for shapefiles. The preferred alternative for rasters are "raster", "stack" or "brick" in the raster packages. There are also options using the maptools package but they do not retain projection information. One advantage of rgdal is that you have access to several additional functions associated with GDAL. Commented Oct 13, 2014 at 15:48

3 Answers 3

6

Possible duplicate with How to open a Shapefile in R?

There are many packages to read shapefiles:

2
  • I cannot find any "shapefile" command in dismo package, not even the words "shapefile" or "shp" in the description pdf of the package. A look at the functions does not help, either. Were there any changes in the last versions (I'm reading the help for version 1.3-3, published in nov 17th 2020)? Or is there any obscure way to do it?
    – Asier
    Commented May 29, 2021 at 19:54
  • from Dismo: 0.7/shapefile: Read a shapefile it is a simple wrapper around readOGR (rgdal package)
    – gene
    Commented May 30, 2021 at 8:03
3

readShapeSpatial in the maptools package is my go-to function:

library("maptools")
readShapeSpatial("c:\\Temp\\My_Shapefile.shp")

Nice tutorial here: http://www.kevjohnson.org/making-maps-in-r/

0

As previously stated, a really useful function is readOGR from the package rgdal, but also fortify from ggplot2, which transforms the object SpatialPolygonsDataFrame (difficult to work with!) into an R-friendly one.

A good tutorial can be found here.

1
  • 1
    This is not correct! The readOGR is what creates the sp SpatialPolygonsDataFrame object, not fortify. The fortify function, which is now depreciated in leu of broom and tidy, is just a method for converting an sp object to something that ggplot2 can handle. I have no idea what R friendly is supposed to mean. An S4 class is quite R like and objects resulting from fortify are not suitable for subsequent spatial analysis using the array of supporting spatial R packages. Commented Dec 30, 2017 at 2:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.