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

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

Added logical years to the prototype. Now all first decade of the 20th Century is in year 1900, the next in year 1901 etc.

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    break
92   
93#Now attach the Document element as a subelement of the root 'kml' element
94kml_root=Element('kml', xmlns='http://earth.google.com/kml/2.2')
95kml_root.append(kml_document)
96
97#which could be written to a file or served from a url
98indent(kml_root, 0)
99tree = ElementTree(kml_root)
100print 'This is the output KML:'
101outputfile='gstest.kml'
102f=open(outputfile, 'w')
103tree.write(f)
104f.close()
105print 'Written to %s'%outputfile
106
107
Note: See TracBrowser for help on using the repository browser.