|
import waitress |
|
import yaml |
|
import logging |
|
from urllib.parse import unquote, parse_qs |
|
|
|
from mapproxy.config.config import load_default_config, load_config |
|
from mapproxy.config.spec import validate_options |
|
from mapproxy.config.validator import validate_references |
|
from mapproxy.config.loader import ProxyConfiguration, ConfigurationError |
|
from mapproxy.response import Response |
|
from mapproxy.version import version |
|
from mapproxy.wsgiapp import MapProxyApp |
|
|
|
logging.basicConfig() |
|
LOGGER = logging.getLogger(__name__) |
|
LOGGER.setLevel(logging.INFO) |
|
|
|
|
|
def mapproxy_config(environ, |
|
url="https://geo.weather.gc.ca/geomet/?", |
|
disable_storage=False, |
|
): |
|
qs = parse_qs(environ['QUERY_STRING']) |
|
|
|
qs = {k.lower(): v for k, v in qs.items()} |
|
|
|
if "layers" in qs: |
|
layers = qs["layers"] |
|
else: |
|
LOGGER.debug(qs) |
|
raise Exception("No layers passed") |
|
|
|
if "," in layers or layers is None or len(layers) != 1: |
|
raise Exception("Only single layer is supported") |
|
|
|
# the upstream server is happy to return |
|
# an image without style parameter but |
|
# mapproxy isn't. |
|
# TODO: Is this related to wms 1.1.1 vs 1.3.0? |
|
if 'styles' not in qs: |
|
environ['QUERY_STRING'] = environ['QUERY_STRING'] + '&STYLES=' |
|
qs['styles'] = None |
|
|
|
if 'tiled' in qs and qs['tiled'] == ['true']: |
|
LOGGER.debug(qs['tiled']) |
|
environ['QUERY_STRING'] = environ['QUERY_STRING'].replace("TILED=true", "TILED=false") |
|
qs['tiled'] = False |
|
|
|
LOGGER.debug(layers) |
|
# At this point we are guaranteed to have only one layer. |
|
layer_name=layers[0] |
|
layer_id=layers[0].lower() |
|
|
|
default_source = { |
|
'type': 'wms', |
|
'req': { |
|
'url': url, |
|
'layers': layer_name, |
|
'forward_req_params': ['styles', 'time', 'elevation'], |
|
}, |
|
'wms_opts': { |
|
'featureinfo': True, |
|
'legendgraphic': True |
|
} |
|
} |
|
|
|
grids = { |
|
'default_grid': { |
|
'base': 'GLOBAL_WEBMERCATOR', |
|
'tile_size': [ 256, 256 ], |
|
}, |
|
} |
|
|
|
# A source is the WMS config |
|
sources = { |
|
layer_id : default_source |
|
} |
|
|
|
caches = { |
|
layer_id : { |
|
'sources': [ layer_id ], |
|
'grids': [ 'default_grid' ], |
|
'disable_storage': disable_storage, |
|
}, |
|
} |
|
|
|
# The layer is connected to the cache |
|
layers = [ |
|
{'name': layer_name, |
|
'sources': [layer_id], |
|
'title': layer_name, |
|
}, |
|
] |
|
|
|
services = { |
|
'wms': { |
|
'image_formats': ['image/png'], |
|
'md': { |
|
'abstract': layer_name, |
|
'title': layer_name |
|
}, |
|
'on_source_errors': 'raise', |
|
'versions': ['1.3.0'] |
|
}, |
|
'wmts': { |
|
'restful': True, |
|
'restful_template': |
|
'/{Layer}/{TileMatrixSet}/{TileMatrix}/{TileCol}/{TileRow}.png', |
|
}, |
|
'tms': { |
|
'origin': 'nw', |
|
}, |
|
'demo': None, |
|
} |
|
|
|
global_config = { |
|
'http': { |
|
'ssl_no_cert_checks': True |
|
}, |
|
'meta_size' : [8, 8] |
|
} |
|
|
|
|
|
extra_config = { |
|
'caches': caches, |
|
'grids': grids, |
|
'layers': layers, |
|
'services': services, |
|
'sources': sources, |
|
'globals': global_config, |
|
} |
|
return extra_config |
|
|
|
|
|
def application(environ, start_response): |
|
extra_config = mapproxy_config(environ) |
|
|
|
conf = ProxyConfiguration(extra_config) |
|
LOGGER.debug(extra_config) |
|
app = MapProxyApp(conf.configured_services(), conf.base_config) |
|
|
|
return app(environ, start_response) |
|
|
|
|
|
waitress.serve(application, port=8000, url_scheme='http') |