LoginSignup
0
0

More than 1 year has passed since last update.

flaskでfoliumを使いマップ表示

Last updated at Posted at 2023-03-24

※anacondaでflask環境を既に構築していることが前提となります

1.foliumを組み込む

(flask-test) [xxx flask-test]$ conda install folium
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##
  environment location: /home/xxx/anaconda3/envs/flask-test
  added / updated specs:
    - folium

The following packages will be downloaded:
    package                    |            build
    ---------------------------|-----------------
    asn1crypto-0.24.0          |           py37_0         156 KB
    blas-1.0                   |              mkl           6 KB
    cffi-1.12.3                |   py37h2e261b9_0         222 KB
    cryptography-2.7           |   py37h1ba5d50_0         544 KB
    idna-2.8                   |           py37_0          85 KB
    intel-openmp-2019.4        |              243         729 KB
    libgfortran-ng-7.3.0       |       hdf63c60_0        1006 KB
    mkl-2019.4                 |              243       131.2 MB
    mkl_fft-1.0.12             |   py37ha843d7b_0         154 KB
    mkl_random-1.0.2           |   py37hd81dba3_0         361 KB
    numpy-1.16.4               |   py37h7e9f1db_0          48 KB
    numpy-base-1.16.4          |   py37hde5b4d6_0         3.5 MB
    pandas-0.24.2              |   py37he6710b0_0         8.6 MB
    pycparser-2.19             |           py37_0         171 KB
    python-dateutil-2.8.0      |           py37_0         266 KB
    ------------------------------------------------------------
                                           Total:       146.9 MB

The following NEW packages will be INSTALLED:

  altair             conda-forge/linux-64::altair-3.1.0-py37_0
  asn1crypto         pkgs/main/linux-64::asn1crypto-0.24.0-py37_0
  attrs              pkgs/main/linux-64::attrs-19.1.0-py37_1
  blas               pkgs/main/linux-64::blas-1.0-mkl
  branca             conda-forge/noarch::branca-0.3.1-py_0
  cffi               pkgs/main/linux-64::cffi-1.12.3-py37h2e261b9_0
  chardet            pkgs/main/linux-64::chardet-3.0.4-py37_1
  cryptography       pkgs/main/linux-64::cryptography-2.7-py37h1ba5d50_0
  entrypoints        pkgs/main/linux-64::entrypoints-0.3-py37_0
  folium             conda-forge/noarch::folium-0.9.1-py_0
  idna               pkgs/main/linux-64::idna-2.8-py37_0
  intel-openmp       pkgs/main/linux-64::intel-openmp-2019.4-243
  jsonschema         pkgs/main/linux-64::jsonschema-3.0.1-py37_0
  libgfortran-ng     pkgs/main/linux-64::libgfortran-ng-7.3.0-hdf63c60_0
  mkl                pkgs/main/linux-64::mkl-2019.4-243
  mkl_fft            pkgs/main/linux-64::mkl_fft-1.0.12-py37ha843d7b_0
  mkl_random         pkgs/main/linux-64::mkl_random-1.0.2-py37hd81dba3_0
  numpy              pkgs/main/linux-64::numpy-1.16.4-py37h7e9f1db_0
  numpy-base         pkgs/main/linux-64::numpy-base-1.16.4-py37hde5b4d6_0
  pandas             pkgs/main/linux-64::pandas-0.24.2-py37he6710b0_0
  pycparser          pkgs/main/linux-64::pycparser-2.19-py37_0
  pyopenssl          pkgs/main/linux-64::pyopenssl-19.0.0-py37_0
  pyrsistent         pkgs/main/linux-64::pyrsistent-0.14.11-py37h7b6447c_0
  pysocks            pkgs/main/linux-64::pysocks-1.7.0-py37_0
  python-dateutil    pkgs/main/linux-64::python-dateutil-2.8.0-py37_0
  pytz               pkgs/main/noarch::pytz-2019.1-py_0
  requests           pkgs/main/linux-64::requests-2.22.0-py37_0
  six                pkgs/main/linux-64::six-1.12.0-py37_0
  toolz              pkgs/main/noarch::toolz-0.10.0-py_0
  urllib3            pkgs/main/linux-64::urllib3-1.24.2-py37_0
  vincent            conda-forge/noarch::vincent-0.4.4-py_1

Proceed ([y]/n)? y

