Skip to content

Commit

Permalink
Fix missing vis params causing error
Browse files Browse the repository at this point in the history
Refactor to limit to image and not image collections
  • Loading branch information
zacdezgeo committed Feb 12, 2025
1 parent 7538ab8 commit b3f65a3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
1 change: 0 additions & 1 deletion ee_plugin/ee_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def initGui(self):
triggered=self._run_cmd_set_cloud_project,
)

# TODO: how is iface passed here?
add_gee_layer_action = QtWidgets.QAction(
icon=icon("add-layer.svg"),
text=self.tr("Add GEE Dataset"),
Expand Down
40 changes: 20 additions & 20 deletions ee_plugin/ui/form_add_ee_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ def add_gee_layer_dialog(iface: gui.QgisInterface):
"""Display a dialog to add a GEE dataset to the QGIS map."""

dialog = build_vbox_dialog(
windowTitle="Add Google Earth Engine Layer",
windowTitle="Add Google Earth Engine Image",
widgets=[
build_form_group_box(
title="Dataset",
rows=[
(
QtWidgets.QLabel(
text="Enter GEE Dataset Name (e.g., COPERNICUS/S2, USGS/SRTMGL1_003)",
toolTip="Provide the full Earth Engine dataset ID.",
text="Enter GEE Image Name (e.g., COPERNICUS/S2, USGS/SRTMGL1_003)",
toolTip="Provide the full Earth Engine ID.",
),
QtWidgets.QLineEdit(objectName="datasetId"),
QtWidgets.QLineEdit(objectName="imageId"),
)
],
),
Expand Down Expand Up @@ -62,38 +62,38 @@ def add_gee_layer_dialog(iface: gui.QgisInterface):
def _load_gee_layer(dialog: Dict[str, QtWidgets.QWidget]):
"""Fetch and add the selected Earth Engine dataset to the map with user-defined visualization parameters."""
values = get_values(dialog)
dataset_id = values["datasetId"]
image_id = values["imageId"]

if not dataset_id:
message = "Dataset ID is required."
if not image_id:
message = "Image ID is required."
QgsMessageLog.logMessage(message, "GEE Plugin", level=Qgis.Critical)
return

try:
# Get asset metadata
asset_info = ee.data.getAsset(dataset_id)
asset_info = ee.data.getAsset(image_id)
asset_type = asset_info.get("type", "")

# Determine if it's an ImageCollection or Image
if asset_type == "IMAGE_COLLECTION":
ee_object = ee.ImageCollection(dataset_id).mosaic()
elif asset_type == "IMAGE":
ee_object = ee.Image(dataset_id)
if asset_type == "IMAGE":
ee_object = ee.Image(image_id)
else:
raise ValueError(f"Unsupported asset type: {asset_type}")

# Get visualization parameters from user as JSON
vis_params_input = values.get("vizParams", "{}")

try:
vis_params = json.loads(vis_params_input.replace("'", '"'))
except json.JSONDecodeError:
raise ValueError("Invalid JSON format in visualization parameters.")
if not vis_params_input:
vis_params = {}
else:
try:
vis_params = json.loads(vis_params_input.replace("'", '"'))
except json.JSONDecodeError:
raise ValueError("Invalid JSON format in visualization parameters.")

# Add the dataset to QGIS map
addLayer(ee_object, vis_params, dataset_id)
addLayer(ee_object, vis_params, image_id)

success_message = (
f"Successfully added {dataset_id} to the map with custom visualization."
f"Successfully added {image_id} to the map with custom visualization."
)
QgsMessageLog.logMessage(success_message, "GEE Plugin", level=Qgis.Success)

Expand Down
25 changes: 19 additions & 6 deletions test/test_form_add_ee_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@

def test_add_gee_layer_dialog(qgis_iface_clean):
dialog = add_gee_layer_dialog(qgis_iface_clean)
dialog.findChild(QtWidgets.QLineEdit, "datasetId").setText("COPERNICUS/S2")
dialog.findChild(QtWidgets.QLineEdit, "imageId").setText("COPERNICUS/S2")

dialog.findChild(QtWidgets.QTextEdit, "vizParams").setText(
'{"min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00"]}'
)

assert get_values(dialog) == {
"datasetId": "COPERNICUS/S2",
"imageId": "COPERNICUS/S2",
"vizParams": '{"min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00"]}',
}


def test_load_gee_layer_srtm(qgis_iface_clean):
dialog = add_gee_layer_dialog(qgis_iface_clean)
dialog.findChild(QtWidgets.QLineEdit, "datasetId").setText("USGS/SRTMGL1_003")
dialog.findChild(QtWidgets.QLineEdit, "imageId").setText("USGS/SRTMGL1_003")

dialog.findChild(QtWidgets.QTextEdit, "vizParams").setText(
'{"min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00"]}'
)

assert get_values(dialog) == {
"datasetId": "USGS/SRTMGL1_003",
"imageId": "USGS/SRTMGL1_003",
"vizParams": '{"min": 0, "max": 4000, "palette": ["006633", "E5FFCC", "662A00"]}',
}

Expand All @@ -40,7 +40,7 @@ def test_load_gee_layer_srtm(qgis_iface_clean):

def test_converting_viz_params_json(qgis_iface_clean):
dialog = add_gee_layer_dialog(qgis_iface_clean)
dialog.findChild(QtWidgets.QLineEdit, "datasetId").setText("USGS/SRTMGL1_003")
dialog.findChild(QtWidgets.QLineEdit, "imageId").setText("USGS/SRTMGL1_003")

# single quotes should get replaced to double quotes
# by _load_gee_layer, so dialog still has single quotes
Expand All @@ -57,7 +57,7 @@ def test_converting_viz_params_json(qgis_iface_clean):

def test_invalid_vis_params(qgis_iface_clean):
dialog = add_gee_layer_dialog(qgis_iface_clean)
dialog.findChild(QtWidgets.QLineEdit, "datasetId").setText("USGS/SRTMGL1_003")
dialog.findChild(QtWidgets.QLineEdit, "imageId").setText("USGS/SRTMGL1_003")

dialog.findChild(QtWidgets.QTextEdit, "vizParams").setText(
"not a valid JSON string"
Expand All @@ -66,3 +66,16 @@ def test_invalid_vis_params(qgis_iface_clean):
_load_gee_layer(dialog)

assert len(qgis_iface_clean.mapCanvas().layers()) == 0


def test_empty_vis_params(qgis_iface_clean):
dialog = add_gee_layer_dialog(qgis_iface_clean)
dialog.findChild(QtWidgets.QLineEdit, "imageId").setText("USGS/SRTMGL1_003")

dialog.findChild(QtWidgets.QTextEdit, "vizParams").setText("")

_load_gee_layer(dialog)

assert len(qgis_iface_clean.mapCanvas().layers()) == 1
assert qgis_iface_clean.mapCanvas().layers()[0].name() == "USGS/SRTMGL1_003"
assert qgis_iface_clean.mapCanvas().layers()[0].dataProvider().name() == "EE"

0 comments on commit b3f65a3

Please sign in to comment.