4

Is there a way to read a shape file with a specific character encoding? I'm trying to read in a Canadian shapefile that has special (French) characters in some of the names. I can convert them manually, but I'd prefer not to do this if there's a setting somewhere that I'm so far blind to.

# manual conversion works
library(maptools)
shp <- file.path("path/to/file.shp")
map <- readShapePoly(shp, proj4string = CRS("+init=epsg:25832"))
map$ERNAME <- iconv(map$ERNAME, "Windows-1252", "UTF-8") 
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255

1 Answers1

3

Instead of using, maptools and the readShapePoly arguments, using the rgdal library with readOGR function allows for more options. For example, the syntax with rgdal could be:

pasl=readOGR(".","filename", use_iconv=TRUE, encoding="UTF-8")

Be aware that this is not a universal solution but depends on the encoding of the file, which in the OP case was UTF-8. Common encoding also includes latin1. In some shapefiles, the encoding is named in the .cpg file (open with a text editor software) along with the .shp file. QGIS automatically generates a .cpg file when a new shapefile is created but many other GIS software do not.

user3386170
  • 276
  • 4
  • 22