Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WCPS ProcessCoverages implementation #778

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/wcps-processcoverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Very simple script demonstrating the use ProcessCoverages request making use WCPS.
# ---
#
# The equivalent ProcessCoverage request that is equivalent ot hte example is:
# https://code-de.rasdaman.com/rasdaman/ows?service=WCS&version=2.0.1&request=ProcessCoverages&query=for%20%24c%20in%20(S2_L2A_32631_B01_60m)%20%0Areturn%0A%20%20encode(%0A%20%20%20%20(%200.20%20*%20(%201050.0%20%2B%20(%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(float)%20%24c%5B%20ansi(%20%222017-04-03%22%20)%20%5D%20-%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24c%5B%20ansi(%20%222017-04-10%22%20)%20%5D%20)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%0A%20%20%20%20)%5B%20E(%20660000%3A690000%20)%2C%20N(%205090220%3A5115220%20)%20%5D%0A%20%20%20%2C%20%22image%2Fjpeg%22)%0A%27%27%27
# ---
#
# Example to find the equivalent information using OWSLib is as below:
#

from owslib.wcs import WebCoverageService
import numpy as np
from io import BytesIO
from PIL import Image as im

my_wcs = WebCoverageService('https://code-de.rasdaman.com/rasdaman/ows', version='2.0.1')

#query performs time-series processing by calculating, in the Sentinel-2 datacube,
#the difference between two timeslices of the B1 band. The result is numerically
#adjusted so that it fits into the range of [0, 255] for encoding in JPEG
query = '''
for $c in (S2_L2A_32631_B01_60m)
return
encode(
( 0.20 * ( 1050.0 + (
(float) $c[ ansi( "2017-04-03" ) ] -
$c[ ansi( "2017-04-10" ) ] )
)
)[ E( 660000:690000 ), N( 5090220:5115220 ) ]
, "image/jpeg")
'''


response = my_wcs.process(query)

#bytes -> BytesIO -> PIL.Image -> np.array
img_arr = np.array(im.open(BytesIO(response)))

#image being extracted from image array
data = im.fromarray(img_arr)

#showing image
data.show()

#saving image as a jpeg file
data.save('test.jpeg')
49 changes: 49 additions & 0 deletions owslib/coverage/wcps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from urllib.parse import quote, urlencode
itsayeshanaeem marked this conversation as resolved.
Show resolved Hide resolved
from owslib.util import Authentication, openURL

class ProcessCoverages(object):
itsayeshanaeem marked this conversation as resolved.
Show resolved Hide resolved
"""Sends the encoded WCPS query to WCS server by using "ProcessCoverage" operation
"""

def __init__(self, version=None, cookies=None, auth=None, headers=None):

self.version = version
self._infoset = None
self.cookies = cookies
self.headers = headers
self.auth = auth or Authentication()

#get/kvp encoding implementation
def getKVPUrl(self, service_url, query):
"""Return a process coverage url encoded in get/kvp
@type service_url: string
@param service_url: base url of WCS service
@rtype: string
@return: processCoverage URL
"""
url = []
url.append(('service', 'WCS'))
url.append(('request', 'ProcessCoverages'))
url.append(('version', '2.0'))
url.append(('query', ""))

urlqs = urlencode(tuple(url))

return service_url.split('?')[0] + '?' + urlqs + query


def process(self, service_url, query):
"""Get and process a WCPS query result, returning the
result in encode() format

@type service_url: string
@param service_url: The base url, to which is appended the service,
version, and request parameters
@param query: WCPS that needs to be processed
@rtype: response content
@return: encoded coverage
"""
query = quote(query) # encoding the query parameter
request = ProcessCoverages.getKVPUrl(self, service_url=service_url, query=query)
u = openURL(request, timeout=self.timeout, cookies=self.cookies, auth=self.auth, headers=self.headers)
return u.read()
6 changes: 5 additions & 1 deletion owslib/coverage/wcs201.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
ServiceProvider,
OperationsMetadata,
)

from owslib.coverage.wcps import ProcessCoverages
from urllib.parse import urlencode
from owslib.util import openURL, testXMLValue
from owslib.etree import etree
Expand Down Expand Up @@ -226,6 +226,10 @@ def getOperationByName(self, name):
return item
raise KeyError("No operation named %s" % name)

#WCS ProcessCoverages implementation
def process(self,query):
result = ProcessCoverages.process(self, service_url=self.url, query=query)
return result

class ContentMetadata(object):
"""
Expand Down