Skip to content

Instantly share code, notes, and snippets.

@ingenieroariel
Created March 11, 2020 10:25
Show Gist options
  • Save ingenieroariel/3af5603d5bc32b2ab0e16af8841da488 to your computer and use it in GitHub Desktop.
Save ingenieroariel/3af5603d5bc32b2ab0e16af8841da488 to your computer and use it in GitHub Desktop.
Mapproxy Cache Dynamic Test

This code allows testing a mapproxy cache with variables like dimension and styles that are prone to be overwritten, it does so by comparing the histograms of a remotely requested resource and a local one.

On one terminal run (using the piensa/mapproxy:dimensions branch and the less-strict.patch here):

python3 cache.py

On another terminal run:

xargs <sample-requests.txt curl

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')
index bcbf4552..e0f07d78 100644
--- a/mapproxy/request/wms/__init__.py
+++ b/mapproxy/request/wms/__init__.py
@@ -247,12 +247,7 @@ class WMSMapRequest(WMSRequest):
raise RequestError('unsupported srs: ' + self.params['srs'],
code='InvalidSRS', request=self)
def validate_styles(self):
- if 'styles' in self.params:
- styles = self.params['styles']
- if not set(styles.split(',')).issubset(set(['default', '', 'inspire_common:DEFAULT'])):
- raise RequestError('unsupported styles: ' + self.params['styles'],
- code='StyleNotDefined', request=self)
-
+ return
@property
def exception_handler(self):
@@ -371,7 +366,7 @@ class WMS130MapRequest(WMSMapRequest):
request_params = WMS130MapRequestParams
xml_exception_handler = exception.WMS130ExceptionHandler
fixed_params = {'request': 'GetMap', 'version': '1.3.0', 'service': 'WMS'}
- expected_param = ['version', 'request', 'layers', 'styles', 'crs', 'bbox',
+ expected_param = ['version', 'request', 'layers', 'bbox',
'width', 'height', 'format']
def adapt_to_111(self):
del self.params['wmtver']
http://localhost:8000/service?time=2020-02-24T05:30:00Z&service=WMS&request=GetMap&version=1.3.0&layers=RADAR_1KM_RRAI&styles=&format=image%2Fpng&transparent=true&style=RADARURPPRECIPR14-LINEAR&width=752&height=639&crs=EPSG%3A3857&bbox=-8168978.086893358%2C5717489.715731186%2C-7709132.924729737%2C6108235.804325006
http://localhost:8000/service?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX=-12523442.714243278,5009377.085697312,-10018754.171394622,7514065.628545966&CRS=EPSG:3857&WIDTH=1000&HEIGHT=1000&LAYERS=RADAR_1KM_RDBR&TILED=true&FORMAT=image/png
http://localhost:8000/service?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX=-17532819.79994059,2504688.542848654,-15028131.257091932,5009377.085697312&CRS=EPSG:3857&WIDTH=1000&HEIGHT=1000&LAYERS=RADAR_1KM_RDBR&TILED=true&FORMAT=image/png
http://localhost:8000/service?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX=-15028131.257091932,5009377.085697312,-10018754.171394622,10018754.17139462&CRS=EPSG:3857&WIDTH=1000&HEIGHT=1000&LAYERS=RADAR_1KM_RDBR&TILED=true&FORMAT=image/png
/?LAYERS=RADAR_RDBR&REQUEST=GetMap&SERVICE=WMS&FORMAT=image/png&STYLES=&HEIGHT=500&VERSION=1.1.1&SRS=EPSG:4326&WIDTH=1829&BBOX=-87.8551992428217,44.4091846355662,-47.6672109687688,51.732917346786&TRANSPARENT=TRUE&EXCEPTIONS=application/vnd.ogc.se_inimage
http://localhost:8000/service?time=2020-02-24T05:10:00Z&service=WMS&request=GetMap&version=1.3.0&layers=RADAR_1KM_RRAI&styles=&format=image%2Fpng&transparent=true&style=RADARURPPRECIPR14-LINEAR&width=752&height=639&crs=EPSG%3A3857&bbox=-8168978.086893358%2C5717489.715731186%2C-7709132.924729737%2C6108235.804325006
http://localhost:8000/service?time=2020-02-24T05:20:00Z&service=WMS&request=GetMap&version=1.3.0&layers=RADAR_1KM_RRAI&styles=&format=image%2Fpng&transparent=true&style=RADARURPPRECIPR14-LINEAR&width=752&height=639&crs=EPSG%3A3857&bbox=-8168978.086893358%2C5717489.715731186%2C-7709132.924729737%2C6108235.804325006
http://localhost:8000/service?time=2020-02-24T05:40:00Z&service=WMS&request=GetMap&version=1.3.0&layers=RADAR_1KM_RRAI&styles=&format=image%2Fpng&transparent=true&style=RADARURPPRECIPR14-LINEAR&width=752&height=639&crs=EPSG%3A3857&bbox=-8168978.086893358%2C5717489.715731186%2C-7709132.924729737%2C6108235.804325006
http://localhost:8000/service?time=2020-02-24T05:50:00Z&service=WMS&request=GetMap&version=1.3.0&layers=RADAR_1KM_RRAI&styles=&format=image%2Fpng&transparent=true&style=RADARURPPRECIPR14-LINEAR&width=752&height=639&crs=EPSG%3A3857&bbox=-8168978.086893358%2C5717489.715731186%2C-7709132.924729737%2C6108235.804325006
http://localhost:8000/service?time=2020-02-24T06:00:00Z&service=WMS&request=GetMap&version=1.3.0&layers=RADAR_1KM_RRAI&styles=&format=image%2Fpng&transparent=true&style=RADARURPPRECIPR14-LINEAR&width=752&height=639&crs=EPSG%3A3857&bbox=-8168978.086893358%2C5717489.715731186%2C-7709132.924729737%2C6108235.804325006
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment