#!/usr/bin/env python # coding=utf-8 # # import unittest import logging import os,sys from ConfigParser import ConfigParser import urllib import urlparse from owslib import crs OWSVIEWER_DIR=os.path.abspath(os.path.join(os.path.dirname(__file__),"..")) sys.path.append(OWSVIEWER_DIR) from OWS import * class TestOWS(unittest.TestCase): """Base class for OWS services (WFS, WCS) """ owsUrl = None config = None service = None def setUp(self): # set logging logging.basicConfig(level=logging.DEBUG) # mapfile for original services mapfile = os.path.join(OWSVIEWER_DIR,"tests","mapfiles","ows.map") self.config = ConfigParser.ConfigParser() self.config.read(os.path.join(OWSVIEWER_DIR,"tests","test.cfg")) self.owsUrl = self._getURLWithMap(mapfile) os.environ.update({"QUERY_STRING": "owsService=%s&owsUrl=%s"%(self.service,urllib.quote(self.owsUrl))}) self.service = getService() def test_bbox(self): """test if corect bounding box for layer will be used""" initExtent = [15, 50, 16, 51] class ContentsMetadata: boundingBoxWGS84 = initExtent id = "Temporary test layer" wcsLayer = ContentsMetadata() ows = OWS() extent = ows.getLayerExtent(wcsLayer,crs.Crs("EPSG:4326")) self.assertAlmostEqual(extent[0], initExtent[0]) self.assertAlmostEqual(extent[1], initExtent[1]) self.assertAlmostEqual(extent[2], initExtent[2]) self.assertAlmostEqual(extent[3], initExtent[3]) extent = ows.getLayerExtent(wcsLayer,crs.Crs("EPSG:32633")) self.assertAlmostEqual(extent[0], 500000.00, 0) self.assertAlmostEqual(extent[1], 5538630.70, 0) self.assertAlmostEqual(extent[2], 570168.86, 0) self.assertAlmostEqual(extent[3], 5650300.79, 0) def _getURLWithMap(self,mapfile): # create URL which will produce WCS and WFS services for testing # purposes owsUrl = urlparse.urlparse(self.config.get("OWSServer","owsserver")) owsParams = urlparse.parse_qs(owsUrl[4]) owsParams["map"] = mapfile return urlparse.urlunparse((owsUrl[0],owsUrl[1],owsUrl[2], owsUrl[3], urllib.unquote(urllib.urlencode(owsParams,True)),owsUrl[5])) def _getWMSCapabilities(self): # download the WMS Capabilities url = self._getURLWithMap(self.service.mapfilename) url = urlparse.urlparse(url) params = urlparse.parse_qs(url[4]) params["REQUEST"] = "GetCapabilities" params["SERVICE"] = "WMS" params = urllib.unquote(urllib.urlencode(params,True)) attrs = (url[0],url[1],url[2],url[3],params,url[5]) logging.debug("GetCapabilities URL: " + urlparse.urlunparse(attrs)) resp = objectify.parse(urllib.urlopen(urlparse.urlunparse(attrs))) return resp.getroot()