Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #261] Fix remote serializer and add comments
Browse files Browse the repository at this point in the history
mattiagiupponi committed Jul 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 35c7c93 commit be77125
Showing 4 changed files with 58 additions and 30 deletions.
4 changes: 3 additions & 1 deletion importer/handlers/base.py
Original file line number Diff line number Diff line change
@@ -326,13 +326,15 @@ def rollback(
def _create_geonode_resource_rollback(
self, exec_id, istance_name=None, *args, **kwargs
):
from importer.orchestrator import orchestrator
"""
The handler will remove the resource from geonode
"""
logger.info(
f"Rollback geonode step in progress for execid: {exec_id} resource created was: {istance_name}"
)
resource = ResourceBase.objects.filter(alternate__icontains=istance_name)
_exec_obj = orchestrator.get_execution_object(exec_id)
resource = ResourceBase.objects.filter(alternate__icontains=istance_name, owner=_exec_obj.user)
if resource.exists():
resource.delete()

2 changes: 1 addition & 1 deletion importer/handlers/common/remote.py
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ def import_resource(self, files: dict, execution_id: str, **kwargs) -> str:
try:
params = _exec.input_params.copy()
url = params.get("url")
title = params.get("title", os.path.basename(urlparse(url).path))
title = params.get("title", None) or os.path.basename(urlparse(url).path)

# start looping on the layers available
layer_name = self.fixup_name(title)
13 changes: 10 additions & 3 deletions importer/handlers/common/serializer.py
Original file line number Diff line number Diff line change
@@ -15,7 +15,14 @@ class Meta:
"source",
)

url = serializers.URLField(required=True)
title = serializers.CharField(required=False)
type = serializers.CharField(required=True)
url = serializers.URLField(
required=True, help_text="URL of the remote service / resource"
)
title = serializers.CharField(
required=True, help_text="Title of the resource. Can be None or Empty"
)
type = serializers.CharField(
required=True,
help_text="Remote resource type, for example wms or 3dtiles. Is used by the handler to understand if can handle the resource",
)
source = serializers.CharField(required=False, default="upload")
69 changes: 44 additions & 25 deletions importer/handlers/remote/wms.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
from importer.handlers.common.remote import BaseRemoteResourceHandler
from geonode.services import enumerations
from geonode.base.enumerations import SOURCE_TYPE_REMOTE
from geonode.base.models import Link
from geonode.resource.enumerator import ExecutionRequestAction as exa
from importer.handlers.remote.serializers.wms import RemoteWMSSerializer
from importer.orchestrator import orchestrator
@@ -19,6 +18,9 @@ class RemoteWMSResourceHandler(BaseRemoteResourceHandler):

@staticmethod
def has_serializer(data) -> bool:
"""
Return the custom serializer for the WMS
"""
if "url" in data and enumerations.WMS in data.get("type", "").upper():
return RemoteWMSSerializer
return False
@@ -52,23 +54,38 @@ def extract_params_from_data(_data, action=None):
return payload, original_data

def prepare_import(self, files, execution_id, **kwargs):
"""
If the title and bbox must be retrieved from the remote resource
we take them before starting the import, so we can keep the default
workflow behaviour
"""
_exec = orchestrator.get_execution_object(exec_id=execution_id)
cleaned_url, _, _, _ = WmsServiceHandler.get_cleaned_url_params(
_exec.input_params.get("url")
)
parsed_url = f"{cleaned_url.scheme}://{cleaned_url.netloc}{cleaned_url.path}"
ows_url = f"{parsed_url}?{cleaned_url.query}"
to_update = {"ows_url": ows_url, "parsed_url": parsed_url}
to_update = {
"ows_url": ows_url,
"parsed_url": parsed_url,
"remote_resource_id": _exec.input_params.get("lookup", None),
}
if _exec.input_params.get("parse_remote_metadata", False):
_, wms = WebMapService(_exec.input_params.get("url"))
wms_resource = wms[_exec.input_params.get("lookup")]
to_update.update(
{
"title": wms_resource.title,
"bbox": wms_resource.boundingBoxWGS84,
"remote_resource_id": _exec.input_params.get("lookup", None),
}
)
try:
_, wms = WebMapService(_exec.input_params.get("url"))
wms_resource = wms[_exec.input_params.get("lookup")]
to_update.update(
{
"title": wms_resource.title,
"bbox": wms_resource.boundingBoxWGS84,
}
)
except Exception as e:
logger.error(
f"Error during the fetch of the WSM details, please check the log {e}"
)
raise e

_exec.input_params.update(to_update)
_exec.save()

@@ -81,6 +98,10 @@ def generate_alternate(
user_datasets,
dataset_exists,
):
"""
For WMS we dont want to generate an alternate, otherwise we cannot use
the alternate to lookup the layer in the remote service
"""
return layer_name, payload_alternate

def create_geonode_resource(
@@ -91,18 +112,27 @@ def create_geonode_resource(
resource_type: Dataset = ...,
asset=None,
):
"""
Use the default RemoteResourceHandler to create the geonode resource
after that, we assign the bbox and re-generate the thumbnail
"""
resource = super().create_geonode_resource(
layer_name, alternate, execution_id, Dataset, asset
)
_exec = orchestrator.get_execution_object(execution_id)
bbox = _exec.input_params.get("bbox")
resource.set_bbox_polygon(bbox, "EPSG:4326")
resource_manager.set_thumbnail(None, instance=resource)
remote_bbox = _exec.input_params.get("bbox")
if remote_bbox:
resource.set_bbox_polygon(remote_bbox, "EPSG:4326")
resource_manager.set_thumbnail(None, instance=resource)
return resource

def generate_resource_payload(
self, layer_name, alternate, asset, _exec, workspace, **kwargs
):
"""
Here are returned all the information required to generate the geonode resource
inclusing the OWS url
"""
return dict(
resource_type="dataset",
subtype="remote",
@@ -121,14 +151,3 @@ def generate_resource_payload(
ptype="gxp_wmscsource",
ows_url=_exec.input_params.get("ows_url"),
)

def create_link(self, resource, params: dict, name):
link = Link(
resource=resource,
extension="text/html",
url=params.get("ows_url"),
link_type="OGC:WMS",
name="OGC WMS: remoteWorkspace Service",
)
link.save()
return resource

0 comments on commit be77125

Please sign in to comment.