maproxy提供了更新在发送给客户机之前响应wms getmap或tile请求而生成的图像的功能。这可以用于以某种方式装饰图像,例如应用图像水印或应用效果。
注解
一些Python编程和相关知识 WSGI 并且需要使用wsgi中间件来利用这个特性。
以类似于 authorization 被处理。您必须编写一个包装maproxy应用程序的wsgi过滤器,以便注册一个接受要修饰的ImageSource的回调。
通过为键分配函数来注册回调 decorate_img
在wsgi环境中。在响应中发送图像之前,maproxy会检查环境并调用回调,传递ImageSource和许多与当前请求相关的其他参数。然后回调必须返回将在响应中发送的有效ImageSource实例。
一个简单的中间件,用关于请求的信息注释每个图像,它可能看起来像:
from mapproxy.image import ImageSource
from PIL import ImageColor, ImageDraw, ImageFont
def annotate_img(image, service, layers, environ, query_extent, **kw):
# Get the PIL image and convert to RGBA to ensure we can use black
# for the text
img = image.as_image().convert('RGBA')
text = ['service: %s' % service]
text.append('layers: %s' % ', '.join(layers))
text.append('srs: %s' % query_extent[0])
text.append('bounds:')
for coord in query_extent[1]:
text.append(' %s' % coord)
draw = ImageDraw.Draw(img)
font = ImageFont.load_default()
fill = ImageColor.getrgb('black')
line_y = 10
for line in text:
line_w, line_h = font.getsize(line)
draw.text((10, line_y), line, font=font, fill=fill)
line_y = line_y + line_h
# Return a new ImageSource specifying the updated PIL image and
# the image options from the original ImageSource
return ImageSource(img, image.image_opts)
class RequestInfoFilter(object):
"""
Simple MapProxy decorate_img middleware.
Annotates map images with information about the request.
"""
def __init__(self, app, global_conf):
self.app = app
def __call__(self, environ, start_response):
# Add the callback to the WSGI environment
environ['mapproxy.decorate_img'] = annotate_img
return self.app(environ, start_response)
您需要使用定制的装饰中间件包装maproxy应用程序。对于部署脚本,可能如下所示:
application = make_wsgi_app('./mapproxy.yaml')
application = RequestInfoFilter(application)
为了 PasteDeploy 你可以使用 filter-with
选择权。这个 config.ini
看起来像:
[app:mapproxy]
use = egg:MapProxy#app
mapproxy_conf = %(here)s/mapproxy.yaml
filter-with = requestinfo
[filter:requestinfo]
paste.filter_app_factory = mydecoratemodule:RequestInfoFilter
[server:main]
...
decorate_img 函数签名:
decorate_img
(image, service, layers=[], environ=None, query_extent=None, **kw)¶image -- 要修饰的ImageSource实例
service -- 与当前请求相关联的服务(例如 wms.map
, tms
或 wmts
)
layers -- 请求中指定的层名称列表
environ -- 请求 WSGI 环境
query_extent -- 一组SRS(例如 EPSG:4326
)以及请求的bbox
ImageSource
这个 environ
和 query_extent
参数是可选的,可以被回调忽略。这些参数可能在未来版本的maproxy中得到扩展。因此,您应该在catch all关键字参数(即 **kw
)
注解
可调用文件的实际名称不重要,只有环境键 mapproxy.decorate_img
很重要。
函数应该返回一个有效的ImageSource实例,或者是传递的实例,或者是一个新实例,具体取决于实现。