Ignore:
Timestamp:
17/03/08 15:34:40 (12 years ago)
Author:
mkochan
Message:

Finished listing of features according to collection times (only those features are listed which are available during the whole interval. Also, add more detailed error messages.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/csml2kmlpylon/csml2kmlpylon/controllers/csmlGrapher.py

    r3592 r3633  
    22import logging 
    33from csml2kmlpylon.lib.base import * 
     4from paste.httpexceptions import HTTPBadRequest, HTTPNotFound 
    45 
    56# Other imports 
     
    3637 
    3738        # Set the configurable variables 
    38         self.displayIntervalStart = grapherConfig.find('DisplayIntervalStart').text 
    39         self.displayIntervalEnd = grapherConfig.find('DisplayIntervalEnd').text 
     39        self.displayIntervalStart = dates.dateutil.parser.parse(grapherConfig.find('DisplayIntervalStart').text) 
     40        self.displayIntervalEnd = dates.dateutil.parser.parse(grapherConfig.find('DisplayIntervalEnd').text) 
    4041        self.geoServerUrl = grapherConfig.find('GeoServerURL').text 
    4142 
     
    4344        return BaseController.__call__(self, environ, start_response) 
    4445 
     46    def _datetimeToGeoServerDate(self, datetime): 
     47        monthCodes = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'] 
     48        return repr(datetime.day) + '-' + monthCodes[datetime.month-1] + '-' + repr(datetime.year) 
     49 
    4550    def _retrieveCsmlPointSeriesFeature(self, station_name, feature_id): 
    4651        ''' 
    47         @return: A PointSeriesFeature object representing the single CSML feature. 
    48         @throws: A LookupError exception on retrieval error. 
    49         ''' 
    50  
     52        @return: A C{PointSeriesFeature} object representing the single CSML feature if the feature was found; 
     53        C{None} if a feature collection has been returned, but contains no CSML features (this happens when 
     54        there are no measured time points in the used time interval). 
     55        @throws: A C{LookupError} exception with a message if there is no response from the server. 
     56        ''' 
     57         
    5158        # Prepare the request for the GeoServer. 
    52         geoServerRequestUrl = urllib.quote(self.geoServerUrl + '?request=getfeature&service=wfs&version=1.1.0&typename=csml:PointSeriesFeature&filter=<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><ogc:And><ogc:PropertyIsEqualTo><ogc:PropertyName>gml:description</ogc:PropertyName><ogc:Literal>'+ station_name +'</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>csml:parameter/swe:Phenomenon/gml:name</ogc:PropertyName><ogc:Literal>'+ feature_id +'</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsBetween><ogc:PropertyName>csml:value/csml:PointSeriesCoverage/csml:pointSeriesDomain/csml:TimeSeries/csml:timePositionList</ogc:PropertyName><ogc:LowerBoundary><ogc:Literal>'+ self.displayIntervalStart +'</ogc:Literal></ogc:LowerBoundary><ogc:UpperBoundary><ogc:Literal>'+ self.displayIntervalEnd +'</ogc:Literal></ogc:UpperBoundary></ogc:PropertyIsBetween></ogc:And></ogc:Filter>', '/._:?&=') 
     59        geoServerRequestUrl = urllib.quote(self.geoServerUrl + '?request=getfeature&service=wfs&version=1.1.0&typename=csml:PointSeriesFeature&filter=<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><ogc:And><ogc:PropertyIsEqualTo><ogc:PropertyName>gml:description</ogc:PropertyName><ogc:Literal>'+ station_name +'</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>csml:parameter/swe:Phenomenon/gml:name</ogc:PropertyName><ogc:Literal>'+ feature_id +'</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsBetween><ogc:PropertyName>csml:value/csml:PointSeriesCoverage/csml:pointSeriesDomain/csml:TimeSeries/csml:timePositionList</ogc:PropertyName><ogc:LowerBoundary><ogc:Literal>'+ self._datetimeToGeoServerDate(self.displayIntervalStart) +'</ogc:Literal></ogc:LowerBoundary><ogc:UpperBoundary><ogc:Literal>'+ self._datetimeToGeoServerDate(self.displayIntervalEnd) +'</ogc:Literal></ogc:UpperBoundary></ogc:PropertyIsBetween></ogc:And></ogc:Filter>', '/._:?&=') 
    5360 
    5461        # Acquire the GeoServer response and parse it into a CSMLFeatureCollection object 
     
    5966        csmlFeatureCollection.fromXML( XML(geoServerResponse) ) 
    6067 
    61         # Now, csmlFeatureCollection should only contain a single CSML feature (if the feature was found). 
    62         # Isolate that CSML feature. 
     68        # If csmlFeatureCollection contains a single CSML feature, the isolate it. If it contains no feature, 
     69        # probably no data points are available for that interval. 
    6370        try: 
    6471            csmlFeature = csmlFeatureCollection.featureMembers 
    6572        except AttributeError: 
    66             raise LookupError('No PointSeriesFeature with ID "' + feature_id + '" found in the collection.') 
     73            raise LookupError( 
     74                'No data points available for the given time interval. ' +  
     75                'Perhaps the interval is shorter than the frequency of data points, ' + 
     76                'or collection of data for this feature was interrupted at the time.' 
     77                ) 
    6778 
    6879        # Make sure that the feature is a PointSeriesFeature 
    6980        if not isinstance(csmlFeature, csml.parser.PointSeriesFeature): 
    70             raise TypeError('CSML feature not a PointSeriesFeature') 
     81            raise LookupError('CSML feature not a PointSeriesFeature') 
    7182        csmlPointSeriesFeature = csmlFeature 
    7283 
     
    177188            raise HTTPBadRequest('Parameters "feature_id" and "station_name" must be supplied.') 
    178189 
    179         # Retrieve the csmlPointSeriesFeature object 
    180         csmlPointSeriesFeature = self._retrieveCsmlPointSeriesFeature(station_name, feature_id) 
     190        # Try to retrieve the csmlPointSeriesFeature object 
     191        try: 
     192            csmlPointSeriesFeature = self._retrieveCsmlPointSeriesFeature(station_name, feature_id) 
     193        except LookupError, e: 
     194            errorMessage = e.args[0] 
     195            httpNotFound = HTTPNotFound() 
     196            httpNotFound.explanation = '' 
     197            httpNotFound.detail = errorMessage 
     198            raise httpNotFound 
    181199 
    182200        # Try to plot the feature into a temporary file, and put the contents of that file into the response 
     
    220238 
    221239        # Generate HTML with a list of CSML features contained in the station. 
     240        # Currently, this is done simply by directly generating HTML (i.e. no template was used). 
    222241        htmlElement = Element('html') 
    223242        SubElement(htmlElement, 'title').text = 'List of CSML features for station ' + station_name 
    224243        bodyElement = SubElement(htmlElement, 'body') 
    225         SubElement(htmlElement, 'h2').text = 'List of CSML features for station ' + station_name 
    226         tableElement = SubElement(htmlElement, 'table') 
     244        SubElement(bodyElement, 'h2').text = 'List of CSML features for station ' + station_name 
     245        tableElement = SubElement(bodyElement, 'table') 
    227246        tableElement.set('border', '1') 
    228         for csmlFeatureId in wfsStation.csmlFeatureIds: 
    229             try: 
    230                 # Test whether the feature exists. Remove this if such test is no more necessary. 
    231                 self._retrieveCsmlPointSeriesFeature(station_name, csmlFeatureId) 
    232  
    233                 # If no LookupError exception has been caught, proceed with inluding the feature in the list. 
     247        headingRowElement = SubElement(tableElement, 'tr') 
     248        SubElement(headingRowElement, 'th').text = 'Feature name' 
     249        SubElement(headingRowElement, 'th').text = 'Data collected since*' 
     250        SubElement(headingRowElement, 'th').text = 'Data collected until* ' 
     251        for stationFeature in wfsStation.stationFeatures: 
     252            if self.displayIntervalStart >= stationFeature.collectBeginDate and self.displayIntervalEnd <= stationFeature.collectEndDate: 
    234253                rowElement = SubElement(tableElement, 'tr') 
    235                 dataElement = SubElement(rowElement, 'td') 
    236                 anchorElement = SubElement(dataElement, 'a') 
    237                 linkToGrapher = 'http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot?station_name=' + station_name + '&feature_id=' + csmlFeatureId 
     254                featureNameElement = SubElement(rowElement, 'td') 
     255                anchorElement = SubElement(featureNameElement, 'a') 
     256                linkToGrapher = 'http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot?station_name=' + station_name + '&feature_id=' + stationFeature.featureId 
    238257                anchorElement.set('href', linkToGrapher) 
    239                 anchorElement.text = csmlFeatureId 
    240             except LookupError: 
    241                 pass 
     258                anchorElement.text = stationFeature.featureId 
     259                SubElement(rowElement, 'td').text = self._datetimeToGeoServerDate(stationFeature.collectBeginDate) 
     260                SubElement(rowElement, 'td').text = self._datetimeToGeoServerDate(stationFeature.collectEndDate) 
    242261 
    243262        htmlStringIO = StringIO() 
Note: See TracChangeset for help on using the changeset viewer.