Changeset 3366 for DPPP


Ignore:
Timestamp:
11/02/08 13:08:15 (12 years ago)
Author:
mkochan
Message:

Have working prototype psOntoWMS.py: draws points correctly, zoom supported.

Location:
DPPP/kml/csml2kml/python
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/notes.txt

    r3365 r3366  
    88|| Station.py / 
    99 
    10 [O] Write tests for both classes inside; make psOntoWMS.py use NPStation.py.  
     10[O] Make psOntoWMS.py use NPStation.py.  
    1111     
    1212---------------------------------------------------------------------------------------------------------------------------- 
  • DPPP/kml/csml2kml/python/prototypes/psOntoWMS.py

    r3327 r3366  
    1 import utils 
    21from Image import Image, open 
    32import ImageDraw 
    43from StringIO import StringIO 
    54import re 
    6 from StationConvertor import Station 
    75 
    8 def _parseStation(textGml): 
    9     ''' 
    10     Parse the GML returned from GeoServer, return a Station object with info about each station. 
    11     ''' 
    12     root = XML(textGml) 
    13     featureCollectionElement = root 
    14     for featureMember in featureCollectionElement.getchildren(): 
    15         stationElement = featureMember.getchildren()[0] 
    16         (stationNameElement, stationIDElement, locationElement) = stationElement.getchildren() 
    17         pointElement = locationElement.getchildren()[0] 
    18         posElement = pointElement.getchildren()[0] 
    19         (lon,lat) = map(float, posElement.text.split()) 
    20         yield Station(stationElement.get('{http://www.opengis.net/gml}id'), stationNameElement.text, lon, lat) 
     6import utils 
     7import Station 
    218 
    22 requestUrl = 'http://www-devel.ipcc-data.org/maps/wms/obs?request=GetMap&SERVICE=1.1.1&FORMAT=image/png&LAYERS=clim_10/cld&BBOX=-180,-90,180,90&WIDTH=960&HEIGHT=480&SRS=EPSG:4326&TIME=1905-03-15T00:00:00.0Z&STATION=midas.station.1600' 
     9class BBox: 
     10    def __init__(self, west, south, east, north): 
     11        if west < -180. or south < -90. or east > 180 or north > 90: 
     12            raise ValueError('Degree(s) out of bounds') 
     13        self.west = west; self.east = east 
     14        self.south = south; self.north = north 
     15    def __repr__(self): 
     16        return '%f,%f,%f,%f' % (self.west, self.south, self.east, self.north) 
    2317 
    24 rawImage = utils.wget(requestUrl) 
     18def extractBBoxFromWmsRequestUrl(wmsRequestUrl): 
     19    mo = re.search('BBOX\=([\-\d\.\,]+)', wmsRequestUrl) 
     20    bbox_str = mo.group(1) 
     21    coords_str = bbox_str.split(',') 
     22    coords = map(float, coords_str) 
     23    return BBox(coords[0], coords[1], coords[2], coords[3]) 
     24 
     25def buildWmsRequestUrl(bbox): 
     26    wmsRequestUrl = 'http://www-devel.ipcc-data.org/maps/wms/obs?request=GetMap&amp;amp;SERVICE=1.1.1&amp;amp;FORMAT=image/png&amp;amp;LAYERS=clim_10/cld&amp;amp;BBOX=%s&amp;amp;WIDTH=960&amp;amp;HEIGHT=480&amp;amp;SRS=EPSG:4326&amp;TIME=1905-03-15T00:00:00.0Z&amp;amp;STATION=midas.station.1600' % repr(bbox) 
     27    return wmsRequestUrl 
     28 
     29def drawStation(image, imageDraw, npStation, bbox): 
     30 
     31    (imgWidth, imgHeight) = image.size 
     32     
     33    x = imgWidth * (npStation.lon - bbox.west) / (bbox.east - bbox.west) 
     34    y = imgHeight * (npStation.lat - bbox.south) / (bbox.north - bbox.south) 
     35 
     36    if x >= 0 and x < imgWidth and y >= 0 and y < imgHeight: 
     37        left_top = (x - 5, y - 5) 
     38        right_bottom = (x + 5, y + 5) 
     39        imageDraw.ellipse(left_top + right_bottom, fill='black') 
     40 
     41bbox = BBox(-10,-10,10,10) 
     42wmsServerRequestUrl = buildWmsRequestUrl(bbox) 
     43 
     44rawImage = utils.wget(wmsServerRequest) 
     45 
     46geoServerUrl = 'http://bond.badc.rl.ac.uk:8089/dummyGeoServer/GetStationCSMLFeatures?gml_id=MIDAS_Stations_2' 
     47geoServerResponse = utils.wget(geoServerUrl) 
     48 
     49# Create a WFSStationCollection object, which is a representation of a <wfs:StationCollection> XML element. 
     50wfsStationsCollection = Station.WFSStationCollection() 
     51wfsStationsCollection.parseString(geoServerResponse) 
     52 
     53# Extract the stations from the collection 
     54npStations = wfsStationsCollection.stations 
     55 
    2556f = StringIO(rawImage) 
    2657img = open(f) 
    2758draw = ImageDraw.Draw(img) 
    28 for i in range(10): 
    29     left_top = (20*i - 5, 20*i - 5) 
    30     right_bottom = (20*i + 5, 20*i + 5) 
    31     draw.ellipse(left_top + right_bottom, fill='yellow') 
     59 
     60for npStation in npStations: 
     61    drawStation(img, draw, npStation, bbox) 
     62 
    3263img.save('yahoo.png') 
Note: See TracChangeset for help on using the changeset viewer.