Changeset 3576


Ignore:
Timestamp:
10/03/08 17:46:55 (11 years ago)
Author:
mkochan
Message:

Updated csmlGrapher and StationConvertor? to allow dynamic display of a list of CSML features per station, and per-feature graphing.

Location:
DPPP/kml/csml2kml
Files:
5 edited

Legend:

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

    r3562 r3576  
    177177    ''' 
    178178     
    179     def __init__(self, id, name, lon, lat, featuresHtml, styleID = None, data=None, visible = True): 
     179    def __init__(self, id, name, lon, lat, styleID = None, data=None, visible = True): 
    180180        self.id = id 
    181181        self.name = name 
    182182        self.lon = lon 
    183183        self.lat = lat 
    184         self.featuresHtml = featuresHtml 
    185184        self.styleID = styleID 
    186185        self.data = data 
     
    217216                value = value.replace('#ID#', self.id) 
    218217                value = value.replace('#NAME#', self.name) 
    219                 value = value.replace('#LON#', str(self.lon)) 
    220                 value = value.replace('#LAT#', str(self.lat)) 
    221                 value = value.replace('#FEATURES#', self.featuresHtml) 
    222218                SubElement(dataElement, 'value').text = value 
    223219                 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/StationConvertor.py

    r3558 r3576  
    4646        self.lockedFolderStyle = KMLStyle('locked_folder_style', listItemType = 'checkHideChildren') 
    4747 
    48         self.grapherUrl = self.config.find('GrapherRequest/URL').text 
    49  
    5048    def _npStationToKmlPlacemark(self, npStation): 
    5149        '''Converts a Station.NPStation object into it's KML.KMLPlacemark representation''' 
    52         featuresHtml = '' 
    53         for featureId in npStation.csmlFeatureIds: 
    54             featuresHtml = featuresHtml + '<img src="%s?station_name=%s&feature_id=%s"><br>' % (self.grapherUrl, npStation.desc, featureId) 
    55         return KMLPlacemark(npStation.id, npStation.desc, npStation.lon, npStation.lat, featuresHtml, 
     50        return KMLPlacemark(npStation.id, npStation.desc, npStation.lon, npStation.lat, 
    5651                            styleID = self.placemarkKmlStyle.id, data = self.stationData 
    5752                            ) 
     
    120115        Convert GML input from GeoServer (containing the <Station> tags with station information) 
    121116        to the KML representation. 
    122         [NOTE] At the moment, since the <Station> GML-feature element does not contain a list of CSML features, 
    123                the output of this code is not entirely correct. In particular, the HTTP reference to the grapher web-service 
    124                cannot be correctly determined. 
    125117        ''' 
    126118 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/tests/testStationConvertor.py

    r3439 r3576  
    1 from StationConvertor import StationConvertor 
     1from csml2kml.StationConvertor import StationConvertor 
    22from cElementTree import ElementTree 
    33 
    44# Load the configuration XML element 
    55configTree = ElementTree() 
    6 configTree.parse('../../../testdata/midas.csml2kml.conf.xml') 
     6configTree.parse('../../../../testdata/midas.csml2kml.conf.xml') 
    77configRoot = configTree.getroot() 
    88config = configRoot.find('NPStations2KML') 
    99 
    1010# Do the conversion 
    11 convertor = StationConvertor(config, '../../../output/exampleps_stations.3.kml') 
     11convertor = StationConvertor(config, '../../../../output/exampleps_stations.4.kml') 
    1212convertor.convert() 
  • DPPP/kml/csml2kml/python/pylonsstack/pylonsstack/controllers/csmlGrapher.py

    r3567 r3576  
    44 
    55# Other imports 
    6 import csml 
    76import Image 
    87import pylab 
     
    1312from cStringIO import StringIO 
    1413from tempfile import NamedTemporaryFile 
    15 from cElementTree import Element, ElementTree, XML 
    16 from utils import wget 
     14from cElementTree import Element, SubElement, ElementTree, XML 
     15from urllib import quote 
     16 
     17import csml 
     18import csml2kml.Station 
     19from csml2kml.utils import wget 
    1720 
    1821log = logging.getLogger(__name__) 
     
    8184            # Prepare the plot the figure (actual plotting actions are postponed until save) 
    8285            fig = figure() 
    83             plot_date(elapsed_times, vals, '-o', xdate=True, lw=2) 
     86            plot_date(elapsed_times, vals, 'b-', xdate=True, lw=1) 
     87            plot_date(elapsed_times, vals, 'go', markeredgecolor = 'g', xdate=True, lw=2) 
    8488            ax = gca() 
    8589            ax.xaxis.set_major_locator(tickLocator) 
     
    112116        # Get parameters from the request object 
    113117        try: 
    114             feature_id = request.params['feature_id'] 
    115             station_name = request.params['station_name'] 
     118            feature_id = str(request.params['feature_id'])     # convert back from Unicode 
     119            station_name = str(request.params['station_name']) # convert back from Unicode  
    116120        except KeyError: 
    117             raise HTTPBadRequest('Parameters "feature_id" and "station_id" must be supplied.') 
     121            raise HTTPBadRequest('Parameters "feature_id" and "station_name" must be supplied.') 
    118122 
    119         # Get response from GeoServer and parse it into an ElementTree 
    120         lowerTimeBoundary = '1-JAN-2003'  # hard-coded for now 
    121         upperTimeBoundary = '1-FEB-2004'  # hard-coded for now 
    122         geoServerRequestUrl = 'http://130.246.76.98:8084/geoserver/wfs?request=getfeature&service=wfs&version=1.1.0&typename=%20csml:PointSeriesFeature&filter=<ogc:Filter%20xmlns:ogc="http://www.opengis.net/ogc"%20xmlns: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>'+ lowerTimeBoundary +'</ogc:Literal></ogc:LowerBoundary><ogc:UpperBoundary><ogc:Literal>'+ upperTimeBoundary +'</ogc:Literal></ogc:UpperBoundary></ogc:PropertyIsBetween></ogc:And></ogc:Filter>' 
     123        # Prepare the request for the GeoServer 
     124        lowerTimeBoundary = '1-JAN-2003'   # hard-coded for now 
     125        upperTimeBoundary = '31-JAN-2003'  # hard-coded for now 
     126        geoServerRequestUrl = urllib.quote('http://130.246.76.98:8084/geoserver/wfs?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>'+ lowerTimeBoundary +'</ogc:Literal></ogc:LowerBoundary><ogc:UpperBoundary><ogc:Literal>'+ upperTimeBoundary +'</ogc:Literal></ogc:UpperBoundary></ogc:PropertyIsBetween></ogc:And></ogc:Filter>', '/._:?&=') 
    123127 
    124         # [NOTE] Need to experiment with encoding for this request -- this one does not work. Maybe 
    125         #        return back to using URL escape characters, and play around with it? 
    126         #geoServerRequestUrl = 'http://130.246.76.98:8084/geoserver/wfs?request=getfeature&service=wfs&version=1.1.0&typename=%20csml:PointSeriesFeature&filter=%3Cogc:Filter%20xmlns:ogc=%22http://www.opengis.net/ogc%22%20xmlns:gml=%22http://www.opengis.net/gml%22%3E%3Cogc:And%3E%3Cogc:PropertyIsEqualTo%3E%3Cogc:PropertyName%3Egml:description%3C/ogc:PropertyName%3E%3Cogc:Literal%3E' + station_name + '%3C/ogc:Literal%3E%3C/ogc:PropertyIsEqualTo%3E%3Cogc:PropertyIsEqualTo%3E%3Cogc:PropertyName%3Ecsml:parameter/swe:Phenomenon/gml:name%3C/ogc:PropertyName%3E%3Cogc:Literal%3E' + feature_id + '%3C/ogc:Literal%3E%3C/ogc:PropertyIsEqualTo%3E%3Cogc:PropertyIsBetween%3E%3Cogc:PropertyName%3Ecsml:value/csml:PointSeriesCoverage/csml:pointSeriesDomain/csml:TimeSeries/csml:timePositionList%3C/ogc:PropertyName%3E%3Cogc:LowerBoundary%3E%3Cogc:Literal%3E' + lowerTimeBoundary + '%3C/ogc:Literal%3E%3C/ogc:LowerBoundary%3E%3Cogc:UpperBoundary%3E%3Cogc:Literal%3E' + upperTimeBoundary + '%3C/ogc:Literal%3E%3C/ogc:UpperBoundary%3E%3C/ogc:PropertyIsBetween%3E%3C/ogc:And%3E%3C/ogc:Filter%3E' 
     128        # Acquire the GeoServer response and parse it into a CSMLFeatureCollection object 
     129        geoServerResponse = wget(geoServerRequestUrl) 
     130        if not geoServerResponse: 
     131            raise LookupError('Cannot acquire response from server (wrong URL or server down)') 
     132        csmlFeatureCollection = csml.parser.CSMLFeatureCollection() 
     133        csmlFeatureCollection.fromXML( XML(geoServerResponse) ) 
    127134 
    128         # Get the GeoServer response and parse it in as a CSML PointSeriesFeature 
    129         geoServerResponse = wget(geoServerRequestUrl) 
    130         fc = csml.parser.CSMLFeatureCollection() 
    131         fc.fromXML( XML(geoServerResponse) ) 
    132         raise ValueError('Oops') 
     135        # Now, csmlFeatureCollection should only contain a single CSML feature (if the feature was found). 
     136        # Isolate that CSML feature. 
     137        try: 
     138            csmlFeature = csmlFeatureCollection.featureMembers 
     139        except AttributeError: 
     140            raise LookupError('No PointSeriesFeature with ID "' + feature_id + '" found in the collection.') 
    133141 
    134         # If there was a parsing error, raise an exception 
    135         if not feature: 
    136             raise HTTPNotFound('Feature not found') 
    137              
     142        # Make sure that the feature is a PointSeriesFeature 
     143        if not isinstance(csmlFeature, csml.parser.PointSeriesFeature): 
     144            raise TypeError('CSML feature not a PointSeriesFeature') 
     145        csmlPointSeriesFeature = csmlFeature 
     146 
    138147        # Try to plot the feature into a temporary file, and put the contents of that file into the response 
    139148        try: 
    140             tempFile = _plot_feature(feature)              # plot the feature into a temporary file 
    141             _set_response(tempFile)                        # set the response as an image containing the plot 
     149            tempFile = _plot_feature(csmlPointSeriesFeature)    # plot the feature into a temporary file 
     150            _set_response(tempFile)                             # set the response as an image containing the plot 
    142151        finally: 
    143152            try: 
     
    151160        # Get parameters from the request object 
    152161        try: 
    153             feature_id = request.params['feature_id'] 
    154             station_name = request.params['station_name'] 
     162            station_name = str(request.params['station_name']) # convert back from Unicode  
    155163        except KeyError: 
    156             raise HTTPBadRequest('Parameters "feature_id" and "station_id" must be supplied.') 
     164            raise HTTPBadRequest('Parameter "station_name" must be supplied.') 
    157165 
    158         # [NOTE] Should use: fc = csml.parser.CSMLFeatureCollection() 
     166        # Prepare the request for the GeoServer 
     167        geoServerRequestUrl = urllib.quote('http://130.246.76.98:8084/geoserver/wfs?request=getFeature&service=wfs&version=1.1.0&typename=np:Station&filter=<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><ogc:PropertyIsEqualTo><ogc:PropertyName>stationName</ogc:PropertyName><ogc:Literal>' + station_name + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>', '/._:?&=') 
     168 
     169        # Acquire the GeoServer response and parse it into a WFSStationCollection object 
     170        geoServerResponse = wget(geoServerRequestUrl) 
     171        if not geoServerResponse: 
     172            raise LookupError('Cannot acquire response from server (wrong URL or server down)') 
     173        wfsStationCollection = csml2kml.Station.WFSStationCollection() 
     174        wfsStationCollection.parseString(geoServerResponse) 
     175        if len(wfsStationCollection.stations) != 1: 
     176            raise ValueError('Multiple stations match OGC selection filter (only one must match)') 
     177        wfsStation = wfsStationCollection.stations[0] 
     178 
     179        htmlElement = Element('html') 
     180        SubElement(htmlElement, 'title').text = 'List of CSML features for station ' + station_name 
     181        bodyElement = SubElement(htmlElement, 'body') 
     182        for csmlFeatureId in wfsStation.csmlFeatureIds: 
     183            anchorElement = SubElement(bodyElement, 'a') 
     184            linkToGrapher = 'http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot?station_name=' + station_name + '&feature_id=' + csmlFeatureId 
     185            anchorElement.set('href', linkToGrapher) 
     186            anchorElement.text = csmlFeatureId 
     187            SubElement(bodyElement, 'br') 
     188        htmlStringIO = StringIO() 
     189        ElementTree(htmlElement).write(htmlStringIO) 
     190 
     191        response.content_type = 'text/html' 
     192        response.content = htmlStringIO.getvalue() 
     193         
  • DPPP/kml/csml2kml/testdata/midas.csml2kml.conf.xml

    r3436 r3576  
    11<CSML2KMLConfig> 
    22  <NPStations2KML> 
    3     <!-- Notes: The GrapherURL tag should refer to a service that can graph csml:PointSeriesFeature elements, 
    4          so that a request to address "GrapherURL?feature_id=some-feature-id" results in that feature being 
    5          drawn ( hence, the graphing service must be accessing the same GeoServer ). --> 
    6     <name>MIDAS stations (example)</name> 
     3    <name>MIDAS stations (first 100 of them)</name> 
    74    <UseRegions>yes</UseRegions> 
    85    <GeoServerRequest> 
    9       <URL>http://bond.badc.rl.ac.uk:8089/dummyGeoServer/GetStationCSMLFeatures?gml_id=MIDAS_Stations_with_features</URL> 
    10       <BalloonTemplate>&lt;h2&gt;Station $[station_name]&lt;/h2&gt;Contains features:&lt;br&gt;$[station_features]</BalloonTemplate> 
     6      <URL><![CDATA[http://130.246.76.98:8084/geoserver/wfs?request=getFeature&service=wfs&version=1.1.0&typename=np:Station&maxFeatures=100]]></URL> 
     7      <BalloonTemplate>&lt;h2&gt;Station $[station_name]&lt;/h2&gt;Click here to see the list of &lt;a href=&quot;http://bond.badc.rl.ac.uk:8089/csmlGrapher/list?station_name=$[station_name]&quot;&gt;CSML features associated with this station&lt;/a&gt; (this will open a window of your default web browser).</BalloonTemplate> 
    118      <StationData> 
    129        <Datum name="station_id">#ID#</Datum> 
    1310        <Datum name="station_name">#NAME#</Datum> 
    14         <Datum name="station_features">#FEATURES#</Datum> 
    1511      </StationData> 
    1612    </GeoServerRequest> 
    17     <GrapherRequest> 
    18       <URL>http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot</URL> 
    19     </GrapherRequest>  
    2013  </NPStations2KML> 
    2114</CSML2KMLConfig> 
Note: See TracChangeset for help on using the changeset viewer.