How can I read ESRI polygon/polyline shapefile in R environment using rgdal or other library? can I get a syntax for it?
-
1This link might help as it mentions how to do it using rgdal: nceas.ucsb.edu/scicomp/usecases/ReadWriteESRIShapeFiles– JosephCommented Oct 13, 2014 at 13:00
-
2In 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.– Jeffrey EvansCommented Oct 13, 2014 at 15:48
3 Answers
Possible duplicate with How to open a Shapefile in R?
There are many packages to read shapefiles:
- rgdal with readOGR (examples) and Read and write ESRI Shapefiles with R, pointed out by Joseph
- maptools with ReadShapePoint, readShapeLines,readShapePoly and readShapeSpatial as Tumbledown show ( and Read and write ESRI Shapefiles with R)
- PBSmapping with importShapefile (examples) and Read and write ESRI Shapefiles with R, pointed out by Joseph
- shapefiles with readshapefile (examples)
- raster with shapefile (examples)
- dismo with shapefile (examples)
- fastshp`with read.shp (examples)
- RSAGA
- ...
-
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?– AsierCommented May 29, 2021 at 19:54
-
from Dismo: 0.7/shapefile: Read a shapefile it is a simple wrapper around readOGR (rgdal package)– geneCommented May 30, 2021 at 8:03
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/
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.
-
1This 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