Mapnik + TileStache + uWSGI / IOError:无法识别图像文件

时间:2019-06-26 11:51:10

标签: python uwsgi mapnik tilestache

我有一个带有Ansible设置脚本的Vagrant环境,它使用Mapnik + Postgis + TileStache + uWSGI + Nginx来提供一个新的Ubuntu 16.04服务器,用于提供地图图块。

一年前,一切工作正常。现在,在启动Vagrant容器之后,置备步骤可以正常工作,所有osm2pgsql导入都可以正常工作,并且我的TileStache吼叫声向我表明,nginx-> uwsgi-> tilestache在起作用。

我已经尝试测试PIL /枕头是否在工作。像下面这样的简单脚本可以正常工作:

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

我的tiletache配置:

{
    "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 /枕头软件包存在问题,因为要重现错误,仅代理OSM切片就足够了。目前不涉及向量导入或postgres。这些OSM磁贴被代理到http://tile.openstreetmap.org/ {Z} / {X} / {Y} .png

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

1 个答案:

答案 0 :(得分:0)

我发现,这对枕头没有问题。取而代之的是,我正在使用的当前版本的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的第262行(tilestache == 1.51.14):

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安装,所以我的tiletache文件位于: /usr/local/lib/python2.7/dist-packages/TileStache /

您应从此目录重新编译并随后重新启动tiletache:

$ sudo python -m compileall .
$ sudo service tilestache restart