Repository: hydroshare/hydroshare

Issue:
REST API Enhancements to return additional metadata (optionally) 
Azavea (@kdeloach) has requested that the REST API be enhanced to return spatial fields in discovery.  Specifically:

Our current objective is to replicate something like this:
https://beta.hydroshare.org/search/?q=water&NElat=&NElng=&SWlat=&SWlng=&start_date=&end_date=#map-view

We want to be able to search for resources, and show the results on a map. However, the DRF "resource" endpoint doesn't return enough information to do this.

Here's a sample request and response:
https://beta.hydroshare.org/hsapi/resource/?full_text_search=water&format=json

{
   "count":297,
   "next":"https://beta.hydroshare.org/hsapi/resource/?format=json&full_text_search=water&page=2",
   "previous":null,
   "results": [
      {
         "resource_type":"GenericResource",
         "resource_title":"GIS in Water Resources Term Project 2015",
         "resource_id":"0359cab7d93f403ba6ee8726cff74f8a",
         "creator":"Tomsen Reed",
         "date_created":"09-24-2015",
         "date_last_updated":"12-05-2015",
         "public":true,
         "discoverable":true,
         "shareable":true,
         "immutable":false,
         "published":false,
         "bag_url":"http://beta.hydroshare.org/django_irods/download/bags/0359cab7d93f403ba6ee8726cff74f8a.zip",
         "science_metadata_url":"http://beta.hydroshare.org/hsapi/resource/0359cab7d93f403ba6ee8726cff74f8a/scimeta/",
         "resource_map_url":"http://beta.hydroshare.org/hsapi/resource/0359cab7d93f403ba6ee8726cff74f8a/map/",
         "resource_url":"http://beta.hydroshare.org/resource/0359cab7d93f403ba6ee8726cff74f8a/"
      },
      ...
   ]
}

Since resources don't include lat/lng or bbox, we can't show these results on a map.

Besides adding spatial information, we are also requesting that a summary/abstract field be included in the results, to reach parity with other services that we are integrating with. 



Discussion:
This is a little conflated because it refers to the old 'search' page rather than the new 'discovery' endpoint. The REST API for discovery isn't ready, but we are open to adding this functionality to that. 

@alvacouch  What about returning coverage data in the existing GET endpoint ```/hsapi/resource```?
@alvacouch @pkdash Checking in on this issue (in order to support BiG CZ development) - do you have any updates on returning spatial fields with the discovery endpoint, or an estimate of when a HydroShare release will make this available? 
@alvacouch How should we address this issue? Are you planning to create a new REST endpoint for the discovery api/interface to address this issue? If not then I can modify what is returned for a GET request for endpoint ```hsapi/resource```.
Hi folks, I'm taking over from Kevin at Azavea for BiG-CZ development. The two things we're looking for in the `hsapi/resource` endpoint are:

 1. Filtering by bounding box. While the old search API seems to have support for that somewhat (evident in the `NElat`, `NElng` etc params in the sample query above), the REST API does not support these. It would be really great if we could give a bounding box filter to the API and get only contained results.
 2. Geometry in the results. While many result types (such as `RasterResource`) do have geometric information (see https://www.hydroshare.org/resource/5a992fbcb53544f789f7f53e0f2b4d63/), this is not accessible via the API at all. The other endpoints we are consuming provide geometry in their results and thus are plottable and comparable to each other. It would be wonderful if such geometry was available in the relevant results here too.

There was a recent update deployed to the Hydroshare API, but it does not seem to include either of these feature requests. Please keep us abreast of any progress thereof. Thanks.

/cc @aufdenkampe @emiliom
I just reviewed this with @pkdash.  He said Filtering by bounding box is done and in current deploy.  If there are shortcomings please respond.

Returning of geometry in the results is an enhancement not done (geometry can be obtained in a separate call, but not in the first return).  I'd like to prioritize this for the 1.12 HydroShare deploy.  @aphelionz please coordinate this.  @pkdash can help with coding if needed following August 1.
@pkdash Could you please point me to some documentation that specifies how the bounding box filter can be applied? I don't see anything instructions for that here: https://github.com/hydroshare/hydroshare/wiki/HydroShare-REST-API#implemented-rest-end-points.

Resolving patch:
diff --git a/hs_core/views/resource_rest_api.py b/hs_core/views/resource_rest_api.py
--- a/hs_core/views/resource_rest_api.py
+++ b/hs_core/views/resource_rest_api.py
@@ -43,6 +43,8 @@ def resourceToResourceListItem(self, r):
         science_metadata_url = site_url + reverse('get_update_science_metadata', args=[r.short_id])
         resource_map_url = site_url + reverse('get_resource_map', args=[r.short_id])
         resource_url = site_url + r.get_absolute_url()
+        coverages = [{"type": v['type'], "value": json.loads(v['_value'])}
+                     for v in r.metadata.coverages.values()]
         resource_list_item = serializers.ResourceListItem(resource_type=r.resource_type,
                                                           resource_id=r.short_id,
                                                           resource_title=r.metadata.title.value,
@@ -55,6 +57,7 @@ def resourceToResourceListItem(self, r):
                                                           date_created=r.created,
                                                           date_last_updated=r.updated,
                                                           bag_url=bag_url,
+                                                          coverages=coverages,
                                                           science_metadata_url=science_metadata_url,
                                                           resource_map_url=resource_map_url,
                                                           resource_url=resource_url)
diff --git a/hs_core/views/serializers.py b/hs_core/views/serializers.py
--- a/hs_core/views/serializers.py
+++ b/hs_core/views/serializers.py
@@ -99,6 +99,7 @@ class ResourceListItemSerializer(serializers.Serializer):
     public = serializers.BooleanField()
     discoverable = serializers.BooleanField()
     shareable = serializers.BooleanField()
+    coverages = serializers.JSONField(required=False)
     immutable = serializers.BooleanField()
     published = serializers.BooleanField()
     bag_url = serializers.URLField()
@@ -131,6 +132,7 @@ def __init__(self, resource_type):
                                'date_created',
                                'date_last_updated',
                                'bag_url',
+                               'coverages',
                                'science_metadata_url',
                                'resource_map_url',
                                'resource_url'])


Test patch: