0

我有一个带有 ansible 配置脚本的 Vagrant 环境,它使用 Mapnik + Postgis + TileStache + uWSGI + Nginx 提供了一个全新的 Ubuntu 16.04 服务器,用于提供地图切片。

半年前,一切正常。现在,在启动 Vagrant 容器之后,配置步骤工作正常,所有 osm2pgsql 导入工作正常,我的 TileStache 发出 hello,这表明 nginx -> uwsgi -> tilestache 正在工作。

我已经尝试测试 PIL/pillow 是否有效。像下面这样的简单脚本完全可以工作:

from PIL import Image
import io
with open('test.png') as f:
   io = io.BytesIO(f.read())
im = Image.open(io)

我的tilestache配置:

{
    "cache": {
        "name": "Disk",
        "path": "./cache/",
          "umask": "0000"
    },
    "layers": {
        "osm_layer": {
            "provider": {
                "name": "proxy", 
                "url": "http://tile.openstreetmap.org/{Z}/{X}/{Y}.png"
            }
        }  
    }
}

但是当我尝试访问这样的平铺图像时:http://localhost/osm_layer/0/0/0.png它不起作用。通常这应该给我与http://tile.openstreetmap.org/0/0/0.png相同的图块

我的日志文件中出现以下错误:

Jun 26 09:07:16 gis uwsgi[31478]: Traceback (most recent call last):
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/TileStache/__init__.py", line 379, in __call__
Jun 26 09:07:16 gis uwsgi[31478]:     status_code, headers, content = requestHandler2(self.config, path_info, query_string, script_name)
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/TileStache/__init__.py", line 255, in requestHandler2
Jun 26 09:07:16 gis uwsgi[31478]:     status_code, headers, content = layer.getTileResponse(coord, extension)
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/TileStache/Core.py", line 414, in getTileResponse
Jun 26 09:07:16 gis uwsgi[31478]:     tile = self.render(coord, format)
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/TileStache/Core.py", line 500, in render
Jun 26 09:07:16 gis uwsgi[31478]:     tile = provider.renderTile(width, height, srs, coord)
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/TileStache/Providers.py", line 250, in renderTile
Jun 26 09:07:16 gis uwsgi[31478]:     tile = Verbatim(body)
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/TileStache/Providers.py", line 164, in __init__
Jun 26 09:07:16 gis uwsgi[31478]:     self.format = self.image().format
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/TileStache/Providers.py", line 170, in image
Jun 26 09:07:16 gis uwsgi[31478]:     self._image = Image.open(self.buffer)
Jun 26 09:07:16 gis uwsgi[31478]:   File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2295, in open
Jun 26 09:07:16 gis uwsgi[31478]:     % (filename if filename else fp))
Jun 26 09:07:16 gis uwsgi[31478]: IOError: cannot identify image file <StringIO.StringIO instance at 0x7f513b7f5a28>
Jun 26 09:07:16 gis uwsgi[31478]: [pid: 31484|app: 0|req: 3/6] 192.168.20.1 () {42 vars in 681 bytes} [Wed Jun 26 09:07:16 2019] GET /osm_layer/0/0/0.png => generated 0 bytes in 138 msecs (HTTP/2.0 500) 0 headers in 0 bytes (0 switches on core 0)

似乎相应的包、图像文件或 PIL/pillow 包存在问题,因为要重现错误,仅代理 OSM 瓦片就足够了。目前不涉及矢量导入或 postgres。这些 OSM 瓦片被代理到http://tile.openstreetmap.org/ {Z}/{X}/{Y}.png

我还可以看到tilestache 创建了缓存目录结构,但是由于存在IOError,因此没有缓存实际的图块/图像。

4

1 回答 1

1

我发现,Pillow 没有问题。相反,我正在使用的当前版本的 TileStache (tilestache==1.51.14) 缺少用户代理,OSM 不会接受。为了完整起见,您还应该传递一个推荐人(https://wiki.openstreetmap.org/wiki/DE:Tile_usage_policy

有关更多信息,请参阅https://github.com/TileStache/TileStache/issues/360

解决方法: 将用户代理添加到/usr/local/lib/python2.7/dist-packages/TileStache/Providers.py中的 renderTile 函数:

添加到 Providers.py (tilestache==1.51.14) 的第 262 行:

url_opener.addheaders = [('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'), ('Referer', 'https://example.com/')]

我是通过 pip 而不是 apt 安装的,所以我的 tilestache 文件位于:/usr/local/lib/python2.7/dist-packages/TileStache/

之后,您应该从此目录重新编译并重新启动tilestache:

$ sudo python -m compileall .
$ sudo service tilestache restart
于 2019-10-08T07:47:20.813 回答