2020年4月24日金曜日

参考
basemap は終了なのか。。。

How To Install "python-mpltoolkits.basemap" Package on Ubuntu

Quick Install Instructions of python-mpltoolkits.basemap on Ubuntu Server. It’s Super Easy! simply click on Copy button to copy the command and paste into your command line terminal using built-in APT package manager.

See below for quick step by step instructions of SSH commands, Copy/Paste to avoid miss-spelling or accidently installing a different package.


Quick Install Steps:
Step 1
sudo apt-get update -y
Step 2
sudo apt-get install -y python-mpltoolkits.basemap
Step 3
Check the system logs to confirm that there are no related errors. You can use ZoomAdmin to check the logs, server management, website hosting and more.
See ZA Features Page for demo videos and 30 Days Free trial info.
Execute the commands above step by step. You can simply hit the copy button to copy the command and paste into the command line interface.
Note: -y flag means to assume yes and silently install, without asking you questions in most cases.

matplotlib toolkit to plot on map projections
The matplotlib basemap toolkit is a library for plotting 2D data on maps in Python. It is similar in functionality to the matlab mapping toolbox, the IDL mapping facilities, GrADS, or the Generic Mapping Tools. PyNGL and CDAT are other libraries that provide similar capabilities in Python. matplotlib toolkit to plot on map projections
The matplotlib basemap toolkit is a library for plotting 2D data on maps in Python. It is similar in functionality to the matlab mapping toolbox, the IDL mapping facilities, GrADS, or the Generic Mapping Tools. PyNGL and CDAT are other libraries that provide similar capabilities in Python.

Detailed Instructions:
Step 1
Run update command to update package repositories and get latest package information.
sudo apt-get update -y
Step 2
Run the install command with -y flag to quickly install the packages and dependencies.
sudo apt-get install -y python-mpltoolkits.basemap
Step 3
Check the system logs to confirm that there are no related errors. You can use ZoomAdmin to check the logs, server management, website hosting and more.
See ZA Features Page for demo videos and 30 Days Free trial info.


もそも、なぜBasemap?

まずはこういうのを日本地図で実現したい….(参考URL:https://qiita.com/sakaia/items/36c92d14942c3458bb56)
Basemapをインストールしようとした目的は、Pythonで分析したデータを地図上に描画したいと考えたためです。
もちろん、基礎をやるだけで目一杯なので、授業ではやっていません。
しかし、総務省統計局などから出ているオープンデータなどでは、都道府県ごとに集計されたデータなども数多くありますので、これをなんとか地図上に表現して、いろいろな角度から視覚化してみたいと考えました。
また、一度ビジュアライズの方法が確立できれば、いろいろ使い回しができそうだという単純な理由もありますw
というわけで、調べてみたところ、2018年8月現在では、OpenStreetMap, Google Colabなどを使う方法などもありましたが、中でもPythonライブラリのmatplotlib「Basemap」を使うのが一般的のようでしたのでBasemapを使える環境を構築することにしました。
以下は、途中紆余曲折もあったなかで調べながらエラーを対処し、今回最終的に成功した方法のご紹介です。

環境情報

マシン:MacBook Pro (13-inch 2016, Four Thunderbolt 3 ports)
OS: macOS Sierra (バージョン10.12.6)
Python 3.6.0 (Jupyter Notebook)
【参考: Pythonのバージョン確認方法】#下記をターミナルで実行するとバージョンがわかります
$ python3 --version
Python 3.6.0
Basemap動作に必要なライブラリ:
matplotlib, numpy, geos, proj, 必須ではないがオプションとしてpillow
※Pythonの基礎習得が終わった頃には、matplotlib, numpyは普通にインストールされていることと思いますので、足らないライブラリのみを追加でインストールしてください。
私の場合は geos, projとオプションのpillowをpipでインストールしました。なお、Basemapはpipではインストールできません。
【参考:pipでのインストール方法】
#下記をターミナルで実行することによりライブラリが個別にインストールできます
$ pip install geos
$ pip install proj
$ pip install pillow
ライブラリのインストールは他にもいろいろな方法があります。HomebrewやAnacondaを利用してPython環境を構築している場合などそれぞれに適した方法でインストールしてください。

Basemapインストール

さて、いよいよBasemapをインストールします。
途中でエラーになって苦労したので、もし実際にインストールされる際は、このエントリの最後まで読んでからの方が無難かもしれません。
【手順1】basemap-1.0.7.tar.gzをダウンロード
※検索や公式サイトからダウンロードサイトが見つかります。
https://sourceforge.net/projects/matplotlib/files/matplotlib-toolkits/
オリジナルソースは下記にあります。
【手順2】basemap-1.0.7.tar.gzの解凍
手順1でダウンロードしたファイルを解凍すると,「basemap_1.0.7」というディレクトリが作成されますので、パスの通った場所に解凍してください。
【手順3】.bash_profile に以下を設定
ターミナルで以下を実行します。(pathは環境によって異なる可能性あり)
export GEOS_DIR=/usr/local/Cellar/geos/3.6.2/ 
source ~/.bash_profile
【手順4】Basemapのインストール
cdコマンドで「basemap_1.0.7」ディレクトリまで戻り、下記セットアップコマンドを実行します。
python3 setup.py install
【手順5】Basemapのテストランを実行
Jupyter Notebookを起動し下記を実行することにより、インストール状況を確認します。
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

map = Basemap()
map.drawcoastlines()

fnameF='fig_basemap.png'
plt.savefig(fnameF, bbox_inches="tight", pad_inches=0.2)
plt.show()
これで下の地図が出れば成功です。
別の地図も試してみました。(参考サイト4)

【オマケ情報】エラーが起きたら…

上記の【手順4】でエラーが発生し、時間の都合上ここで一旦日をおくことになりました。
エラー内容としては、
「fatal error: ‘geos_c.h’ file not found」というもので、
stackoverflowやteratailなども調べましたが、容易に解決することなく、何時間もかけていろいろ試したところ、結局、最終的にmatplotlib Basemapの本家英語の公式サイトのインストール方法(https://matplotlib.org/basemap/users/installing.html#installation)でエラー回避し、Basemapが正常にインストールできました。
日本語のページは本当にありがたいですが、やはり基本に立ち戻るのは重要ですね…。
つまづいたら(日本語でなくても)公式サイトに戻って確認してみることをお勧めします。

■参考サイト

  1. まず最初に参考にしたサイト
    Python matplotlib Basemapのインストール
    https://qiita.com/damyarou/items/14a2ce435b308c38ac67
    Basemapインストールに関するサイトはいくつかありましたが、更新日付の比較的新しい(2017年09月29日)こちらのサイトを参考にさせていただき、途中まではうまくいきました。
    全く同じ方法は使わなかったので、実際に発生したエラーは異なります。
  2. エラーが発生して参照したサイト(英語)
    Installing basemap on Mac / Python
    https://stackoverflow.com/questions/42299352/installing-basemap-on-mac-python
    エラー内容は異なりましたが手順的な参考になるかと思い、参照しました。
  3. Basemap公式サイト(英語)
    https://matplotlib.org/basemap/users/index.html
    「Installation」を参考にしました。この方法でエラーなくセットアップできました。
  4. テストランのために参照したサイト
    matplotlib.basemapで簡単にマップを描画する
    https://qiita.com/msrks/items/ed18a2653bc177a24cca
    参考サイト1でも確認しましたが、別の方法でもできるか確認するために参照しました。