def create_wms_source(raster_source, app_state): url = raster_source.url username = raster_source.username password = raster_source.password http_client = create_http_client(username, password) coverage = coverage_from_geojson(raster_source.download_coverage) if coverage: # wrap to prevent partial tiles coverage = AlwaysContainsCoverage(coverage) request = create_request({'url': url, 'layers': raster_source.layer}, {}, version='1.1.1') image_opts = ImageOptions(resampling='bicubic', transparent=True) supported_srs = None if raster_source.srs != 'EPSG:3857': supported_srs = [SRS(raster_source.srs)] client = WMSClient(request, http_client=http_client) source = WMSSource(client, coverage=coverage, supported_srs=supported_srs, image_opts=image_opts, ) # wrap to prevent partial tiles source.extent = AlwaysContainsMapExtent(source.extent) return source
def test_transformed_request_transparent(self): self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={ 'layers': 'foo', 'transparent': 'true' }) self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear')) req = MapQuery((-200000, -200000, 200000, 200000), (512, 512), SRS(900913), 'png') resp = self.source.get_map(req) eq_(len(self.http.requested), 1) assert wms_query_eq( self.http.requested[0], TESTSERVER_URL + '/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326' '&VERSION=1.1.1&WIDTH=512&STYLES=&transparent=true' '&BBOX=-1.79663056824,-1.7963362121,1.79663056824,1.7963362121') img = resp.as_image() assert img.mode in ('P', 'RGBA') img = img.convert('RGBA') eq_(img.getpixel((5, 5))[3], 0)
class TestWMSSourceWithClient(object): def setup(self): self.req_template = WMS111MapRequest( url='http://%s:%d/service?' % TEST_SERVER_ADDRESS, param={'format': 'image/png', 'layers': 'foo'}) self.client = WMSClient(self.req_template) self.source = WMSSource(self.client) def test_get_map(self): with tmp_image((512, 512)) as img: expected_req = ({'path': r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512'}, {'body': img.read(), 'headers': {'content-type': 'image/png'}}) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) result = self.source.get_map(q) assert isinstance(result, ImageSource) eq_(result.size, (512, 512)) assert is_png(result.as_buffer(seekable=True)) eq_(result.as_image().size, (512, 512)) def test_get_map_non_image_content_type(self): with tmp_image((512, 512)) as img: expected_req = ({'path': r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512'}, {'body': img.read(), 'headers': {'content-type': 'text/plain'}}) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) try: self.source.get_map(q) except SourceError, e: assert 'no image returned' in e.args[0] else: assert False, 'no SourceError raised'
def create_wms_source(raster_source, app_state): url = raster_source.url username = raster_source.username password = raster_source.password http_client = create_http_client(username, password) coverage = coverage_from_geojson(raster_source.download_coverage) if coverage: # wrap to prevent partial tiles coverage = AlwaysContainsCoverage(coverage) request = create_request({ 'url': url, 'layers': raster_source.layer }, {}, version='1.1.1') image_opts = ImageOptions(resampling='bicubic', transparent=True) supported_srs = None if raster_source.srs != 'EPSG:3857': supported_srs = [SRS(raster_source.srs)] client = WMSClient(request, http_client=http_client) source = WMSSource( client, coverage=coverage, supported_srs=supported_srs, image_opts=image_opts, ) # wrap to prevent partial tiles source.extent = AlwaysContainsMapExtent(source.extent) return source
class TestWMSSourceTransform(object): def setup(self): self.http_client = MockHTTPClient() self.req_template = WMS111MapRequest(url='http://localhost/service?', param={ 'format': 'image/png', 'layers': 'foo' }) self.client = WMSClient(self.req_template, http_client=self.http_client) self.source = WMSSource(self.client, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear')) def test_get_map(self): self.source.get_map( MapQuery((-180, -90, 180, 90), (300, 150), SRS(4326))) assert query_eq( self.http_client.requested[0], "http://localhost/service?" "layers=foo&width=300&version=1.1.1&bbox=-180,-90,180,90&service=WMS" "&format=image%2Fpng&styles=&srs=EPSG%3A4326&request=GetMap&height=150" ) def test_get_map_transformed(self): self.source.get_map( MapQuery((556597, 4865942, 1669792, 7361866), (300, 150), SRS(900913))) assert wms_query_eq( self.http_client.requested[0], "http://localhost/service?" "layers=foo&width=300&version=1.1.1" "&bbox=4.99999592195,39.9999980766,14.999996749,54.9999994175&service=WMS" "&format=image%2Fpng&styles=&srs=EPSG%3A4326&request=GetMap&height=450" )
class TestWMSSourceTransform(object): def setup(self): self.http_client = MockHTTPClient() self.req_template = WMS111MapRequest( url="http://localhost/service?", param={"format": "image/png", "layers": "foo"} ) self.client = WMSClient(self.req_template, http_client=self.http_client) self.source = WMSSource(self.client, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling="bilinear")) def test_get_map(self): self.source.get_map(MapQuery((-180, -90, 180, 90), (300, 150), SRS(4326))) assert query_eq( self.http_client.requested[0], "http://localhost/service?" "layers=foo&width=300&version=1.1.1&bbox=-180,-90,180,90&service=WMS" "&format=image%2Fpng&styles=&srs=EPSG%3A4326&request=GetMap&height=150", ) def test_get_map_transformed(self): self.source.get_map(MapQuery((556597, 4865942, 1669792, 7361866), (300, 150), SRS(900913))) assert wms_query_eq( self.http_client.requested[0], "http://localhost/service?" "layers=foo&width=300&version=1.1.1" "&bbox=4.99999592195,39.9999980766,14.999996749,54.9999994175&service=WMS" "&format=image%2Fpng&styles=&srs=EPSG%3A4326&request=GetMap&height=450", )
def setup(self): self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers': 'foo'}) self.http = MockHTTPClient() self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear'))
def setup(self): self.http_client = MockHTTPClient() self.req_template = WMS111MapRequest(url='http://localhost/service?', param={ 'format': 'image/png', 'layers': 'foo' }) self.client = WMSClient(self.req_template, http_client=self.http_client) self.source = WMSSource(self.client, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear'))
def setup(self): self.req_template = WMS111MapRequest(url='http://%s:%d/service?' % TEST_SERVER_ADDRESS, param={ 'format': 'image/png', 'layers': 'foo' }) self.client = WMSClient(self.req_template) self.source = WMSSource(self.client)
def __init__(self, client, image_opts=None, coverage=None, supported_srs=None, supported_formats=None): WMSSource.__init__(self, client, image_opts=image_opts, coverage=coverage, supported_srs=supported_srs, supported_formats=supported_formats)
def test_same_source(self): layers = [ WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "a"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "b"}, {})) ), ] combined = combined_layers(layers, self.q) assert len(combined) == 1 assert combined[0].client.request_template.params.layers == ["a", "b"]
class TestWMSSourceWithClient(object): def setup(self): self.req_template = WMS111MapRequest( url='http://%s:%d/service?' % TEST_SERVER_ADDRESS, param={'format': 'image/png', 'layers': 'foo'}) self.client = WMSClient(self.req_template) self.source = WMSSource(self.client) def test_get_map(self): with tmp_image((512, 512)) as img: expected_req = ({'path': r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512'}, {'body': img.read(), 'headers': {'content-type': 'image/png'}}) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) result = self.source.get_map(q) assert isinstance(result, ImageSource) eq_(result.size, (512, 512)) assert is_png(result.as_buffer(seekable=True)) eq_(result.as_image().size, (512, 512)) def test_get_map_non_image_content_type(self): with tmp_image((512, 512)) as img: expected_req = ({'path': r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512'}, {'body': img.read(), 'headers': {'content-type': 'text/plain'}}) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) try: self.source.get_map(q) except SourceError as e: assert 'no image returned' in e.args[0] else: assert False, 'no SourceError raised' def test_basic_auth(self): http_client = HTTPClient(self.req_template.url, username='******', password='******') self.client.http_client = http_client def assert_auth(req_handler): assert 'Authorization' in req_handler.headers auth_data = req_handler.headers['Authorization'].split()[1] auth_data = base64.b64decode(auth_data.encode('utf-8')).decode('utf-8') eq_(auth_data, 'foo:bar@') return True expected_req = ({'path': r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512&STYLES=', 'require_basic_auth': True, 'req_assert_function': assert_auth}, {'body': b'no image', 'headers': {'content-type': 'image/png'}}) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) self.source.get_map(q)
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=10, minimize_meta_requests=True)
def setup(self): self.http_client = MockHTTPClient() self.req_template = WMS111MapRequest( url="http://localhost/service?", param={"format": "image/png", "layers": "foo"} ) self.client = WMSClient(self.req_template, http_client=self.http_client) self.source = WMSSource(self.client, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling="bilinear"))
def test_get_map_with_res_range(self, mock_file_cache, mock_wms_client, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) res_range = resolution_range(1000, 10) source = WMSSource(mock_wms_client, res_range=res_range) image_opts = ImageOptions(resampling='nearest') tile_mgr = TileManager(grid, mock_file_cache, [source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=image_opts, locker=tile_locker) layer = CacheMapLayer(tile_mgr, image_opts=default_image_opts) with pytest.raises(BlankImage): result = layer.get_map( MapQuery( (-20037508.34, -20037508.34, 20037508.34, 20037508.34), (500, 500), SRS(900913), 'png')) assert mock_file_cache.stored_tiles == set() result = layer.get_map( MapQuery((0, 0, 10000, 10000), (50, 50), SRS(900913), 'png')) assert mock_file_cache.stored_tiles == \ set([(512, 257, 10), (513, 256, 10), (512, 256, 10), (513, 257, 10)]) assert result.size == (50, 50)
def __init__(self, client, image_opts=None, coverage=None, res_range=None, supported_srs=None, supported_formats=None, error_handler=None): WMSSource.__init__(self, client, image_opts=image_opts, coverage=coverage, res_range=res_range, supported_srs=supported_srs, supported_formats=supported_formats, error_handler=error_handler)
def test_get_map_with_res_range(self): res_range = resolution_range(1000, 10) self.source = WMSSource(self.client, res_range=res_range) self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts, locker=self.locker) self.layer = CacheMapLayer(self.tile_mgr, image_opts=default_image_opts) try: result = self.layer.get_map( MapQuery( (-20037508.34, -20037508.34, 20037508.34, 20037508.34), (500, 500), SRS(900913), 'png')) except BlankImage: pass else: assert False, 'expected BlankImage exception' eq_(self.file_cache.stored_tiles, set()) result = self.layer.get_map( MapQuery((0, 0, 10000, 10000), (50, 50), SRS(900913), 'png')) eq_( self.file_cache.stored_tiles, set([(512, 257, 10), (513, 256, 10), (512, 256, 10), (513, 257, 10)])) eq_(result.size, (50, 50))
def tile_mgr(self, mock_file_cache, mock_wms_client, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) source = WMSSource(mock_wms_client) return TileManager(grid, mock_file_cache, [source], 'png', meta_size=[2, 2], meta_buffer=10, minimize_meta_requests=True, locker=tile_locker, )
def source(self, mock_http_client): req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers': 'foo'}) wms = WMSClient(req, http_client=mock_http_client) return WMSSource(wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear'))
def source(self, mock_http_client): req_template = WMS111MapRequest(url='http://localhost/service?', param={ 'format': 'image/png', 'layers': 'foo' }) client = WMSClient(req_template, http_client=mock_http_client) return WMSSource(client, supported_srs=SupportedSRS([SRS(4326)]), image_opts=ImageOptions(resampling='bilinear'))
def test_http_error_handler(self, client): error_handler = HTTPSourceErrorHandler() error_handler.add_handler(500, (255, 0, 0), cacheable=True) error_handler.add_handler(400, (0, 0, 0), cacheable=False) source = WMSSource(client, error_handler=error_handler) expected_req = [ ( { 'path': r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512&STYLES=' }, { 'body': b'error', 'status': 500, 'headers': { 'content-type': 'text/plain' }, }, ), ( { 'path': r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512&STYLES=' }, { 'body': b'error', 'status': 400, 'headers': { 'content-type': 'text/plain' }, }, ), ] with mock_httpd(TEST_SERVER_ADDRESS, expected_req): query = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) resp = source.get_map(query) assert resp.cacheable assert resp.as_image().getcolors() == [((512 * 512), (255, 0, 0))] resp = source.get_map(query) assert not resp.cacheable assert resp.as_image().getcolors() == [((512 * 512), (0, 0, 0))]
def layer(self, mock_file_cache, mock_wms_client, tile_locker): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) source = WMSSource(mock_wms_client) image_opts = ImageOptions(resampling='nearest') tile_mgr = TileManager(grid, mock_file_cache, [source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=image_opts, locker=tile_locker) return CacheMapLayer(tile_mgr, image_opts=default_image_opts)
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.image_opts = ImageOptions(format='image/png') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts)
def tile_mgr(self, mock_file_cache, tile_locker, mock_wms_client): grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) source = WMSSource(mock_wms_client) image_opts = ImageOptions(format='image/png') return TileManager(grid, mock_file_cache, [source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=image_opts, locker=tile_locker, )
def test_similar_srs(self): # request in 3857 and source supports only 900913 # 3857 and 900913 are equal but the client requests must use 900913 self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers':'foo', 'transparent': 'true'}) self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(900913)], image_opts=ImageOptions(resampling='bilinear')) req = MapQuery((-200000, -200000, 200000, 200000), (512, 512), SRS(3857), 'png') self.source.get_map(req) eq_(len(self.http.requested), 1) assert_query_eq(self.http.requested[0], TESTSERVER_URL+'/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A900913' '&VERSION=1.1.1&WIDTH=512&STYLES=&transparent=true' '&BBOX=-200000,-200000,200000,200000')
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png', lock_dir=tmp_lock_dir) self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.image_opts = ImageOptions(resampling='nearest') self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[2, 2], meta_buffer=0, image_opts=self.image_opts) self.layer = CacheMapLayer(self.tile_mgr, image_opts=default_image_opts)
def test_same_source(self): layers = [ WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'a' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'b' }, {}))), ] combined = combined_layers(layers, self.q) eq_(len(combined), 1) eq_(combined[0].client.request_template.params.layers, ['a', 'b'])
def setup(self): self.file_cache = MockFileCache('/dev/null', 'png') self.grid = TileGrid(SRS(4326), bbox=[-180, -90, 180, 90]) self.client = MockWMSClient() self.source = WMSSource(self.client) self.image_opts = ImageOptions(resampling='nearest', format='png') self.locker = TileLocker(tmp_lock_dir, 10, "id") self.tile_mgr = TileManager(self.grid, self.file_cache, [self.source], 'png', meta_size=[1, 1], meta_buffer=0, image_opts=self.image_opts, locker=self.locker) self.layer = CacheMapLayer(self.tile_mgr, image_opts=default_image_opts) self.layer.extent = BBOXCoverage([0, 0, 90, 45], SRS(4326)).extent
def test_mixed_params(self): layers = [ WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "a"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "b"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "c"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "d"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "e"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "f"}, {})) ), ] layers[0].supported_srs = ["EPSG:4326"] layers[1].supported_srs = ["EPSG:4326"] layers[2].supported_formats = ["image/png"] layers[3].supported_formats = ["image/png"] combined = combined_layers(layers, self.q) assert len(combined) == 3 assert combined[0].client.request_template.params.layers == ["a", "b"] assert combined[1].client.request_template.params.layers == ["c", "d"] assert combined[2].client.request_template.params.layers == ["e", "f"]
def test_mixed_hosts(self): layers = [ WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "a"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "b"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://bar/", "layers": "c"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://bar/", "layers": "d"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "e"}, {})) ), WMSSource( WMSClient(create_request({"url": "http://foo/", "layers": "f"}, {})) ), ] combined = combined_layers(layers, self.q) assert len(combined) == 3 assert combined[0].client.request_template.params.layers == ["a", "b"] assert combined[1].client.request_template.params.layers == ["c", "d"] assert combined[2].client.request_template.params.layers == ["e", "f"]
def test_mixed_params(self): layers = [ WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'a' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'b' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'c' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'd' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'e' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'f' }, {}))), ] layers[0].supported_srs = ["EPSG:4326"] layers[1].supported_srs = ["EPSG:4326"] layers[2].supported_formats = ["image/png"] layers[3].supported_formats = ["image/png"] combined = combined_layers(layers, self.q) eq_(len(combined), 3) eq_(combined[0].client.request_template.params.layers, ['a', 'b']) eq_(combined[1].client.request_template.params.layers, ['c', 'd']) eq_(combined[2].client.request_template.params.layers, ['e', 'f'])
def test_transformed_request_transparent(self): self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers':'foo', 'transparent': 'true'}) self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear')) req = MapQuery((-200000, -200000, 200000, 200000), (512, 512), SRS(900913), 'png') resp = self.source.get_map(req) eq_(len(self.http.requested), 1) assert_query_eq(self.http.requested[0], TESTSERVER_URL+'/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326' '&VERSION=1.1.1&WIDTH=512&STYLES=&transparent=true' '&BBOX=-1.79663056824,-1.7963362121,1.79663056824,1.7963362121') img = resp.as_image() assert img.mode in ('P', 'RGBA') img = img.convert('RGBA') eq_(img.getpixel((5, 5))[3], 0)
def test_transformed_request_transparent(self): self.req = WMS111MapRequest( url=TESTSERVER_URL + "/service?map=foo", param={"layers": "foo", "transparent": "true"} ) self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling="bilinear")) req = MapQuery((-200000, -200000, 200000, 200000), (512, 512), SRS(900913), "png") resp = self.source.get_map(req) eq_(len(self.http.requested), 1) assert wms_query_eq( self.http.requested[0], TESTSERVER_URL + "/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326" "&VERSION=1.1.1&WIDTH=512&STYLES=&transparent=true" "&BBOX=-1.79663056824,-1.7963362121,1.79663056824,1.7963362121", ) img = resp.as_image() assert img.mode in ("P", "RGBA") img = img.convert("RGBA") eq_(img.getpixel((5, 5))[3], 0)
def test_mixed_hosts(self): layers = [ WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'a' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'b' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://bar/', 'layers': 'c' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://bar/', 'layers': 'd' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'e' }, {}))), WMSSource( WMSClient( create_request({ 'url': 'http://foo/', 'layers': 'f' }, {}))), ] combined = combined_layers(layers, self.q) eq_(len(combined), 3) eq_(combined[0].client.request_template.params.layers, ['a', 'b']) eq_(combined[1].client.request_template.params.layers, ['c', 'd']) eq_(combined[2].client.request_template.params.layers, ['e', 'f'])
def setup(self): self.req_template = WMS111MapRequest( url="http://%s:%d/service?" % TEST_SERVER_ADDRESS, param={"format": "image/png", "layers": "foo"} ) self.client = WMSClient(self.req_template) self.source = WMSSource(self.client)
class TestWMSSourceWithClient(object): def setup(self): self.req_template = WMS111MapRequest( url="http://%s:%d/service?" % TEST_SERVER_ADDRESS, param={"format": "image/png", "layers": "foo"} ) self.client = WMSClient(self.req_template) self.source = WMSSource(self.client) def test_get_map(self): with tmp_image((512, 512)) as img: expected_req = ( { "path": r"/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326&styles=" "&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512" }, {"body": img.read(), "headers": {"content-type": "image/png"}}, ) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) result = self.source.get_map(q) assert isinstance(result, ImageSource) eq_(result.size, (512, 512)) assert is_png(result.as_buffer(seekable=True)) eq_(result.as_image().size, (512, 512)) def test_get_map_non_image_content_type(self): with tmp_image((512, 512)) as img: expected_req = ( { "path": r"/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326&styles=" "&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512" }, {"body": img.read(), "headers": {"content-type": "text/plain"}}, ) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) try: self.source.get_map(q) except SourceError as e: assert "no image returned" in e.args[0] else: assert False, "no SourceError raised" def test_basic_auth(self): http_client = HTTPClient(self.req_template.url, username="******", password="******") self.client.http_client = http_client def assert_auth(req_handler): assert "Authorization" in req_handler.headers auth_data = req_handler.headers["Authorization"].split()[1] auth_data = base64.b64decode(auth_data.encode("utf-8")).decode("utf-8") eq_(auth_data, "foo:bar@") return True expected_req = ( { "path": r"/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326" "&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512&STYLES=", "require_basic_auth": True, "req_assert_function": assert_auth, }, {"body": b"no image", "headers": {"content-type": "image/png"}}, ) with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]): q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326)) self.source.get_map(q)
def setup(self): self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers':'foo'}) self.http = MockHTTPClient() self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear'))
class TestWMSSource(object): def setup(self): self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers':'foo'}) self.http = MockHTTPClient() self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear')) def test_request(self): req = MapQuery((-180.0, -90.0, 180.0, 90.0), (512, 256), SRS(4326), 'png') self.source.get_map(req) eq_(len(self.http.requested), 1) assert_query_eq(self.http.requested[0], TESTSERVER_URL+'/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=256&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=-180.0,-90.0,180.0,90.0&WIDTH=512&STYLES=') def test_transformed_request(self): req = MapQuery((-200000, -200000, 200000, 200000), (512, 512), SRS(900913), 'png') resp = self.source.get_map(req) eq_(len(self.http.requested), 1) assert_query_eq(self.http.requested[0], TESTSERVER_URL+'/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326' '&VERSION=1.1.1&WIDTH=512&STYLES=' '&BBOX=-1.79663056824,-1.7963362121,1.79663056824,1.7963362121') img = resp.as_image() assert img.mode in ('P', 'RGB') def test_similar_srs(self): # request in 3857 and source supports only 900913 # 3857 and 900913 are equal but the client requests must use 900913 self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers':'foo', 'transparent': 'true'}) self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(900913)], image_opts=ImageOptions(resampling='bilinear')) req = MapQuery((-200000, -200000, 200000, 200000), (512, 512), SRS(3857), 'png') self.source.get_map(req) eq_(len(self.http.requested), 1) assert_query_eq(self.http.requested[0], TESTSERVER_URL+'/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A900913' '&VERSION=1.1.1&WIDTH=512&STYLES=&transparent=true' '&BBOX=-200000,-200000,200000,200000') def test_transformed_request_transparent(self): self.req = WMS111MapRequest(url=TESTSERVER_URL + '/service?map=foo', param={'layers':'foo', 'transparent': 'true'}) self.wms = WMSClient(self.req, http_client=self.http) self.source = WMSSource(self.wms, supported_srs=[SRS(4326)], image_opts=ImageOptions(resampling='bilinear')) req = MapQuery((-200000, -200000, 200000, 200000), (512, 512), SRS(900913), 'png') resp = self.source.get_map(req) eq_(len(self.http.requested), 1) assert_query_eq(self.http.requested[0], TESTSERVER_URL+'/service?map=foo&LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326' '&VERSION=1.1.1&WIDTH=512&STYLES=&transparent=true' '&BBOX=-1.79663056824,-1.7963362121,1.79663056824,1.7963362121') img = resp.as_image() assert img.mode in ('P', 'RGBA') img = img.convert('RGBA') eq_(img.getpixel((5, 5))[3], 0)
def setup(self): self.req_template = WMS111MapRequest( url='http://%s:%d/service?' % TEST_SERVER_ADDRESS, param={'format': 'image/png', 'layers': 'foo'}) self.client = WMSClient(self.req_template) self.source = WMSSource(self.client)
def source(self, client): return WMSSource(client)