def test_missing_elements(self): """Test if we can get missing elements correctly.""" # Copy the inasafe template to temp dir template_path = os.path.join( temp_dir('test'), 'inasafe-portrait-a4.qpt') shutil.copy2(INASAFE_TEMPLATE_PATH, template_path) template_composition = TemplateComposition(template_path=template_path) # No missing elements here component_ids = ['safe-logo', 'north-arrow', 'organisation-logo', 'impact-map', 'impact-legend'] template_composition.component_ids = component_ids message = 'There should be no missing elements, but it gets: %s' % ( template_composition.missing_elements) expected_result = [] self.assertEqual( template_composition.missing_elements, expected_result, message) # There are missing elements component_ids = ['safe-logo', 'north-arrow', 'organisation-logo', 'impact-map', 'impact-legend', 'i-added-element-id-here-nooo'] template_composition.component_ids = component_ids message = 'There should be no missing elements, but it gets: %s' % ( template_composition.missing_elements) expected_result = ['i-added-element-id-here-nooo'] self.assertEqual( template_composition.missing_elements, expected_result, message) # Remove test dir shutil.rmtree(temp_dir('test'))
def test_missing_elements(self): """Test if we can get missing elements correctly.""" # Copy the inasafe template to temp dir template_path = os.path.join(temp_dir('test'), 'a4-portrait-blue.qpt') shutil.copy2(INASAFE_TEMPLATE_PATH, template_path) template_composition = TemplateComposition(template_path=template_path) # No missing elements here component_ids = [ 'white-inasafe-logo', 'north-arrow', 'organisation-logo', 'impact-map', 'impact-legend' ] template_composition.component_ids = component_ids message = 'There should be no missing elements, but it gets: %s' % ( template_composition.missing_elements) expected_result = [] self.assertEqual(template_composition.missing_elements, expected_result, message) # There are missing elements component_ids = [ 'white-inasafe-logo', 'north-arrow', 'organisation-logo', 'impact-map', 'impact-legend', 'i-added-element-id-here-nooo' ] template_composition.component_ids = component_ids message = 'There should be no missing elements, but it gets: %s' % ( template_composition.missing_elements) expected_result = ['i-added-element-id-here-nooo'] self.assertEqual(template_composition.missing_elements, expected_result, message) # Remove test dir shutil.rmtree(temp_dir('test'))
def load_template(self, map_settings): """Load composer template for merged report. Validate it as well. The template needs to have: 1. QgsComposerMap with id 'impact-map' for merged impact map. 2. QgsComposerPicture with id 'safe-logo' for InaSAFE logo. 3. QgsComposerLabel with id 'summary-report' for a summary of two impacts. 4. QgsComposerLabel with id 'aggregation-area' to indicate the area of aggregation. 5. QgsComposerScaleBar with id 'map-scale' for impact map scale. 6. QgsComposerLegend with id 'map-legend' for impact map legend. 7. QgsComposerPicture with id 'organisation-logo' for organisation logo. 8. QgsComposerLegend with id 'impact-legend' for map legend. 9. QgsComposerHTML with id 'merged-report-table' for the merged report. :param map_settings: Map settings. :type map_settings: QgsMapSettings, QgsMapRenderer """ # Create Composition template_composition = TemplateComposition( self.template_path, map_settings) # Validate the component in the template component_ids = ['impact-map', 'safe-logo', 'summary-report', 'aggregation-area', 'map-scale', 'map-legend', 'organisation-logo', 'merged-report-table'] template_composition.component_ids = component_ids if len(template_composition.missing_elements) > 0: raise ReportCreationError(self.tr( 'Components: %s could not be found' % ', '.join( template_composition.missing_elements))) # Prepare map substitution and set to composition impact_title = '%s and %s' % ( self.first_impact['map_title'], self.second_impact['map_title']) substitution_map = { 'impact-title': impact_title, 'hazard-title': self.first_impact['hazard_title'], 'disclaimer': self.disclaimer } template_composition.substitution = substitution_map # Load Template try: template_composition.load_template() except TemplateLoadingError: raise # Draw Composition # Set InaSAFE logo composition = template_composition.composition safe_logo = composition.getComposerItemById('safe-logo') safe_logo.setPictureFile(self.safe_logo_path) # set organisation logo org_logo = composition.getComposerItemById('organisation-logo') org_logo.setPictureFile(self.organisation_logo_path) # Set Map Legend legend = composition.getComposerItemById('map-legend') if qgis_version() < 20400: layers = map_settings.layerSet() else: layers = map_settings.layers() if qgis_version() < 20600: legend.model().setLayerSet(layers) legend.synchronizeWithModel() else: root_group = legend.modelV2().rootGroup() layer_ids = map_settings.layers() for layer_id in layer_ids: # noinspection PyUnresolvedReferences layer = QgsMapLayerRegistry.instance().mapLayer(layer_id) root_group.addLayer(layer) legend.synchronizeWithModel() return composition
def load_template(self, map_settings): """Load composer template for merged report. Validate it as well. The template needs to have: 1. QgsComposerMap with id 'impact-map' for merged impact map. 2. QgsComposerPicture with id 'safe-logo' for InaSAFE logo. 3. QgsComposerLabel with id 'summary-report' for a summary of two impacts. 4. QgsComposerLabel with id 'aggregation-area' to indicate the area of aggregation. 5. QgsComposerScaleBar with id 'map-scale' for impact map scale. 6. QgsComposerLegend with id 'map-legend' for impact map legend. 7. QgsComposerPicture with id 'organisation-logo' for organisation logo. 8. QgsComposerLegend with id 'impact-legend' for map legend. 9. QgsComposerHTML with id 'merged-report-table' for the merged report. :param map_settings: Map settings. :type map_settings: QgsMapSettings, QgsMapRenderer """ # Create Composition template_composition = TemplateComposition(self.template_path, map_settings) # Validate the component in the template component_ids = [ 'impact-map', 'safe-logo', 'summary-report', 'aggregation-area', 'map-scale', 'map-legend', 'organisation-logo', 'merged-report-table' ] template_composition.component_ids = component_ids if len(template_composition.missing_elements) > 0: raise ReportCreationError( self.tr('Components: %s could not be found' % ', '.join(template_composition.missing_elements))) # Prepare map substitution and set to composition impact_title = '%s and %s' % (self.first_impact['map_title'], self.second_impact['map_title']) substitution_map = { 'impact-title': impact_title, 'hazard-title': self.first_impact['hazard_title'], 'disclaimer': self.disclaimer } template_composition.substitution = substitution_map # Load Template try: template_composition.load_template() except TemplateLoadingError: raise # Draw Composition # Set InaSAFE logo composition = template_composition.composition safe_logo = composition.getComposerItemById('safe-logo') safe_logo.setPictureFile(self.safe_logo_path) # set organisation logo org_logo = composition.getComposerItemById('organisation-logo') org_logo.setPictureFile(self.organisation_logo_path) # Set Map Legend legend = composition.getComposerItemById('map-legend') if qgis_version() < 20400: layers = map_settings.layerSet() else: layers = map_settings.layers() if qgis_version() < 20600: legend.model().setLayerSet(layers) legend.synchronizeWithModel() else: root_group = legend.modelV2().rootGroup() layer_ids = map_settings.layers() for layer_id in layer_ids: # noinspection PyUnresolvedReferences layer = QgsMapLayerRegistry.instance().mapLayer(layer_id) root_group.addLayer(layer) legend.synchronizeWithModel() return composition