LoginSignup
0
1

More than 3 years have passed since last update.

jpndistrictをCentOS 7 にインストールする

Last updated at Posted at 2020-11-18

jpndistrictは日本の行政地区をプロットするのに便利なRのパッケージである。
便利だよと勧められたので使ってみようと思ったら、インストールにかなり手こずった。
最終的にインストールは成功し、便利なツールだと実感したので、自分が人に勧める側になったときにインストールの手助けができるよう記事にしてまとめておく。

jpndistrictの使用方法自体は説明しない。公式ページで詳しく説明されているのでそちらを参照してほしい。

環境

CentOS 7
R version 3.6.0

インストール

まずは単純にjpndistrictをRにインストールできないか試してみる。
ubuntu等, CentOS 7以外であれば多分これだけで上手くいく(後述)。

install.packages("jpndistrict")

もしインストール中に以下のようなエラーが出た場合は、要求されたパッケージをインストールして再挑戦。

configure: error: 
--------------------------------------------------------------------------------
  Configuration failed because libudunits2.so was not found. Try installing:
    * deb: libudunits2-dev (Debian, Ubuntu, ...)
    * rpm: udunits2-devel (Fedora, EPEL, ...)
    * brew: udunits (OSX)
  If udunits2 is already installed in a non-standard location, use:
    --configure-args='--with-udunits2-lib=/usr/local/lib'
  if the library was not found, and/or:
    --configure-args='--with-udunits2-include=/usr/include/udunits2'
  if the header was not found, replacing paths with appropriate values.
  You can alternatively set UDUNITS2_INCLUDE and UDUNITS2_LIBS manually.
--------------------------------------------------------------------------------

筆者のOSはCentOS 7なのでudunits2-develをインストールして再挑戦。でも失敗。

CentOS 7だと上手くいかない理由

install.packages("jpndistrict")

中略
* installing *source* package sf ...
**  パッケージ sf の解凍および MD5 サムの検証に成功しました 
** using staged installation
configure: CC: gcc -m64 -std=gnu99
configure: CXX: g++ -m64 -std=gnu++11
checking for gdal-config... no
configure: error: gdal-config not found or not executable.
ERROR: configuration failed for package sf

checking for gdal-config... no

と言われたのでココを参考にGDALをインストールしたが、

configure: GDAL: 1.11.4
checking GDAL version >= 2.0.1... no
configure: error: sf is not compatible with GDAL versions below 2.0.1
ERROR: configuration failed for package ‘sf’

GDALのバージョンが古いと言われてしまった。

yum info gdal

名前                : gdal
アーキテクチャー    : x86_64
バージョン          : 1.11.4
リリース            : 3.el7
容量                : 276 k
リポジトリー        : epel/x86_64
要約                : GIS file format library
URL                 : http://www.gdal.org
ライセンス          : MIT
説明                : Geospatial Data Abstraction Library (GDAL/OGR) is a cross
                    : platform C++ translator library for raster and vector
                    : geospatial data formats. As a library, it presents a single
                    : abstract data model to the calling application for all
                    : supported formats. It also comes with a variety of useful
                    : commandline utilities for data translation and processing.
                    : 
                    : It provides the primary data access engine for many
                    : applications. GDAL/OGR is the most widely used geospatial data
                    : access library.

yum(EPELリポジトリ使用)でインストール出来る最新バージョンは1.11.4

apt info gdal-bin

Package: gdal-bin
Version: 3.0.4+dfsg-1build3
Priority: optional
Section: universe/science
Source: gdal
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian GIS Project <pkg-grass-devel@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 1369 kB
Depends: python3-gdal (= 3.0.4+dfsg-1build3), python3-numpy (>= 1:1.16.0~rc1), python3-numpy-abi9, python3:any, libc6 (>= 2.15), libgcc-s1 (>= 3.0), libgdal26 (>= 3.0.0), libstdc++6 (>= 5.2)
Suggests: libgdal-grass
Breaks: gdal-bin (<< 1.10.0-0~), python-gdal (<< 2.4.3~)
Replaces: python-gdal (<< 2.4.3~)
Homepage: http://www.gdal.org/
Download-Size: 256 kB
APT-Sources: http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages
Description: Geospatial Data Abstraction Library - Utility programs

apt(ubuntu)でインストール出来るGDALは3.0.4だった。

というわけで、レポジトリに登録されているパッケージのバージョンが古いCentOS 7の場合、
jpndistrict(で必要なsfパッケージ)をインストールしたければ、
yumを使わずソースからビルドしてGDALをインストールする必要がある。
更に言えばGDALを動かすために必要なPROJもyumからインストールするとバージョンが古い。

yum info proj

名前                : proj
アーキテクチャー    : x86_64
バージョン          : 4.8.0
リリース            : 4.el7
容量                : 181 k
リポジトリー        : epel/x86_64
要約                : Cartographic projection software (PROJ.4)
URL                 : http://proj.osgeo.org
ライセンス          : MIT
説明                : Proj and invproj perform respective forward and inverse
                    : transformation of cartographic data to or from cartesian data
                    : with a wide range of selectable projection functions.

