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

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

Moved code for building placemarks into KMLDocument.py

Line 
1from cElementTree import ElementTree, Element, SubElement, XML
2from pylab import *
3from datetime import datetime, timedelta
4import re
5import csml
6import urllib
7
8from KMLDocument import KMLDocument, KMLStyle, KMLPlacemark, getBasicKMLStyle
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    '''
29    A generator method. Parse the GML returned from GeoServer and yield tuples containing information
30    (gml_id, name, lon, lat) about each station.
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# --------------------------------------------------------------------------------------------------------------------------
43
44url = 'http://bond.badc.rl.ac.uk:8089/dummyGeoServer/GetStationCSMLFeatures?gml_id=MIDAS_Stations'
45textGml = wget(url)
46print 'Download finished.'
47
48balloonTemplate = '<img src="http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot?feature_id=$[feature_id]">'
49eachStationData = {'feature_id': '#ID#'}
50
51basicKmlStyle = getBasicKMLStyle(balloonTemplate)
52kmlDocument = KMLDocument('MIDAS stations (example)', basicKmlStyle)
53
54generator =  parseGMLStationsCollection(textGml)
55for (gml_id, name, lon, lat) in generator:
56    print 'Station %s ("%s"): @ (%f,%f)' %  (gml_id, name, lon, lat)
57    kmlStation = KMLPlacemark(gml_id, name, lon, lat, data=eachStationData)
58    kmlDocument.appendElement(kmlStation.build())
59kmlDocument.save('./../../output/exampleps_stations.kml')
Note: See TracBrowser for help on using the repository browser.