-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #261] Add remote WMS importer handler
- Loading branch information
1 parent
205bc83
commit 35c7c93
Showing
6 changed files
with
222 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from rest_framework import serializers | ||
from importer.handlers.common.serializer import RemoteResourceSerializer | ||
|
||
|
||
class RemoteWMSSerializer(RemoteResourceSerializer): | ||
class Meta: | ||
model = RemoteResourceSerializer.Meta.model | ||
ref_name = "RemoteWMSSerializer" | ||
fields = RemoteResourceSerializer.Meta.fields + ( | ||
"lookup", | ||
"bbox", | ||
"parse_remote_metadata", | ||
) | ||
|
||
lookup = serializers.CharField(required=True) | ||
bbox = serializers.ListField(required=False) | ||
parse_remote_metadata = serializers.BooleanField(required=False, default=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import logging | ||
from django.conf import settings | ||
from geonode.layers.models import Dataset | ||
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 | ||
from geonode.harvesting.harvesters.wms import WebMapService | ||
from geonode.services.serviceprocessors.wms import WmsServiceHandler | ||
from geonode.resource.manager import resource_manager | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RemoteWMSResourceHandler(BaseRemoteResourceHandler): | ||
|
||
@staticmethod | ||
def has_serializer(data) -> bool: | ||
if "url" in data and enumerations.WMS in data.get("type", "").upper(): | ||
return RemoteWMSSerializer | ||
return False | ||
|
||
@staticmethod | ||
def can_handle(_data) -> bool: | ||
""" | ||
This endpoint will return True or False if with the info provided | ||
the handler is able to handle the file or not | ||
""" | ||
if "url" in _data and enumerations.WMS.lower() in _data.get("type", "").lower(): | ||
return True | ||
return False | ||
|
||
@staticmethod | ||
def extract_params_from_data(_data, action=None): | ||
""" | ||
Remove from the _data the params that needs to save into the executionRequest object | ||
all the other are returned | ||
""" | ||
payload, original_data = BaseRemoteResourceHandler.extract_params_from_data( | ||
_data, action=action | ||
) | ||
if action != exa.COPY.value: | ||
payload["lookup"] = original_data.pop("lookup", None) | ||
payload["bbox"] = original_data.pop("bbox", None) | ||
payload["parse_remote_metadata"] = original_data.pop( | ||
"parse_remote_metadata", None | ||
) | ||
|
||
return payload, original_data | ||
|
||
def prepare_import(self, files, execution_id, **kwargs): | ||
_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} | ||
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), | ||
} | ||
) | ||
_exec.input_params.update(to_update) | ||
_exec.save() | ||
|
||
def generate_alternate( | ||
self, | ||
layer_name, | ||
execution_id, | ||
should_be_overwritten, | ||
payload_alternate, | ||
user_datasets, | ||
dataset_exists, | ||
): | ||
return layer_name, payload_alternate | ||
|
||
def create_geonode_resource( | ||
self, | ||
layer_name: str, | ||
alternate: str, | ||
execution_id: str, | ||
resource_type: Dataset = ..., | ||
asset=None, | ||
): | ||
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) | ||
return resource | ||
|
||
def generate_resource_payload( | ||
self, layer_name, alternate, asset, _exec, workspace, **kwargs | ||
): | ||
return dict( | ||
resource_type="dataset", | ||
subtype="remote", | ||
sourcetype=SOURCE_TYPE_REMOTE, | ||
alternate=alternate, | ||
dirty_state=True, | ||
title=kwargs.get("title", layer_name), | ||
name=alternate, | ||
owner=_exec.user, | ||
workspace=getattr(settings, "DEFAULT_WORKSPACE", "geonode"), | ||
store=_exec.input_params.get("parsed_url") | ||
.encode("utf-8", "ignore") | ||
.decode("utf-8") | ||
.replace(".", "") | ||
.replace("/", ""), | ||
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 |