yumでインストールできるのは4.8.0 今必要なのは6.0.0以上

要は,"jpndistrict"をインストールするための"sf"をインストールするための"GDAL"をインストールするための"PROJ"のインストールが必要。気が滅入る。

どのバージョンを入れるべきか

最低でも必要なバージョン
GDAL >= 2.0.1
PROJ >= 6.0.0
GEOS >= 3.4.0

yumでインストール出来る最新バージョン
GDAL : 1.11.4
PROJ : 4.8.0
GEOS : 3.4.2

色々試してみたところ、CentOS 7にsfをインストールする場合

を先にインストールしておくと上手くいった。

GDALは記事作成時時点(2020/11/18)での最新バージョンを入れた。

PROJは最新版の7.2.0を入れようとすると、追加でSQLite3のバージョンアップが必要になり、yumのシステムの一部を弄ることになるので、リスクが高いと考え諦めた。
かといって、使用できる中で一番古くSQLiteのバージョンアップが不要な6.0.0をインストールした場合でも、GDALとPROJのバージョンが離れすぎてしまうためか、その後のツールのインストールが失敗した。様々なバージョンで試した結果、6.1.1でインストールが成功した。
参考
[SQLite3のアップデート方法]https://qiita.com/kai_kou/items/c18b68a7916251231f6d
リンク先はAmazon Linux 2での話だが、OSベースはRHEL 7/CentOS 7っぽいという話を聞いたので参考にした。

GEOSはyumでインストールできるバージョンでも大丈夫そうだが、PROJのようにGDALとバージョンが離れすぎてエラーが出ると困ると考え最新版を入れた。

インストール(CentOS 7 の場合)

PROJ→GEOS→GDALの順でインストール

いずれもインストール方法は、ダウンロードしたファイルを解凍して、

./configure
make
make install

上の3つのコマンドが通ればインストール成功

インストールできたらRを起動して

install.packages("jpndistrict")

と打ってjpndistrictインストール(sfも一緒にインストールされる)。

パッケージを読み込んで、

library("jpndistrict")

This package provide map data is based on the Digital Map 25000 (Map
Image) published by Geospatial Information Authority of Japan (Approval
No.603FY2017 information usage <https://www.gsi.go.jp>)

と出ればインストール成功。

出現したエラー(出なければ無視して良い)

sqlite3が見つからない

PROJをconfigureしたときに発生

checking for SQLITE3... configure: error: Package requirements (sqlite3 >= 3.7) were not met:

No package 'sqlite3' found

確認したけどちゃんとある

sqlite3 --version
3.7.17

解決法:sqlite-develをインストール

yum install sqlite-devel

pcs.csvが読み込めない

sfインストール時に発生

checking GDAL: /usr/local/share/gdal/pcs.csv readable... no

パスの位置を確認したが、pcs.csvは存在しなかった。

解決法:ネット上から探してきて"/usr/local/share/gdal/"下に設置
https://trac.osgeo.org/gdal/browser/trunk/gdal/data/pcs.csv

PROJが利用不可

sfインストール時に発生

checking GDAL: checking whether PROJ is available fur running:... ./gdal_proj: error while loading shared libraries: libgdal.so.28: cannot open shared object file: No such file or directory
no
configure: error: OGRCoordinateTransformation() does not return a coord.trans: PROJ not available?
ERROR: configuration failed for package sf

解決法:Rを終了させてから、/etc/ld.so.conf.d/libgdal-x86_64.confに

/usr/local/lib

と追記(libgdal-x86_64.confがなければ新規作成して上記の内容を書き込む)。

その後、

ldconfig

を実行してからRを起動してインストール再挑戦。

参考 https://stackoverflow.com/questions/46181614/install-r-sf-package-on-centos-gdal-shared-libary-error

インストール後

jpn_pref(14)

と打った際に

ERROR while rich displaying an object: Error in loadNamespace(name): there is no package called geojsonio

とエラーが出た場合は、geojsonioをインストール。

install.packages("geojsonio")

特に何もエラーが出なければ、jpndistrict 使用環境の完成。

動作テストとして、北海道の役所の位置を表示させてみる。

pref01 = jpn_pref(admin_name = "北海道") # 北海道の地形データ
df_office01 = jpn_admins(1) # 北海道の役所の位置

# キャンバスの作成
ggplot() +
# 地形を描画
geom_sf(data = pref01, aes(fill = city)) +
# 点をプロット
geom_sf(data = df_office01, color = "white", size=0.8) +
theme(legend.position = 'none')

001.png

座標を可視化すると、役所の位置から山地を何となく予測できて面白い結果になった。

おわりに

yumでインストールできるパッケージが古いせいでめちゃくちゃ苦労させられた。
CentOS 8もリリースされて1年経ったし、そろそろバージョンアップするべきなのかも。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1