Ignore:
Timestamp:
16/04/08 16:44:07 (12 years ago)
Author:
spascoe
Message:
 
Location:
TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/pylons/ows_controller.py

    r3778 r3786  
    7979        # All OWS parameter names are case insensitive. 
    8080        self._owsParams = {} 
    81         #print request 
     81        print request 
    8282        #if request.has_key('REQUEST'): 
    8383        #print request['REQUEST'] 
     
    8989            else: 
    9090                self._owsParams[k.lower()] = request.params[k] 
     91        if 'info_format' not in request.params: 
     92                self._owsParams['info_format'] = 'text/html' 
    9193            #print [k, request.params[k]] 
    9294 
  • TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/pylons/wms_controller.py

    r3778 r3786  
    6363        print "loading layers" 
    6464        #print self.layers 
    65         self.layers = self.layerMapper.map(**kwargs) 
     65        self.layers = self.layerMapper.map() 
    6666 
    6767    #------------------------------------------------------------------------- 
     
    153153        # This plays nicer with mapClient. 
    154154        if ',' in layerName: 
    155             layerName = layerName.split(',')[0] 
    156             #raise InvalidParameterValue( 
    157             #    'Multi-layer GetMap requests are not supported', 'layers') 
     155            #layerName = layerName.split(',')[0] 
     156            raise InvalidParameterValue( 
     157                'Multi-layer GetLegend requests are not supported', 'layers') 
    158158        try: 
    159159            layerObj = self.layers[layerName] 
     
    239239 
    240240        # Dimension handling 
    241         finalImg = Image.new('RGBA', (width, height)) 
     241        finalImg = Image.new('RGBA', (width, height), (0,0,0,0)) 
    242242        # Multiple Layers handling..   
    243243        for layerName, layerObj in layers.iteritems(): 
     
    352352            raise InvalidParameterValue('Version %s not supported' % version, 
    353353                                        'version') 
    354         layerName, layerObj = self._getLayerParamInfo('query_layers') 
    355         format = self.getOwsParam('info_format') 
    356         if format not in layerObj.featureInfoFormats: 
    357             raise InvalidParameterValue( 
    358                 'Layer %s does not support GetFeatureInfo in format %s' % 
    359                 (layerName, format), 'info_format') 
    360  
    361         # Coordinate parameters 
     354 
     355        # Coordinate parameters 
    362356        bbox = tuple(float(x) for x in self.getOwsParam('bbox').split(',')) 
    363357        width = int(self.getOwsParam('width')) 
    364358        height = int(self.getOwsParam('height')) 
    365  
    366         if version == '1.1.1': 
    367             srs = self.getOwsParam('srs') 
    368         else: 
    369             srs = self.getOwsParam('crs') 
    370  
    371         if srs not in layerObj.crss: 
    372             raise InvalidParameterValue('Layer %s does not support SRS %s' % 
    373                                         (layerName, srs)) 
    374  
    375         # Dimension handling 
    376         dimValues = {} 
    377         for dimName, dim in layerObj.dimensions.items(): 
    378             defaultValue = dim.extent[0] 
    379             dimValues[dimName] = self.getOwsParam(dimName, default=defaultValue) 
    380         # Get pixel location 
     359           
     360        # Get pixel location 
    381361        i = int(self.getOwsParam('i')) 
    382362        j = int(self.getOwsParam('j')) 
     
    384364        # Translate to geo-coordinates 
    385365        x, y = bbox_util.pixelToGeo(i, j, bbox, width, height) 
    386  
    387         # Call the layer 
    388         response.headers['Content-Type'] = format 
    389         response.write(layerObj.getFeatureInfo(format, srs, (x, y), dimValues)) 
     366        #start preparing GetFeatureInfo response. Assumes "HTML" output format 
     367 
     368        htmlResponse = "<html><body><p> <b>Feature Information about pixel position: "+self.getOwsParam('i')+","+self.getOwsParam('j')+"/geo position: "+str(x)+","+str(y) +"<b/></p>" 
     369         
     370         
     371        layers = self._getLayerParam('query_layers') 
     372        #Adjusts response for multiple layers 
     373        if len(layers) > 1: 
     374            htmlResponse = htmlResponse+" Multiple possible features found as follows:" 
     375   
     376        htmlResponse = htmlResponse+"<ul>" 
     377         
     378        format = self.getOwsParam('info_format') 
     379        for layerName, layerObj in layers.iteritems(): 
     380            if format not in layerObj.featureInfoFormats: 
     381                raise InvalidParameterValue('Layer %s does not support GetFeatureInfo in format %s' %(layerName, format), 'info_format') 
     382 
     383        
     384 
     385            if version == '1.1.1': 
     386                srs = self.getOwsParam('srs') 
     387            else: 
     388                srs = self.getOwsParam('crs') 
     389 
     390            if srs not in layerObj.crss: 
     391                raise InvalidParameterValue('Layer %s does not support SRS %s' % 
     392                                                (layerName, srs)) 
     393 
     394        # Dimension handling 
     395            dimValues = {} 
     396            for dimName, dim in layerObj.dimensions.items(): 
     397                defaultValue = dim.extent[0] 
     398                dimValues[dimName] = self.getOwsParam(dimName, default=defaultValue) 
     399         
     400            htmlResponse = htmlResponse+"<li> Layer Name: "+layerName+" <br />" 
     401        # Call the layer 
     402            htmlResponse =htmlResponse+layerObj.getFeatureInfo(format, srs, (x, y), dimValues)+"</li>" 
     403         
     404        htmlResponse = htmlResponse+"</ul></body></html>" 
     405         
     406        response.headers['Content-Type'] = format 
     407        response.write(htmlResponse) 
    390408 
    391409    def GetLegend(self): 
  • TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/service/imps/StationCollection.py

    r3778 r3786  
    3030 
    3131    def getNearestStation(self,lat,lon): 
     32        ''' Determines the station nearest to the given geospatial point in the station collection ''' 
     33 
    3234        curNearest=None 
    3335        curDist = -1 
     
    3638                #calculate distance 
    3739                tempDist = self.getDist(lat, lon, station.lat, station.lon) 
    38                 #print [tempDist, lat, lon, station.lat, station.lon] 
     40                #if distance is smaller than the current shortest distance 
    3941                if curDist < 0 or tempDist < curDist: 
    4042                        curNearest = station 
     
    4547 
    4648    def getDist(self, srcLat, srcLon, destLat, destLon): 
    47          
     49        '''Calculates the distance between to geospatial points'''       
    4850        dlon = destLon - srcLon 
    4951        dlat = destLat - srcLat 
    50         a = (math.sin(dlat / 2))**2 + math.cos(srcLat) * math.cos(destLat) * (math.sin(dlon / 2))**2         
    51         c = 2 * math.asin(min(1, math.sqrt(a))) 
    52         dist = 3956 * c 
     52        #a = (math.sin(dlat / 2))**2 + math.cos(srcLat) * math.cos(destLat) * (math.sin(dlon / 2))**2         
     53        #c = 2 * math.asin(min(1, math.sqrt(a))) 
     54        #dist = 3956 * c 
     55        dist = math.sqrt(dlat**2 + dlon**2) 
    5356        return dist 
    5457                 
  • TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/service/imps/layers.py

    r3778 r3786  
    1 # my first python program 
     1''' 
     2This class reads the wms configuration XML document, extracts the layer elements specified in the xml file and stores them in a list.  This list of layer elements is used by the WMSLayerMapper class to create a StationLayer object for each layer element in the list, using the information recorded under each layer element. 
     3''' 
    24from xml.etree import cElementTree 
    35 
     
    1416                                if elem.tag == "Layer": 
    1517                                        curId = elem.findtext("Name") 
     18                                        # checks for duplicate layer name 
    1619                                        if self.indexList.count(curId) < 1: 
    1720                                                layerlist.append(elem) 
  • TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/service/imps/wms_layers.py

    r3778 r3786  
    77import Image 
    88from copy import copy 
    9 from pywms.render_imp import PointRenderer 
     9from pointrenderer import PointRenderer 
    1010from matplotlib import cm 
    1111import genutil 
     
    1616from xml.etree.ElementTree import ElementTree, Element, SubElement, XML 
    1717import urllib 
     18from matplotlib import dates  
     19 
    1820 
    1921class WMSLayerMapper(object): 
     
    2527     
    2628    """ 
     29    def __init__(self): 
     30     
     31        """ 
     32        Lists the names of all layers available as listed in the configuration file specified by the 'layer_config' parameter in the development.ini file. 
     33 
     34        @return: A mapping of layer names to ILayer implementations. 
     35        @raise ValueError: If no layers are available for these keywords. 
     36        """ 
     37         
     38        filename=config['layer_config'] 
     39        if not os.path.exists(filename): 
     40            raise Exception(str('Config File could not be found: %s')%filename) 
     41        print filename 
     42         
     43        #instantiate LayerParser class with the value of 'layer_config' as parameter to read layer infromation 
     44        layerparser = LayerParser(filename) 
     45 
     46        layermap={} 
     47        layers = layerparser.getLayers() 
     48        for feature in layers: 
     49            # read information necessary to create a StationLayer object 
     50            title, abstract, crss,formats, serverURL, icon, featureName, dataSet, bbox, dataSetURL =self._getInfo(feature) 
     51            # URL required to query the relevant WFS server to acquire a list of station for the current layer in the loop 
     52            geoServerUrl =serverURL+'/wfs?request=getfeature&service=wfs&version=1.1.0&typename='+featureName+'&maxfeatures=100' 
     53            print geoServerUrl 
     54            geoServerResponse = wget(geoServerUrl) 
     55            stationCollection=StationCollection(geoServerResponse) 
     56            # specify the filepath for the static image to be used for representing each station in the GetMap image 
     57            icon =config['csml_config']+'/img/'+icon 
     58            print icon 
     59            #instantiate a StationLayer object and store that in the layermap dictionary with the name of the layer as the key 
     60            layermap[feature.findtext("Name")]=StationLayer(title,abstract, crss, stationCollection,bbox, formats, icon, dataSet, dataSetURL) 
     61        if len(layermap) > 0: 
     62            self.layermap = layermap 
     63        else: 
     64            raise ValueError 
     65 
    2766     
    2867    def _getInfo(self, feature): 
    2968        ''' given a Station feature, return info about the layer/feature 
    30         @return:   title, abstract, dimensions, units, crss ''' 
     69        @return:    title, abstract, crss, formats, serverURL, icon, featureName,dataSet, bbox, dataSetURL ''' 
    3170 
    3271        try: 
     
    4382        print crss 
    4483        formats = [] 
    45         ##read supported getFeatureInfo formats from the "SupportedFormats" element 
     84         
     85        #read supported getFeatureInfo formats from the "SupportedFormats" element 
    4686        sFElem = feature.getchildren()[4] 
    4787        for format in sFElem.getchildren(): 
     
    5191        bboxElem = feature.getchildren()[5] 
    5292        bbox=[int(bboxElem.getchildren()[0].text),int(bboxElem.getchildren()[1].text),int(bboxElem.getchildren()[2].text),int(bboxElem.getchildren()[3].text)] 
    53         #static image to be used for WMS 
     93        #static image to be used for GetMap response 
    5494        icon = feature.getchildren()[6].text 
    5595        #read WFS server information 
     
    64104 
    65105    
    66     def map(self, **kwargs): 
    67         """ 
    68         Given Station.parser.Dataset object list the names of 
    69         all layers available. 
    70          
    71         @return: A mapping of layer names to ILayer implementations. 
    72         @raise ValueError: If no layers are available for these keywords.  
    73         """ 
    74         fileoruri=kwargs['fileoruri'] 
    75          
    76         #TODO - handle file paths/directories URIs in config 
    77         #.xml or .Station extensions are supported: 
    78         filename='%s/%s.Station'%(config['csml_config'],fileoruri) 
    79         if not os.path.exists(filename): 
    80            filename='%s/%s.xml'%(config['csml_config'],fileoruri) 
    81         if not os.path.exists(filename): 
    82             raise Exception(str('Config File could not be found: %s')%filename) 
    83              
    84         layerparser = LayerParser(filename) 
    85              
    86         layermap={} 
    87         layers = layerparser.getLayers() 
    88         for feature in layers: 
    89             title, abstract, crss,formats, serverURL, icon, featureName, dataSet, bbox, dataSetURL =self._getInfo(feature) 
    90             geoServerUrl =serverURL+'/wfs?request=getfeature&service=wfs&version=1.1.0&typename='+featureName+'&maxfeatures=14' 
    91             print geoServerUrl 
    92             geoServerResponse = wget(geoServerUrl) 
    93             stationCollection=StationCollection(geoServerResponse) 
    94             icon =config['csml_config']+'/img/'+icon 
    95             print icon 
    96             layermap[feature.findtext("Name")]=StationLayer(title,abstract, crss, stationCollection,bbox, formats, icon, dataSet, dataSetURL) 
    97         if len(layermap) > 0: 
    98             return layermap 
    99         else: 
    100             raise ValueError 
     106    def map(self): 
     107        '''this function is called by the wms_controller class to acquire information about all available layers''' 
     108        return self.layermap 
    101109 
    102110 
    103111class StationLayer(object): 
    104112    """ 
    105      representing a WMS layer.    Implements ILayer 
     113    representing a WMS layer for MIDAS/ECN Stations.    Implements ILayer 
    106114 
    107115    @ivar title: The layer title.  As seen in the Capabilities document. 
    108116    @ivar abstract:  Abstract as seen in the Capabilities document. 
    109     @ivar dimensions: A dictionary of IDimension objects. 
    110     @ivar units: A string describing the units. 
    111117    @ivar crss: A sequence of SRS/CRSs supported by this layer. 
    112  
    113     @todo: Do we need minValue/maxValue? 
     118    @ivar stationCollection: A list of NPStation objects to be rendered as part of GetMap response 
     119    @ivar formats: A list of output formats supported by the layer in question for GetFeatureInfo response 
     120    @ivar icon: A static image to be used for representing each station in the stationCollection in the GetMap response 
     121    @ivar dataSet: Name of the dataset to be used to construct an URL for the GetFeatureInfo response 
     122    @ivar dataSetURL: Server address part of the URL to be returned as a part of the GetFeatureInfo response 
    114123 
    115124    """ 
     
    119128        self.abstract=abstract 
    120129        self.crss=crss 
    121         self.legendSize=(30,100) 
     130        self.legendSize=(300,60) 
    122131        self.dimensions ={} 
    123132        self.stationCollection = stationCollection 
     
    129138    def getBBox(self, crs): 
    130139        """ 
    131         @return: A 4-typle of the bounding box in the given coordinate 
     140        @return: A 4-tuple of the bounding box in the given coordinate 
    132141            reference system. 
    133142        """ 
     
    150159            options 
    151160        @return: An object implementing ILayerSlab 
    152         #create netcdf for whole lat/lon for given dimValues, use to init slab 
     161         
    153162        """ 
    154163        bbox=self.getBBox(crs) 
     
    172181        """ 
    173182        Return a response string descibing the feature at a given 
    174         point in a given CRS. 
     183        point in a given CRS.  
     184         
     185        Currently only "html" is supported as output format 
    175186 
    176187        @param format: One of self.featureInfoFormats.  Defines which 
     
    181192        @param dimValues: A mapping of dimension names to dimansion values. 
    182193        @return: A string containing the response. 
    183  
     194         
    184195        """ 
    185196        print point 
     
    187198         
    188199        print nearestStation.desc 
    189         fullURL = self.dataSetURL+'/list?dataset_id='+self.dataSet+'&station_name='+nearestStation.desc 
    190         fullURL = fullURL.replace(' ', '%20') 
    191         print fullURL 
    192         filehandle = urllib.urlopen(fullURL) 
    193         x = filehandle.read() 
    194         return x 
    195  
     200         
     201         
     202        responseURL = self.dataSetURL+'/list?dataset_id='+self.dataSet+'&station_name='+nearestStation.desc 
     203        # replace space characters in the URL with %20 to avoid any URL validation error on the client side  
     204        responseURL = responseURL.replace(' ', '%20') 
     205        print responseURL 
     206        #finally construct the response, in this case it is in HTML with the responseURL represented as a hyperlink in it 
     207        response = "Description: <em>"+nearestStation.desc+"</em><br /><a href='"+responseURL+"'>"+responseURL+"</a>" 
     208        return response 
     209 
     210    def getLegendImage(self, orientation='vertical', renderOpts={}): 
     211        """ 
     212        Create an image of the colourbar for this layer. 
     213 
     214        @param orientation: Either 'vertical' or 'horizontal' 
     215        @return: A PIL image 
     216 
     217        """ 
     218        width = self.legendSize[0] 
     219        height = self.legendSize[1] 
     220        # if width and height specified in the GetLegend request parameters 
     221        # then use them instead of the default values 
     222        if 'width' in renderOpts: 
     223                width = renderOpts['width'] 
     224        if 'height' in renderOpts: 
     225                height = renderOpts['height'] 
     226        renderer=PointRenderer() 
     227        legImage = Image.new('RGBA', (width, height), (0,0,0,0)) 
     228        # legend for stations without any associated dataset 
     229        withoutData = Image.open(self.icon) 
     230        # legend for stations that contain datasets 
     231        withData= Image.open(self.icon+'1') 
     232        legImage.paste(withoutData, (0,0)) 
     233        legImage.paste(renderer.txt2img(self.title+' without dataset', "helvB08.pil"),(30, 5) ) 
     234        legImage.paste(withData, (0, 30)) 
     235        legImage.paste(renderer.txt2img(self.title+' with dataset', "helvB08.pil"),(30, 35) )         
     236        return legImage 
    196237 
    197238class StationDimension(object): 
     
    247288        print bbox 
    248289        stationsInBbox = self.stationCollection.getStationsInBBox(bbox[1],bbox[0],bbox[3], bbox[2]) 
    249         print len(stationsInBbox) 
    250         #print self.stationCollection.listAllLatLons() 
    251         cmap=eval(config['colourmap']) # renderOpts is hook for colourmap, for now use config 
    252         renderer=PointRenderer(0.0, 3.0)          
     290         
     291         
     292        cmap=eval(config['colourmap'])  
     293        renderer=PointRenderer()          
    253294        return renderer.renderPoint(bbox, stationsInBbox, width, height, cmap,self.icon) 
Note: See TracChangeset for help on using the changeset viewer.