source: DPPP/kml/csml2kml/python/buildGridSeries.py @ 3187

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/buildGridSeries.py@3187
Revision 3187, 3.8 KB checked in by mkochan, 13 years ago (diff)

Prototype: Changed dataset folders in KML to be closed & unloaded by default. Added multiple datasets to extraction.

Line 
1#Import the csml parser code
2import csml.parser
3import kmlfeatures
4from cElementTree import ElementTree, Element, SubElement, tostring
5from csmlwrappers import GSWrapper
6import os
7
8''' demo script to show how the csmlwrappers and kmlfeatures modules might be used. This example puts KML for all timesteps for all features from one CSML document into a single file. This may not be practical for large model runs (not tested)'''
9
10def indent(elem, level=0):
11    i = "\n" + level*"  "
12    if len(elem):
13        if not elem.text or not elem.text.strip():
14            elem.text = i + "  "
15        for child in elem:
16            indent(child, level+1)
17        if not child.tail or not child.tail.strip():
18            child.tail = i
19        if not elem.tail or not elem.tail.strip():
20            elem.tail = i
21    else:
22        if level and (not elem.tail or not elem.tail.strip()):
23            elem.tail = i
24
25#the name of the csml file (this is hard coded for demonstration purposes)
26filename='../testdata/clim_10.csml'
27
28#empty csml "Dataset" object
29ds=csml.parser.Dataset()
30
31#parse the CSML file into this object
32ds.parse(filename)
33
34#the ds object now contains a hierarchichal representation of the csml document
35#and can be queried e.g.
36print 'This is the ID of the CSMLFeatureCollection: %s'%ds.featureCollection.id
37print 'This is a list of the feature IDs and Descriptions:'
38for feature in ds.featureCollection.featureMembers:
39    print '%s:    %s'%(feature.id, feature.description.CONTENT)
40   
41#NOTE: in the csml parser, XML attributes are referred to as x.y whereas inline element content is referred to as x.CONTENT
42#So if the CSML looks like this:
43#   <x y="some_attribute">Some inline content</x>
44
45#The parser code looks like this:
46#   >>> x.y
47#   some_attribute
48
49#   >>> x.CONTENT
50#   Some inline content
51
52
53#Start with an empty KML document:
54kmldocname='TEST GridSeries KML'
55kml_document=Element('Document')
56SubElement(kml_document, 'name').text=kmldocname
57SubElement(kml_document, 'open').text='1'
58
59
60#Okay, now lets get the information that we need to build some kmlfeatures.GridSeries_KML objects
61#then for every GridSeries feature in the CSML file, create some KML!
62for feature in ds.featureCollection.featureMembers:
63    #create a GSWrapper instance - this has useful methods to get information from the feature
64    gsFeature=GSWrapper(feature) 
65       
66    #Now get the information from the CSML feature that's needed to send to the KML
67    description =  feature.description.CONTENT     
68    shortname = feature.name.CONTENT
69    bbox=gsFeature.getBBox()
70    timesteps=gsFeature.getTimeSteps()
71    timebounds=gsFeature.getTimeBounds()
72    print timebounds
73   
74    #wmsendpoint e.g. http://ndgbeta.badc.rl.ac.uk/wcs/badc.nerc.ac.uk__NDG-A0__AWQX8gTc?
75    wmsendpoint='tba'
76    #wmsendpoint e.g. 'clim_10/cld'
77    #is this consistent for all WMSs -- perhaps we need to provide this information as input parameter
78    wmslayer=os.path.split(filename)[1].split('.')[0]+ '/' + feature.name.CONTENT
79    #bounding box in list format: e.g.  (90, -90, 180, -180)    i.e. (north, south, east, west)
80   
81   
82    #Now instantiate a GridSeries_KML object with this information
83    gsKML=kmlfeatures.GridSeries_KML( description, shortname, wmsendpoint, wmslayer, bbox, timesteps,  timebounds)
84   
85    #Call the outputKML method of this object to generate a populated KML Folder element
86    kml_folder=gsKML.outputKML()   
87   
88    #and append the folder to the kml document.
89    kml_document.append(kml_folder)
90   
91#Now attach the Document element as a subelement of the root 'kml' element
92kml_root=Element('kml', xmlns='http://earth.google.com/kml/2.2')
93kml_root.append(kml_document)
94
95#which could be written to a file or served from a url
96indent(kml_root, 0)
97tree = ElementTree(kml_root)
98print 'This is the output KML:'
99outputfile='gstest.kml'
100f=open(outputfile, 'w')
101tree.write(f)
102f.close()
103print 'Written to %s'%outputfile
Note: See TracBrowser for help on using the repository browser.