Skip to content

Commit

Permalink
Merge pull request #74 from Samweli/site_layer_report_fix
Browse files Browse the repository at this point in the history
Fix for site layer not showing up in the report
  • Loading branch information
Samweli committed Aug 15, 2024
2 parents fdd8235 + da9e1b9 commit 6902508
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
18 changes: 12 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
{
"general": {
"name" : "QGIS GEA afforestation tool",
"qgisMinimumVersion": 3.20,
"name": "QGIS GEA afforestation tool",
"qgisMinimumVersion": 3.2,
"qgisMaximumVersion": 3.99,
"icon": "icon.png",
"experimental": false,
"deprecated": false,
"homepage": "https://github.com/kartoza/qgis-gea-plugin",
"tracker": "https://github.com/kartoza/qgis-gea-plugin/issues",
"repository": "https://github.com/kartoza/qgis-gea-plugin",
"tags": ["gea", "maps", "afforestation"],
"category": ["plugins"],
"tags": [
"gea",
"maps",
"afforestation"
],
"category": [
"plugins"
],
"hasProcessingProvider": "no",
"about": "Adds functionality inside QGIS to enable GEA afforestation visualization and analysis.",
"author": "Kartoza",
"email": "[email protected]",
"description": "View, browse and navigate through imagery.",
"version": "0.0.1",
"version": "0.0.18dev",
"changelog": ""
}
}
}
15 changes: 11 additions & 4 deletions src/qgis_gea_plugin/gui/qgis_gea.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,14 @@ def start_drawing(self):
area_name = self._get_area_name()

unique_area_name = f"{area_name}_{str(uuid.uuid4())[:4]}"
layer_name = clean_filename(unique_area_name)

self.drawing_layer_path = f"{os.path.join(sites_path, clean_filename(unique_area_name))}.shp"
self.drawing_layer_path = f"{os.path.join(sites_path, layer_name)}.shp"

# Create a new layer with multipolygon geometry
self.drawing_layer = QgsVectorLayer(
f"MultiPolygon?crs={crs_id}",
unique_area_name,
layer_name,
"memory"
)

Expand Down Expand Up @@ -685,15 +686,17 @@ def save_area(self):
folder_path = self.project_folder.filePath()
sites_path = os.path.join(folder_path, 'sites')

layer_path = f"{os.path.join(sites_path, clean_filename(area_name))}.shp"
layer_name = clean_filename(area_name)

layer_path = f"{os.path.join(sites_path, layer_name)}.shp"

error, error_message = QgsVectorFileWriter.writeAsVectorFormatV2(
self.drawing_layer, layer_path, transform_context, options
)
if error == QgsVectorFileWriter.NoError:
saved_layer = QgsVectorLayer(
layer_path,
area_name,
layer_name,
"ogr"
)

Expand Down Expand Up @@ -809,6 +812,10 @@ def get_site_layer(self) -> typing.Optional[QgsVectorLayer]:

if (parent_group is not None and
parent_group.name() == SITE_GROUP_NAME):
settings_manager.set_value(
Settings.LAST_SITE_LAYER_PATH,
selected_layer.dataProvider().dataSourceUri()
)
return selected_layer

sites_layer_path = settings_manager.get_value(
Expand Down
4 changes: 2 additions & 2 deletions src/qgis_gea_plugin/gui/report_progress_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ def _on_report_error(self):
self._set_close_state()
tr_msg = tr(
f"Error occurred during report generation. "
f"{self._task.error_messages}"
"\nSee logs for more information"
)
self.lbl_message.setText(tr_msg)

log(tr(f"Error generating report, {self._task.error_messages} \n"))
log(tr(f"Error generating report, {self._task._error_messages} \n"))

log(tr(f"{self._task._result.errors}")) if self._task._result else None

Expand All @@ -126,6 +125,7 @@ def _on_open_pdf(self):
"""Slot raised to show PDF report if report generation process
was successful.
"""
log("Opening pdf")
if self.report_result is None:
log(
tr("Output from the report generation process could not be determined.")
Expand Down
56 changes: 22 additions & 34 deletions src/qgis_gea_plugin/lib/reports/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from qgis.PyQt import QtCore, QtXml

from ...conf import settings_manager, Settings
from ...definitions.defaults import (
ADMIN_AREAS_GROUP_NAME,
DETAILED_ZOOM_OUT_FACTOR,
Expand Down Expand Up @@ -380,6 +381,7 @@ def _get_layer_from_node_name(

if matching_node is None:
tr_msg = tr("layer node not found.")
log(f"{tr_msg}, node name {node_name}")
self._error_messages.append(f"{node_name} {tr_msg}")
return None

Expand Down Expand Up @@ -407,53 +409,39 @@ def _set_site_layer(self):
"""Fetch the site boundary layer saved in the project's
'sites' boundary folder.
"""
site_path = f"{self._context.project_dir}/sites/" \
f"{clean_filename(self._metadata.area_name)}.shp"
site_path = settings_manager.get_value(Settings.LAST_SITE_LAYER_PATH, default="")

if not os.access(site_path, os.R_OK):
tr_msg = tr(
"Current user does not have permission to read the "
"site boundary shapefile"
)
self._error_messages.append(tr_msg)
return

p = Path(site_path)
if not p.exists():
path = Path(site_path)
if not path.exists():
tr_msg = tr("Site boundary shapefile does not exist")
log(tr_msg)
self._error_messages.append(f"{tr_msg} {site_path}")
return

site_layer = QgsVectorLayer(site_path, "Site Boundary", "ogr")
site_layer = self.find_layer_by_name(path.stem)

if site_layer is None:
return

if not site_layer.isValid():
tr_msg = tr("Site boundary shapefile is invalid")
log(tr_msg)
self._error_messages.append(tr_msg)
return

# We need to update the data source of the site layer in the
# project since it was only saved as a memory layer.
project_site_layer = self._get_layer_from_node_name(
self._metadata.area_name,
LayerNodeSearch.EXACT_MATCH,
SITE_GROUP_NAME
)
if project_site_layer is None:
tr_msg = tr("Reference site layer not found in the project.")
self._error_messages.append(tr_msg)
return
site_symbol = QgsFillSymbol.createSimple(REPORT_SITE_BOUNDARY_STYLE)
site_layer.renderer().setSymbol(site_symbol)
site_layer.triggerRepaint()

project_site_layer.setDataSource(
site_layer.source(),
project_site_layer.name(),
site_layer.providerType(),
site_layer.dataProvider().ProviderOptions()
)
self._site_layer = site_layer

site_symbol = QgsFillSymbol.createSimple(REPORT_SITE_BOUNDARY_STYLE)
project_site_layer.renderer().setSymbol(site_symbol)
project_site_layer.triggerRepaint()
def find_layer_by_name(self, layer_name):
layers = QgsProject.instance().mapLayers()

self._site_layer = project_site_layer
for layer_id, layer in layers.items():
if clean_filename(layer.name()) == layer_name:
return layer
return None

def _set_landscape_layer(self):
"""Set the landscape layer i.e. Nicfi or Landsat depending on the
Expand Down

0 comments on commit 6902508

Please sign in to comment.