Downloading and Extracting Packages
libgfortran-ng-7.3.0 | 1006 KB   | ################################################################################### | 100%
mkl-2019.4           | 131.2 MB  | ################################################################################### | 100%
cffi-1.12.3          | 222 KB    | ################################################################################### | 100%
mkl_fft-1.0.12       | 154 KB    | ################################################################################### | 100%
mkl_random-1.0.2     | 361 KB    | ################################################################################### | 100%
pycparser-2.19       | 171 KB    | ################################################################################### | 100%
python-dateutil-2.8. | 266 KB    | ################################################################################### | 100%
intel-openmp-2019.4  | 729 KB    | ################################################################################### | 100%
pandas-0.24.2        | 8.6 MB    | ################################################################################### | 100%
asn1crypto-0.24.0    | 156 KB    | ################################################################################### | 100%
idna-2.8             | 85 KB     | ################################################################################### | 100%
blas-1.0             | 6 KB      | ################################################################################### | 100%
numpy-base-1.16.4    | 3.5 MB    | ################################################################################### | 100%
cryptography-2.7     | 544 KB    | ################################################################################### | 100%
numpy-1.16.4         | 48 KB     | ################################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(flask-test) [xxx flask-test]$

2.組み込んだモジュールを確認する

(flask-test) [xxx flask-test]$ conda list
# packages in environment at /home/xxx/anaconda3/envs/flask-test:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main
altair                    3.1.0                    py37_0    conda-forge
asn1crypto                0.24.0                   py37_0
attrs                     19.1.0                   py37_1
blas                      1.0                         mkl
branca                    0.3.1                      py_0    conda-forge
ca-certificates           2019.5.15                     0
certifi                   2019.6.16                py37_0
cffi                      1.12.3           py37h2e261b9_0
chardet                   3.0.4                    py37_1
click                     7.0                      py37_0
cryptography              2.7              py37h1ba5d50_0
entrypoints               0.3                      py37_0
flask                     1.1.1                      py_0
folium                    0.9.1                      py_0    conda-forge
idna                      2.8                      py37_0
intel-openmp              2019.4                      243
itsdangerous              1.1.0                    py37_0
jinja2                    2.10.1                   py37_0
jsonschema                3.0.1                    py37_0
libedit                   3.1.20181209         hc058e9b_0
libffi                    3.2.1                hd88cf55_4
libgcc-ng                 9.1.0                hdf63c60_0
libgfortran-ng            7.3.0                hdf63c60_0
libstdcxx-ng              9.1.0                hdf63c60_0
markupsafe                1.1.1            py37h7b6447c_0
mkl                       2019.4                      243
mkl_fft                   1.0.12           py37ha843d7b_0
mkl_random                1.0.2            py37hd81dba3_0
ncurses                   6.1                  he6710b0_1
numpy                     1.16.4           py37h7e9f1db_0
numpy-base                1.16.4           py37hde5b4d6_0
openssl                   1.1.1c               h7b6447c_1
pandas                    0.24.2           py37he6710b0_0
pip                       19.1.1                   py37_0
pycparser                 2.19                     py37_0
pyopenssl                 19.0.0                   py37_0
pyrsistent                0.14.11          py37h7b6447c_0
pysocks                   1.7.0                    py37_0
python                    3.7.3                h0371630_0
python-dateutil           2.8.0                    py37_0
pytz                      2019.1                     py_0
readline                  7.0                  h7b6447c_5
requests                  2.22.0                   py37_0
setuptools                41.0.1                   py37_0
six                       1.12.0                   py37_0
sqlite                    3.29.0               h7b6447c_0
tk                        8.6.8                hbc83047_0
toolz                     0.10.0                     py_0
urllib3                   1.24.2                   py37_0
vincent                   0.4.4                      py_1    conda-forge
werkzeug                  0.15.4                     py_0
wheel                     0.33.4                   py37_0
xz                        5.2.4                h14c3975_4
zlib                      1.2.11               h7b6447c_3

3.以下folium_map.py、folium.htmlをflaskフォルダに格納する

3-1.フォルダ階層

(flask-web) [xxx Flask]$ tree
.
├── flask-test
│   ├── folium_map.py
│   └── templates
│       ├── folium_map.html

3-2.pythonファイル

##############################################
# foliumを使って地図上にプロット表示する
##############################################
import folium

from flask import Flask,render_template,request

#インスタンス生成
app = Flask(__name__)

@app.route('/foliummap')
def foliummap():
  #地図の中心の緯度、経度
  start_cords=(35.681236,139.767125)
  folium_map = folium.Map(location=start_cords, zoom_start=14.75)

  #地図上にプロットする ***************
  folium.Marker([35.681236,139.767125], popup='東京駅').add_to(folium_map)
  
  #保存
  folium_map.save('templates/map.html')
  return render_template('folium_map.html')

if __name__ == "__main__":
  #Webサーバ立ち上げ
  app.debug=True
  app.run(host='xxx.xxx.xxx.xxx')

3-3.htmlファイル

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>folium Map Test</title>
</head>
<body>
  <div style="height:768px; width:1024px;">{% include "map.html"%}</div>
</body>
</html>

4.実行する

(flask-test) [xxx flask-test]$ python folium_map.py
 * Serving Flask app "folium_map" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://xxx.xxx.xxx.xxx:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 217-574-955

5.ブラウザで表示する

http://xxx.xxx.xxx.xxx:5000/foliummap

map.PNG

0
0
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
0