Changeset 3633 for DPPP


Ignore:
Timestamp:
17/03/08 15:34:40 (11 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.

Location:
DPPP/kml/csml2kml
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/Station.py

    r3614 r3633  
    2424    ''' 
    2525 
    26     def __init__(self, id = None, desc = None, lon = None, lat = None, features = None): 
     26    def __init__(self, id = None, desc = None, lon = None, lat = None, stationFeatures = None): 
    2727        ''' 
    2828        The initialiser (use empty call C{NPStation()} and later call the C{parseString} or C{parseXML} method). 
     
    3131        self.desc = desc 
    3232        self.lon = lon; self.lat = lat 
    33         self.csmlFeatureIds = csmlFeatureIds 
     33        self.stationFeatures = stationFeatures 
    3434 
    3535    def parseString(self, str): 
     
    4848 
    4949        # Extract the identifier, short description, and longitude and latitute 
    50         (stationNameElement, stationIDElement, locationElement, featuresElement) = stationElement.getchildren() 
     50        (stationNameElement, stationIDElement, locationElement, stationFeaturesElement) = stationElement.getchildren() 
    5151        pointElement = locationElement.getchildren()[0] 
    5252        posElement = pointElement.getchildren()[0] 
     
    5555        self.desc = stationNameElement.text 
    5656 
    57         # Extract the IDs of the associated CSML features 
    58         self.csmlFeatureIds = [] 
    59         for stationsFeatureElement in featuresElement.getchildren(): 
    60             (featureTypeElement, featureParameterElement) = stationsFeatureElement.getchildren() 
    61             if featureTypeElement.text != 'csml:PointSeriesFeature': 
    62                 raise TypeError('Feature type not "csml:PointSeriesFeature"') 
    63             self.csmlFeatureIds.append(featureParameterElement.text) 
     57        self.stationFeatures = [] 
     58        for stationFeatureElement in stationFeaturesElement.getchildren(): 
     59            npStationFeature = NPStationFeature() 
     60            npStationFeature.parseXML(stationFeatureElement) 
     61            self.stationFeatures.append(npStationFeature) 
    6462 
    6563    def __repr__(self): 
    6664        return '<np:Station> %s ("%s") located at (%f, %f) with %d features (%s).' % ( 
    67             self.id, self.desc, self.lon, self.lat, len(self.csmlFeatureIds), str(self.csmlFeatureIds) 
     65            self.id, self.desc, self.lon, self.lat, len(self.stationFeatures), str(self.stationFeatures) 
    6866            ) 
    6967 
     
    9694        (featureTypeElement, featureIdElement,  
    9795         collectBeginDateElement, collectEndDateElement) = stationFeatureElement.getchildren() 
    98         if featureTypeElement != 'csml:PointSeriesFeature': 
     96        if featureTypeElement.text != 'csml:PointSeriesFeature': 
    9997            raise NotImplementedError('No other than "csml:PointSeriesFeature" feature type is supported') 
    100         self.featureID = featureIdElement.text 
     98        self.featureType = featureTypeElement.text 
     99        self.featureId = featureIdElement.text 
    101100        self.collectBeginDate = dates.dateutil.parser.parse(collectBeginDateElement.text) 
    102101        self.collectEndDate = dates.dateutil.parser.parse(collectEndDateElement.text) 
     102 
     103    def __repr__(self): 
     104        return str(vars(self)) 
    103105 
    104106class WFSStationCollection: 
  • DPPP/kml/csml2kml/python/csml2kmlpylon/csml2kmlpylon/config/middleware.py

    r3590 r3633  
    4646        # Display error documents for 401, 403, 404 status codes (and 
    4747        # 500 when debug is disabled) 
    48         app = ErrorDocuments(app, global_conf, mapper=error_mapper, **app_conf) 
     48        # -- commented out by Martin Kochan, 17/03/2008 
     49        # -- to display details in HTTP 404 error messages 
     50        # app = ErrorDocuments(app, global_conf, mapper=error_mapper, **app_conf) 
    4951 
    5052    # Establish the Registry for this application 
  • 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() 
  • DPPP/kml/csml2kml/testdata/midas.csml2kml.conf.xml

    r3587 r3633  
    1212  </NPStations2KML> 
    1313  <GrapherWebService> 
    14      <DisplayIntervalStart>1-JAN-2003</DisplayIntervalStart> 
    15      <DisplayIntervalEnd>31-JAN-2003</DisplayIntervalEnd> 
     14     <DisplayIntervalStart>1-1-2003</DisplayIntervalStart> 
     15     <DisplayIntervalEnd>31-1-2003</DisplayIntervalEnd> 
    1616     <GeoServerURL>http://130.246.76.98:8084/geoserver/wfs</GeoServerURL> 
    1717  </GrapherWebService> 
Note: See TracChangeset for help on using the changeset viewer.