ruby-robotの夢

cygwin-ruby,qgisの達人を目指す奮闘記

国土調査 水系調査結果の活用方法

国土交通省の実施する国土調査のうち、水系調査結果は、流域水利用や河川に関する情報整理に役に立つ。一方で、問題点もある。

活用にあたっての、工夫や課題等について、整理しておく。

水系によって異なる様々なフォーマット

標準的なフォーマットは定められているものの、現段階では、様々な様式のデータが存在する。

いくつかの流域では、かなり統一されているが、微妙に名前が異なる。

筑後川流域では、かなり細分されている。

統一的な書式で用いたい場合に、テンプレートが使えない。

このため、名前を変えて、統一することが考えられる。

ファイル名テーブルを作成し、一括変換する。

一般化する標準名が、今のところ、与えられていないようなので、個別のデータを判断していく必要がある。

#@@@@@@@@@@@@@

# 水域で異なるファイル名を統一するためのファイル名テープルを用い、ファイル名を一# ここでは、utfにしているが、おそらく、sjisでも問題無い。
# utfにした理由は、変換できないフォルダ名があり、試行錯誤したため。
# 結果的には、explorerで、フォルダ名を変更して対応
#@@@@@@@@@@@@@
require "fileutils"
j_dbg=false
fnames=IO.readlines("flist_form.txt",
rs:"\n",chomp:true,encoding:"Shift_JIS:UTF-8").map{|l| l.split("\t")}
head=fnames.shift
print "head",head if j_dbg
dirs=head[2,5]
print "dirs",dirs if j_dbg
fconv=fnames.map{|id,name,*cons| [id+name,cons]}.to_h
print "fconv",fconv if j_dbg
[*0..4].each{|ir|
fsubs=fconv.map{|idn,fs| [fs[ir],idn]}.to_h
print fsubs if j_dbg
# dir=dirs[ir].encode("UTF-8")+"/shape"
dir=dirs[ir]+"/shape/"
dirq=dirs[ir]+"/shape_q/" #for qgis
FileUtils.makedirs(dirq)
print dir,dir.encoding if j_dbg
fs=Dir.children(dir)
fs.each{|f|
fq=fsubs[f.sub(/\.zip/,"")]
next unless fq
print f,fq+=".zip" if j_dbg
FileUtils.copy(dir+f,dirq+fq)
}
}

 ダウンロードと展開

国土数値情報と異なり、国土調査のファイルには、漢字が多様されている。

zipファイルの展開で、漢字の展開は、apt-cyg :unzip(6.0)では、展開ファイル名が文字化けする。

cygwinでまとめと展開するには、PowerShellを用いる必要がある。

このプログラムは、次の通り。

#!/bin/bash exec ruby -S -x -Ku "$0" "$@" #-Ku:utf8
#!ruby
$,="\t";$\="\n" # George Tai Sep.2019
#@@@@@@@@@@@@@
# unzip script by Power Shell
# because you cannot use Unicodei by using apt-cyg:unzip(6.0)
# zip/*.zip -> extract
require "~/Lib/Ruby/util.rb"
#@@@@@@@@@@@@@@@@
def unzip_ps1(zf) #Use Utf-8
  nfo="temp.ps1" #temporally powershell script
  print "zf",zf,zf.encoding
  zj=File.basename(zf,".zip").encode("Shift_JIS") #Utf->sjis
  print "zj",zj,zj.encoding if $j_dbg #sjis
  cygp=`cygpath -w "#{zf}"`.chomp.encode("Shift_JIS") #sjis
  print "cygp",cygp,cygp.encoding if $j_dbg #sjiS
  ps=open(nfo,"w")
  com="expand-Archive -Force -Path \"#{cygp}\" -DestinationPath \"#{zj}\""
  print com if $j_dbg
  ps.print com; ps.close
  print `~/Bin/nkfs #{nps}`.chomp
  listws=`powershell -f #{nps}`.chomp.split("\n");nlw=listws.size
# Power Shell Setting by Administrator Permission
# Set-ExecutionPolicy RemoteSigned[
  print listws if $j_dbg
end
#@@@@@@@@@@@@@@@@
$j_dbg=false
dirs=Dir.children("zip") #utf
print dirs.map.with_index{|d,i| "%3.3d %s" % [i,d]}*"\n"
printf "Input Number or Numbers,0-9..>"
ans=STDIN.gets.chomp
abort "ans is not to-array! and break" unless ans=~/[0-9]/
print "ans",ans if $j_dbg
ais=str2iarray(ans)
print "ais",ais if $j_dbg
ais.each{|i| unzip_ps1("zip/#{dirs[i]}") }