def _layer(self, layer, wms, wms_layers, time): errors = set() l = { 'id': layer.id, 'name': layer.name, 'metadata': {} } for metadata in layer.ui_metadata: l['metadata'][metadata.name] = get_url(metadata.value, self.request, errors=errors) if layer.geo_table: self._fill_editable(l, layer) if isinstance(layer, LayerV1): l.update({ 'type': layer.layer_type, 'public': layer.public, 'legend': layer.legend, 'isChecked': layer.is_checked, 'isLegendExpanded': layer.is_legend_expanded, }) if layer.identifier_attribute_field: l['identifierAttribute'] = layer.identifier_attribute_field if layer.disclaimer: l['disclaimer'] = layer.disclaimer if layer.icon: l['icon'] = get_url(layer.icon, self.request, errors=errors) if layer.kml: l['kml'] = get_url(layer.kml, self.request, errors=errors) if layer.metadata_url: l['metadataURL'] = layer.metadata_url if layer.legend_image: l['legendImage'] = get_url(layer.legend_image, self.request, errors=errors) if layer.layer_type == "internal WMS": self._fill_internal_wms(l, layer, wms, wms_layers, errors) errors |= self._merge_time(time, l, layer, wms, wms_layers) elif layer.layer_type == "external WMS": self._fill_external_wms(l, layer, errors) elif layer.layer_type == "WMTS": self._fill_wmts(l, layer, wms, wms_layers, errors) elif isinstance(layer, LayerInternalWMS): l["type"] = "internal WMS" l["layers"] = layer.layer self._fill_internal_wms(l, layer, wms, wms_layers, errors, version=2) errors |= self._merge_time(time, l, layer, wms, wms_layers) elif isinstance(layer, LayerExternalWMS): l["type"] = "external WMS" l["layers"] = layer.layer self._fill_external_wms(l, layer, errors, version=2) elif isinstance(layer, LayerWMTS): l["type"] = "WMTS" self._fill_wmts(l, layer, wms, wms_layers, errors, version=2) return l, errors
def _layer(self, layer, wms, wms_layers, time): errors = set() l = { "id": layer.id, "name": layer.name, "metadata": {} } for metadata in layer.ui_metadata: l["metadata"][metadata.name] = get_url(metadata.value, self.request, errors=errors) if layer.geo_table: self._fill_editable(l, layer) if isinstance(layer, LayerV1): l.update({ "type": layer.layer_type, "public": layer.public, "legend": layer.legend, "isChecked": layer.is_checked, "isLegendExpanded": layer.is_legend_expanded, }) if layer.identifier_attribute_field: l["identifierAttribute"] = layer.identifier_attribute_field if layer.disclaimer: l["disclaimer"] = layer.disclaimer if layer.icon: l["icon"] = get_url(layer.icon, self.request, errors=errors) if layer.kml: l["kml"] = get_url(layer.kml, self.request, errors=errors) if layer.metadata_url: l["metadataURL"] = layer.metadata_url if layer.legend_image: l["legendImage"] = get_url(layer.legend_image, self.request, errors=errors) if layer.layer_type == "internal WMS": self._fill_internal_wms(l, layer, wms, wms_layers, errors) errors |= self._merge_time(time, l, layer, wms, wms_layers) elif layer.layer_type == "external WMS": self._fill_external_wms(l, layer, errors) elif layer.layer_type == "WMTS": self._fill_wmts(l, layer, wms, wms_layers, errors) elif isinstance(layer, LayerInternalWMS): l["type"] = "internal WMS" l["layers"] = layer.layer self._fill_internal_wms(l, layer, wms, wms_layers, errors, version=2) errors |= self._merge_time(time, l, layer, wms, wms_layers) elif isinstance(layer, LayerExternalWMS): l["type"] = "external WMS" l["layers"] = layer.layer self._fill_external_wms(l, layer, errors, version=2) elif isinstance(layer, LayerWMTS): l["type"] = "WMTS" self._fill_wmts(l, layer, wms, wms_layers, errors, version=2) return l, errors
def test_get_url(self): from c2cgeoportal.lib import get_url request = create_dummy_request({ "package": "my_project", "servers": { "srv": "https://example.com/test", }, }) def static_url(path, **kwargs): return "http://server.org/" + path request.static_url = static_url self.assertEquals(get_url("static://pr:st/icon.png", request), "http://server.org/pr:st/icon.png") self.assertEquals(get_url("static:///icon.png", request), "http://server.org/c2cgeoportal:project/icon.png") self.assertEquals(get_url("config://srv/icon.png", request), "https://example.com/test/icon.png?") self.assertEquals( get_url("config://srv2/icon.png", request, "/icon2.png"), "/icon2.png") self.assertEquals(get_url("http://example.com/icon.png", request), "http://example.com/icon.png") self.assertEquals(get_url("https://example.com/icon.png", request), "https://example.com/icon.png") errors = set() self.assertEquals( get_url("config://srv2/icon.png", request, errors=errors), None) self.assertEquals(errors, set(["The server 'srv2' isn't found in the config"]))
def _fill_wmts(self, l, layer, wms, wms_layers, errors, version=1): l['url'] = get_url(layer.url, self.request, errors=errors) if layer.style: l['style'] = layer.style if layer.matrix_set: l['matrixSet'] = layer.matrix_set if version == 1: self._fill_wmts_v1(l, layer, wms, wms_layers, errors) else: self._fill_wmts_v2(l, layer)
def _fill_external_wms(self, l, layer, errors, version=1): self._fill_wms(l, layer, version=version) if version == 1: self._fill_legend_rule_query_string(l, layer, layer.url) if layer.min_resolution is not None: l['minResolutionHint'] = layer.min_resolution if layer.max_resolution is not None: l['maxResolutionHint'] = layer.max_resolution l['url'] = get_url(layer.url, self.request, errors=errors) l['isSingleTile'] = layer.is_single_tile
def test_get_url(self): from c2cgeoportal.lib import get_url request = create_dummy_request({ "package": "my_project", "servers": { "srv": "https://example.com/test", }, }) def static_url(path, **kwargs): return "http://server.org/" + path request.static_url = static_url self.assertEquals(get_url("static://pr:st/icon.png", request), "http://server.org/pr:st/icon.png") self.assertEquals(get_url("static:///icon.png", request), "http://server.org/c2cgeoportal:project/icon.png") self.assertEquals(get_url("config://srv/icon.png", request), "https://example.com/test/icon.png?") self.assertEquals(get_url("config://srv2/icon.png", request, "/icon2.png"), "/icon2.png") self.assertEquals(get_url("http://example.com/icon.png", request), "http://example.com/icon.png") self.assertEquals(get_url("https://example.com/icon.png", request), "https://example.com/icon.png") errors = set() self.assertEquals(get_url("config://srv2/icon.png", request, errors=errors), None) self.assertEquals(errors, set(["The server 'srv2' isn't found in the config"]))
def _themes( self, role_id, interface="main", filter_themes=True, version=1, catalogue=False, min_levels=1 ): """ This function returns theme information for the role identified by ``role_id``. ``mobile`` tells whether to retrieve mobile or desktop layers """ errors = set() layers = self._layers(role_id, version, interface) wms, wms_layers = self._wms_layers() themes = DBSession.query(Theme) themes = themes.filter(Theme.public.is_(True)) if role_id is not None: auth_themes = DBSession.query(Theme) auth_themes = auth_themes.filter(Theme.public.is_(False)) auth_themes = auth_themes.join(Theme.restricted_roles) auth_themes = auth_themes.filter(Role.id == role_id) themes = themes.union(auth_themes) themes = themes.order_by(Theme.ordering.asc()) if filter_themes and interface is not None: themes = themes.join(Theme.interfaces) themes = themes.filter(Interface.name == interface) export_themes = [] for theme in themes.all(): children, children_errors = self._get_children( theme, layers, wms, wms_layers, version, catalogue, min_levels ) errors |= children_errors # test if the theme is visible for the current user if len(children) > 0: icon = get_url( theme.icon, self.request, self.request.static_url( 'c2cgeoportal:static/images/blank.gif' ), errors=errors ) t = { 'id': theme.id, 'name': theme.name, 'icon': icon, 'children': children, 'functionalities': self._get_functionalities(theme), 'metadata': {}, } if version == 1: t.update({ 'in_mobile_viewer': theme.is_in_interface('mobile'), }) for metadata in theme.ui_metadata: t['metadata'][metadata.name] = get_url( metadata.value, self.request, errors=errors ) export_themes.append(t) return export_themes, errors
def _group( self, path, group, layers, depth=1, min_levels=1, catalogue=True, version=1, **kwargs ): children = [] errors = set() # escape loop if depth > 30: errors.add( "Too many recursions with group '%s'" % group.name ) return None, errors for tree_item in group.children: if type(tree_item) == LayerGroup: depth += 1 if type(group) == Theme or catalogue or \ group.is_internal_wms == tree_item.is_internal_wms: gp, gp_errors = self._group( "%s/%s" % (path, tree_item.name), tree_item, layers, depth=depth, min_levels=min_levels, catalogue=catalogue, version=version, **kwargs ) errors |= gp_errors if gp is not None: children.append(gp) else: errors.add( "Group '%s' cannot be in group '%s' (internal/external mix)." % (tree_item.name, group.name) ) elif self._layer_included(tree_item, version): if (tree_item.name in layers): if (catalogue or group.is_internal_wms == self._is_internal_wms(tree_item)): l, l_errors = self._layer(tree_item, **kwargs) errors |= l_errors if depth < min_levels: errors.add("The Layer '%s' is under indented (%i/%i)." % ( path + "/" + tree_item.name, depth, min_levels )) else: children.append(l) else: errors.add( "Layer '%s' cannot be in the group '%s' (internal/external mix)." % (tree_item.name, group.name) ) if len(children) > 0: g = { 'id': group.id, 'name': group.name, 'children': children, 'metadata': {}, } if version == 1: g.update({ 'isExpanded': group.is_expanded, 'isInternalWMS': group.is_internal_wms, 'isBaseLayer': group.is_base_layer, }) for metadata in group.ui_metadata: g['metadata'][metadata.name] = get_url(metadata.value, self.request, errors=errors) if version == 1 and group.metadata_url: g['metadataURL'] = group.metadata_url return g, errors else: return None, errors