[R-sig-Geo] readOGR workaround for Japanese UTF-8 geojson

Alan Engel ttcro@@ro@d@09 @end|ng |rom gm@||@com
Mon Jun 29 04:55:55 CEST 2020


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 using data)



#^ Apply Encoding(x) <- “UTF-8”

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

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



#^ Get TX subset

tx2000 <- ibaraki[ibaraki using data$CITY_NAME=="つくば市"|ibaraki using data$CITY_NAME=="
守谷町"

              |ibaraki using data$CITY_NAME=="伊奈町"|ibaraki using data$CITY_NAME=="谷和原村
",]

head(tx2000 using 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/

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list