Coverage for gws-app/gws/gis/mpx/util.py: 0%
28 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1from PIL import ImageColor, ImageDraw, ImageFont
2from mapproxy.image import ImageSource
4# see https://mapproxy.org/docs/nightly/decorate_img.html
6def annotate(image, service, layers, environ, query_extent, **kw):
7 img = image.as_image().convert('RGBA')
9 text = [
10 'service: %s' % service,
11 'layers: %s' % ', '.join(layers),
12 'srs: %s' % query_extent[0]
13 ]
15 try:
16 args = environ['mapproxy.request'].args
17 text.append('x=%s y=%s z=%s' % (
18 args['tilecol'],
19 args['tilerow'],
20 args['tilematrix'],
21 ))
22 except:
23 pass
25 for coord in query_extent[1]:
26 text.append(' %s' % coord)
28 draw = ImageDraw.Draw(img)
29 font = ImageFont.load_default()
30 clr = ImageColor.getrgb('#ff0000')
32 line_y = 10
33 for line in text:
34 line_w, line_h = font.getsize(line)
35 draw.text((10, line_y), line, font=font, fill=clr)
36 line_y = line_y + line_h
38 draw.rectangle((0, 0) + img.size, outline=clr)
40 return ImageSource(img, image.image_opts)
43class AnnotationFilter(object):
44 """
45 Simple MapProxy decorate_img middleware.
47 Annotates map images with information about the request.
48 """
50 def __init__(self, app):
51 self.app = app
53 def __call__(self, environ, start_response):
54 # Add the callback to the WSGI environment
55 environ['mapproxy.decorate_img'] = annotate
57 return self.app(environ, start_response)