需求是安装个mapnik库用python接入可以当做服务运行。
1,先安装docker镜像,使用centos8。
FROM centos:8
# 更新镜像来源为Vault源
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* && \
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
# 安装依赖和tools
RUN yum -y update && \
yum -y install epel-release && \
yum -y groupinstall "Development Tools" && \
yum -y install wget git python3 python3-devel cairo-devel \
freetype-devel harfbuzz-devel icu libicu-devel libjpeg-turbo-devel libpng-devel \
libtiff-devel proj proj-devel python3-distutils-extra && \
yum clean all
2.安装boots(Mapnik 是一个用于制作地图的开源工具包,它使用 C++ 编写,依赖于一些其他的开源库,Boost 是其中之一)
注意:最新的boots包中有部分函数名称有变化,mapnik要求的用>=1.61版本,我这里使用的1.68,最开始下载了1.84报错trim函数不存在!
步骤:
1.下载tar包
[https://www.boost.org/users/history/version_1_68_0.html](https://www.boost.org/users/history/version_1_68_0.html)
[https://www.boost.org/users/history/](https://www.boost.org/users/history/)版本列表
2.进入解压后的目录,运行
./bootstrap.sh --prefix=/usr/local --with-python=/usr/bin/python3 --with-python-root=/usr --with-python-version=3.6m
--prefix=/usr/local:这个参数指定了安装 Boost 库的目标路径,即将 Boost 安装到 /usr/local 目录下。
--with-python=/usr/bin/python3:指定了 Python 解释器的路径,告诉 bootstrap 脚本去哪里找 Python 解释器。在这里,Python 解释器的路径是 /usr/bin/python3。
--with-python-root=/usr:指定了 Python 的根目录路径。这个参数告诉 bootstrap 脚本 Python 安装的根目录在 /usr 目录下。
--with-python-version=3.6m:指定了 Python 的版本号。在这里,Python 的版本号是 3.6m。
这里指定python是因为后面用python调用mapnik需要用到boost.python这个模块(Boost 库提供了一个名为 boost.python 的模块,它允许开发人员将 C++ 代码与 Python 交互。通过 Boost.Python,你可以轻松地将现有的 C++ 代码包装成 Python 模块,从而在 Python 中调用这些 C++ 函数和类)
运行完之后,可以在当前目录下的project-config.jam中查看有没有python绑定,没有的话就手动去添加进去。
然后运行./b2 进行编译,编译完之后运行./b2 install 安装到系统环境中。
3.安装 mapnik
安装mapnik 官网步骤
git clone https://github.com/mapnik/mapnik.git
cd mapnik
git checkout v3.1.0
git submodule update --init
./configure && make && make test (.configure会检查需要的环境,需要boost包1.61版本以上,最开始下载了最新版1.84也不适用,编译时提示方法报错,当前使用版本1.68)
我这里没用make,用的scons去编译了
python3 scons/scons.py configure && \
python3 scons/scons.py && \
python3 scons/scons.py install
4.安装 python-mapnik (python-mapnik 是一个 Python 绑定库,它提供了在 Python 中使用 Mapnik 渲染引擎的功能)
git clone https://github.com/mapnik/python-mapnik.git
cd python-mapnik4
git branch -a
git fetch origin v3.0.x:v3.0.x
git checkout v3.0.x
然后进入文件夹运行 python setup.py install
编译完成以后就可以在python中使用了。
from flask import Flask, send_file
import psycopg2
import mapnik
import sys
# 将 Mapnik 库路径添加到 Python 搜索路径中
sys.path.append('/usr/local/lib')
# 创建 Flask 应用
app = Flask(__name__)
# 在函数外部尝试导入 mapnik 模块并验证
try:
mapnik_version = mapnik.mapnik_version()
print("Mapnik version:", mapnik_version)
except Exception as e:
print("Failed to import mapnik module:", str(e))
定义获取瓦片的路由处理函数
@app.route('/tiles/<int:z>/<int:x>/<int:y>.png')
def get_tile(z, x, y):
try:
# 创建地图对象
m = mapnik.Map(256, 256)
# 设置地图背景颜色
m.background = mapnik.Color('white')
# 其他地图设置代码...
# 渲染地图
img = mapnik.Image(256, 256)
mapnik.render(m, img)
# 将渲染后的图像保存为 PNG 格式并发送给客户端
img.save('tile.png', 'png')
return send_file('tile.png', mimetype='image/png')
except Exception as e:
return str(e), 500
启动 Flask 应用
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=6000)
生成了个空白图片。。。。