def test_combine_mixed(self): docs = [ JSONFeatureInfoDoc(b'{"results": [{"foo": 1}]}'), TextFeatureInfoDoc(b"Plain Text"), ] result, infotype = combine_docs(docs, None) assert b"""{"results": [{"foo": 1}]}\nPlain Text""" == result assert infotype == "text"
def test_combine_xml(self): docs = [ XMLFeatureInfoDoc("<root><a>foo</a></root>"), XMLFeatureInfoDoc("<root><b>bar</b></root>"), ] result, infotype = combine_docs(docs, None) assert b"""<root><a>foo</a><b>bar</b></root>""" == result assert infotype == "xml"
def test_combine_json(self): docs = [ JSONFeatureInfoDoc("{}"), JSONFeatureInfoDoc('{"results": [{"foo": 1}]}'), JSONFeatureInfoDoc('{"results": [{"bar": 2}]}'), ] result, infotype = combine_docs(docs, None) assert """{"results": [{"foo": 1}, {"bar": 2}]}""" == result assert infotype == "json"
def featureinfo(self, request): infos = [] self.check_request(request, self.info_formats) tile_layer = self.layers[request.layer][request.tilematrixset] if not request.format: request.format = tile_layer.format bbox = tile_layer.grid.tile_bbox(request.tile) query = InfoQuery( bbox, tile_layer.grid.tile_size, tile_layer.grid.srs, request.pos, request.infoformat, ) self.check_request_dimensions(tile_layer, request) coverage = self.authorize_tile_layer(tile_layer, request, featureinfo=True) if not tile_layer.info_sources: raise RequestError('layer %s not queryable' % str(request.layer), code='OperationNotSupported', request=request) if coverage and not coverage.contains(query.coord, query.srs): infos = [] else: for source in tile_layer.info_sources: info = source.get_info(query) if info is None: continue infos.append(info) mimetype = request.infoformat if not infos: return Response('', mimetype=mimetype) resp, _ = combine_docs(infos) return Response(resp, mimetype=mimetype)
def test_combine_transform_xml(self, tmpdir): xsl = tmpdir.join("transform.xsl") xsl.write("""<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <root> <xsl:apply-templates select='/a/b'/> </root> </xsl:template> <xsl:template match="/a/b"> <foo><xsl:value-of select="text()" /></foo> </xsl:template> </xsl:stylesheet> """) transf = XSLTransformer(xsl.strpath) docs = [ XMLFeatureInfoDoc("<a><b>foo1</b></a>"), XMLFeatureInfoDoc("<a><b>foo2</b></a>"), ] result, infotype = combine_docs(docs, transf) assert b"""<root><foo>foo1</foo><foo>foo2</foo></root>""" == result assert infotype is None
def featureinfo(self, request): infos = [] self.check_featureinfo_request(request) p = request.params query = InfoQuery(p.bbox, p.size, SRS(p.srs), p.pos, p['info_format'], format=request.params.format or None, feature_count=p.get('feature_count')) actual_layers = odict() for layer_name in request.params.query_layers: layer = self.layers[layer_name] if not layer.queryable: raise RequestError('layer %s is not queryable' % layer_name, request=request) for layer_name, info_layers in layer.info_layers_for_query(query): actual_layers[layer_name] = info_layers authorized_layers, coverage = self.authorized_layers( 'featureinfo', actual_layers.keys(), request.http.environ, query_extent=(query.srs.srs_code, query.bbox)) self.filter_actual_layers(actual_layers, request.params.layers, authorized_layers) # outside of auth-coverage if coverage and not coverage.contains(query.coord, query.srs): infos = [] else: info_layers = [] for layers in actual_layers.values(): info_layers.extend(layers) for layer in info_layers: info = layer.get_info(query) if info is None: continue infos.append(info) mimetype = None if 'info_format' in request.params: mimetype = request.params.info_format if not infos: return Response('', mimetype=mimetype) if self.fi_transformers: if not mimetype: if 'xml' in self.fi_transformers: info_type = 'xml' elif 'html' in self.fi_transformers: info_type = 'html' else: info_type = 'text' mimetype = mimetype_from_infotype(request.version, info_type) else: info_type = infotype_from_mimetype(request.version, mimetype) resp, actual_info_type = combine_docs( infos, self.fi_transformers[info_type]) if actual_info_type is not None and info_type != actual_info_type: mimetype = mimetype_from_infotype(request.version, actual_info_type) else: resp, info_type = combine_docs(infos) mimetype = mimetype_from_infotype(request.version, info_type) return Response(resp, mimetype=mimetype)