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

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

Made prototype use KMLDocument as a module.

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, 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 buildPlacemarkElement(id,name,lon,lat):
28
29    placemarkElement = Element('Placemark')
30    SubElement(placemarkElement, 'name').text = name
31    SubElement(placemarkElement, 'open').text = '0'
32    SubElement(placemarkElement, 'visibility').text = '0'
33    SubElement(placemarkElement, 'styleUrl').text = '#basic_style'
34
35    lookAtElement = SubElement(placemarkElement, 'LookAt')
36    SubElement(lookAtElement, 'longitude').text = str(lon)
37    SubElement(lookAtElement, 'latitude').text = str(lat)
38
39    pointElement = SubElement(placemarkElement, 'Point')
40    SubElement(pointElement, 'coordinates').text = '%f,%f,%f' % (lon, lat, 0.)
41
42    extendedDataElement = SubElement(placemarkElement, 'ExtendedData')
43
44    dataElement = SubElement(extendedDataElement, 'Data')
45    dataElement.set('name', 'feature_id')
46    SubElement(dataElement, 'value').text = id
47
48    return placemarkElement
49
50def parseGMLStationsCollection(textGml):
51    '''
52    A generator method. Parse the GML returned from GeoServer and yield tuples containing information
53    (gml_id, name, lon, lat) about each station.
54    '''
55    root = XML(textGml)
56    featureCollectionElement = root
57    for featureMember in featureCollectionElement.getchildren():
58        stationElement = featureMember.getchildren()[0]
59        (stationNameElement, stationIDElement, locationElement) = stationElement.getchildren()
60        pointElement = locationElement.getchildren()[0]
61        posElement = pointElement.getchildren()[0]
62        (lon,lat) = map(float, posElement.text.split())
63        yield (stationElement.get('{http://www.opengis.net/gml}id'), stationNameElement.text, lon, lat)
64
65# --------------------------------------------------------------------------------------------------------------------------
66
67url = 'http://bond.badc.rl.ac.uk:8089/dummyGeoServer/GetStationCSMLFeatures?gml_id=MIDAS_Stations'
68textGml = wget(url)
69print 'Download finished.'
70
71balloonTemplate = '<img src="http://bond.badc.rl.ac.uk:8089/csmlGrapher/plot?feature_id=$[feature_id]">'
72basicKmlStyle = getBasicKMLStyle(balloonTemplate)
73kmlDocument = KMLDocument('MIDAS stations (example)', basicKmlStyle)
74
75for (gml_id, name, lon, lat) in parseGMLStationsCollection(textGml):
76    print 'Station %s ("%s"): @ (%f,%f)' %  (gml_id, name, lon, lat)
77    kmlDocument.append( buildPlacemarkElement(gml_id, name, lon, lat) )
78
79kmlDocument.save('./../../output/exampleps_stations.kml')
Note: See TracBrowser for help on using the repository browser.