source: DPPP/kml/csml2kml/python/prototypes/buildPointSeriesWithReferencedPlotsUsingGeoServerOO.py @ 3291

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/prototypes/buildPointSeriesWithReferencedPlotsUsingGeoServerOO.py@3291
Revision 3291, 2.7 KB checked in by mkochan, 12 years ago (diff)

Made other prototypes use KMLDocument-encapsulated functionality. Tested using these prototypes.

RevLine 
[3285]1from cElementTree import ElementTree, Element, SubElement, XML
2from pylab import *
3from datetime import datetime, timedelta
4import re
5import csml
6import urllib
7
[3291]8from KMLDocument import *
[3285]9
10# --------------------------------------------------------------------------------------------------------------------------
11
12def wget(url):
13    content = None
14    try:
15        socket = urllib.urlopen(url)               # open a socket (actually a file-like object)
16        print 'Socket open.'
17        content = socket.read()                    # read the text in
18        print 'Got contents.'
19    finally:
20        try:
21            socket.close()
22            print 'Socket closed.'
23        except NameError:
24            pass  # socket undefined -- no need to close
25    return content
26
27def parseGMLStationsCollection(textGml):
28    '''
[3291]29    Parse the GML returned from GeoServer and yield tuples containing information (gml_id, name, lon, lat)
30    about each station.
[3285]31    '''
32    root = XML(textGml)
33    featureCollectionElement = root
34    for featureMember in featureCollectionElement.getchildren():
35        stationElement = featureMember.getchildren()[0]
36        (stationNameElement, stationIDElement, locationElement) = stationElement.getchildren()
37        pointElement = locationElement.getchildren()[0]
38        posElement = pointElement.getchildren()[0]
39        (lon,lat) = map(float, posElement.text.split())
40        yield (stationElement.get('{http://www.opengis.net/gml}id'), stationNameElement.text, lon, lat)
41
42# --------------------------------------------------------------------------------------------------------------------------
[3291]43#
44# [NOTE] At the moment, since the <Station> GML-feature element does not contain a list of CSML features,
45#        the output of this code is not entirely correct. In particular, the HTTP reference to the grapher web-service
46#        cannot be correctly determined.
[3285]47
48url = 'http://bond.badc.rl.ac.uk:8089/dummyGeoServer/GetStationCSMLFeatures?gml_id=MIDAS_Stations'
49textGml = wget(url)
50print 'Download finished.'
51
52balloonTemplate = '<img src="http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot?feature_id=$[feature_id]">'
[3289]53eachStationData = {'feature_id': '#ID#'}
54
[3291]55placemarkKmlStyle = createDefaultPlacemarKMLStyle(balloonTemplate = balloonTemplate)
56kmlDocument = KMLDocument('MIDAS stations (example)', [placemarkKmlStyle])
[3285]57
[3289]58generator =  parseGMLStationsCollection(textGml)
59for (gml_id, name, lon, lat) in generator:
[3285]60    print 'Station %s ("%s"): @ (%f,%f)' %  (gml_id, name, lon, lat)
[3291]61    kmlStation = KMLPlacemark(gml_id, name, lon, lat, styleID = placemarkKmlStyle.id, data = eachStationData)
62    kmlDocument.elements.append(kmlStation)
63
[3285]64kmlDocument.save('./../../output/exampleps_stations.kml')
Note: See TracBrowser for help on using the repository browser.