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

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

adding kml code

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