Skip to content
Prev 28224 / 29559 Next

readOGR workaround for Japanese UTF-8 geojson

I am working on a project https://github.com/AlanInTsukuba/jpucd that
involves

extracting shapefiles and property data from Japanese geojson files. When

reading with readOGR(ibarakipath1 , encoding="UTF-8", use_iconv=TRUE),

I find that the subsets of cannot be written with writeOGR without losing

text fields that are in Japanese text. I found the following workaround but

wonder if there is a better way to do this.


Environment: RGui, Windows10



# load ibaraki shapefiles, extract TX subset, write to geojson

library(jpucd)

shppath <- system.file("extdata",package="jpucd")



ibarakipath1 <-
paste(shppath,"JPGen2005CTgenlCY2000P08Ibaraki.geojson",sep="/")



#^ JPGen2005CTgenlCY2000P08Ibaraki.geojson is a UTF-8 encoded geojson file

#^     having Japanese names in property fields. To be able to

#^    read these fields, they need to be converted (to switch-jis?).

#^     The following command does this.

#^ This can also be done by use_iconv=FALSE and setting

#^     the encoding of the Japanese columns using Encoding(x) <- "UTF-8".



ibaraki <- readOGR(ibarakipath1 , encoding="UTF-8", use_iconv=FALSE) ##
use_iconv=TRUE

## loads so that the Japanese fields are readable but writeOGR doesn?t
write them.

head(ibaraki at data)



#^ Apply Encoding(x) <- ?UTF-8?

for (name in colnames(ibaraki at data[,sapply(ibaraki @data, is.character)])){

  Encoding(ibaraki @data[[name]]) <- "UTF-8"}



#^ Get TX subset

tx2000 <- ibaraki[ibaraki at data$CITY_NAME=="????"|ibaraki at data$CITY_NAME=="
???"

              |ibaraki at data$CITY_NAME=="???"|ibaraki at data$CITY_NAME=="????
",]

head(tx2000 at data)



#^ Write it.

dsn <- "TsukubaExpressCensusDistricts2000.geojson"

writeOGR(tx2000 , dsn,layer="TsukubaExpressCensusDistricts2000" ,
driver="GeoJSON", dataset_options = NULL,

layer_options=NULL, verbose = FALSE, check_exists=NULL,

overwrite_layer=FALSE, delete_dsn=FALSE, morphToESRI=NULL,

encoding="UTF-8")



Thank you.

Alan

https://alanintsukuba.github.io/