def layer_summary(layer_id: str, layer: QgsMapLayer): return dict( id=layer_id, name=layer.name(), source=layer.publicSource(), crs=layer.crs().userFriendlyIdentifier(), valid=layer.isValid(), spatial=layer.isSpatial(), )
def add_layer_metadata(map_layer: qgc.QgsMapLayer, layer_cfg: Layer) -> None: """Add layer metadata. Renders a jinja template to a temporary file location as a valid QGIS qmd metadata file. This metadata then gets associated with the `map_layer` using its `loadNamedMetadata` method. This metadata gets written to the project file when the layer is added to the `project`. """ qmd_template = load_template('metadata.jinja') # Set the layer's tooltip tooltip = _build_layer_tooltip(layer_cfg) map_layer.setAbstract(tooltip) # Render the qmd template. abstract = escape(build_layer_metadata(layer_cfg)) layer_extent = map_layer.extent() layer_crs = map_layer.crs() if layer_cfg.steps: provenance_list = [escape(step.provenance) for step in layer_cfg.steps] else: provenance_list = [] rendered_qmd = qmd_template.render( provenance_list=provenance_list, abstract=abstract, title=layer_cfg.title, crs_proj4_str=layer_crs.toProj4(), crs_srsid=layer_crs.srsid(), crs_postgres_srid=layer_crs.postgisSrid(), crs_authid=layer_crs.authid(), crs_description=layer_crs.description(), crs_projection_acronym=layer_crs.projectionAcronym(), crs_ellipsoid_acronym=layer_crs.ellipsoidAcronym(), minx=layer_extent.xMinimum(), miny=layer_extent.yMinimum(), maxx=layer_extent.xMaximum(), maxy=layer_extent.yMaximum(), ) # Write the rendered tempalte to a temporary file # location. `map_layer.loadNamedMetadata` expects a string URI corresponding # to a file on disk. with tempfile.NamedTemporaryFile('w') as temp_file: temp_file.write(rendered_qmd) temp_file.flush() map_layer.loadNamedMetadata(temp_file.name)