I found two basic ways to open shapefiles in R - using rgdal
and maptools
:
# 1
require(maptools)
shape_maptools <- readShapeLines("file.shp")
# 2
require(rgdal)
shape_rgdal <- readOGR(".", "file")
The data structures seem exactly the same in both cases (class SpatialLinesDataFrame, package sp). However, while rgdal
reads the projection properly, maptools
does not (you possibly have to assign the CRS manually):
> proj4string(shape_maptools)
[1] NA
> proj4string(shape_rgdal)
[1] "+proj=utm +zone=31 +ellps=intl +units=m +no_defs"
So, why would I ever use maptools
to open shape files? I may only make a mistake of assigning
the CRS manually!
rgdal
is always safer way to do open a shapefile?The following comparison of maptools
, rgdal
and PBSMapping
from the National Center for Ecological Analysis and Synthesis might interest you:
"Read and write ESRI Shapefiles with R"
maptools::readShapeLines() get problem with file name containing spacial character eg. German Umlaut.
rgdal can deal with such case
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With