[R] readOGR workaround for Japanese UTF-8 geojson

Alan @enge| @end|ng |rom p@terr@@com
Mon Jun 29 04:20:06 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.

# 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$B!G(Bt
write them.
head(ibaraki using data)

#^ Apply Encoding(x) <- $B!H(BUTF-8$B!I(B
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=="$B$D$/$P;T(B
"|ibaraki using data$CITY_NAME=="$B<iC+D.(B"
              |ibaraki using data$CITY_NAME=="$B0KF`D.(B"|ibaraki using data$CITY_NAME=="$BC+(B
$BOB86B<(B",]
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/> https://alanintsukuba.github.io/


	[[alternative HTML version deleted]]



More information about the R-help mailing list