source: TI02-CSML/trunk/parser/CSMLdocBuilder.py @ 1283

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/parser/CSMLdocBuilder.py@1283
Revision 1283, 5.1 KB checked in by domlowe, 14 years ago (diff)

More on CSMLdocBuilder class

Line 
1#!/usr/bin/env python
2
3"""The class CSMLdocBuild handles the construction of a CSMLdocument.at a higher level than direct parser interaction
4"""
5
6from cElementTree import *
7from Parser import *
8import parser_extra
9import string
10
11
12def isURI(uri):
13    """ a very simple function to test if a string is a uri
14    if ;// appears in the first 12 characters it is probably a uri """
15    #TODO - a decent uri check!   
16    result = False
17    if string.find(uri[:12], '://') != -1:
18        result = True
19    return result
20
21
22  ######################################################
23## The class CSMLdocBuild handles the construction of a CSMLdocument.at a higher level than direct parser interaction.
24  ######################################################
25
26class CSMLdocBuild:
27    def __init__(self,docID, docMetaDataProperty):
28        '''
29        docID = gml:id of Dataset element
30        docMetaDataProperty = gml:metaDataProperty of Dataset element  #hmm may need to allow a list here.
31        '''
32        #initialise a new document
33        #Create an Empty Dataset & FeatureCollection
34        self.ds = Dataset()
35        self.fc=FeatureCollection()
36        #Set attributes of dataset
37        #if docMetaDataProperty is a URI, set it as href attribute if MetaDataProperty instance,
38        #else set it as a text attribute of a MetaDataProperty instance.
39        mdp=MetaDataProperty()
40        if isURI(docMetaDataProperty):
41            mdp.href=docMetaDataProperty
42        else: 
43            mdp.text = docMetaDataProperty
44        self.ds.metaDataProperty= [mdp]
45        #set the id
46        self.ds.id=docID
47        self.dm=[] #empty list to hold definition members (e.g. UnitDefinitions)
48        self.fm=[] #empty list to hold feature members (e.g. GridFeatures, PointFeatures)
49        self.ad=[] #empty list to hold array descriptors (e.g. NetCDFExtract)
50        #the dataset is now initialised. now you can add features/defintions/arraydescriptors
51        #using the methods supplied.
52        #when finished building CSML objects call the consolidate method to return the document.       
53       
54    #separate build classes for each feature type
55    def addPointFeature(self):
56        pass
57        return feat
58           
59    def addProfileFeature(self):
60        pass
61        return feat
62
63    def addGridFeature(self):
64        pass
65        return feat
66           
67    def addPointSeriesFeature(self):
68        pass
69        return feat
70           
71    def addProfileSeriesFeature(self):
72        pass
73        return feat
74
75    def addGridSeriesFeature(self):
76        pass
77        return feat
78                       
79    def addTrajectoryFeature(self):
80        pass
81        return feat
82
83    def addFileExtract(self,extractType, id, variableName=None,arraySize=None,fileName=None):
84        #handles the addition of file extract classes (can probably get away with one method for all types of extracts for now)
85        if extractType=='NetCDFExtract':
86            ex=NetCDFExtract()
87        if extractType=='NASAAmesExtract':
88            ex=NASAAmesExtract() 
89        if extractType=='GRIBExtract':
90            ex=GRIBExtract() 
91        if extractType=='PPExtract':
92            ex=PPExtract() 
93        ex.id =id
94        if arraySize:
95            ex.arraySize=[arraySize]
96        if fileName:
97            ex.fileName=fileName
98        if variableName:
99            ex.variableName=variableName
100        self.ad.append(ex)
101
102
103    def removeFileExtract(self):
104        #not implemented yet
105        pass
106       
107    def removeFeature(self,featureID):
108        #not implemented yet.
109        pass
110    def addDefintion(self):
111        #not implemented yet. - may need separate methods for different defintion types?
112        pass
113   
114    def removeDefintion(self):
115        #not implemented yet.
116        pass
117   
118    def setBoundingEnvelope(self,lowerCorner,upperCorner,timePosition1,timePosition2):
119        #set the bounding box envelope of the feature collection.
120        etp = EnvelopeWithTimePeriod()
121        etp.lowerCorner=lowerCorner
122        etp.upperCorner=upperCorner
123        etp.timePosition=timePosition1
124        etp.timePosition2=timePosition2
125        self.fc.boundedBy=etp
126        pass
127   
128    def consolidate(self):
129        #when you have finished building the document, need to consolidate it and return a CSML document (string)
130        self.ds.arrayDescriptors=self.ad
131        csml=self.ds.toXML()
132        #parse and pretty print the result
133        strCSML=parser_extra.PrettyPrint(csml)
134        strCSML=parser_extra.removeInlineNS(strCSML)
135        return strCSML
136
137   
138#test building a CSML document
139def main():
140    csml=CSMLdocBuild('mydoc', 'http://mycsmldoc.com')
141    csml.addFileExtract('NetCDFExtract', 'myNCextract', variableName='myVar',arraySize=30,  fileName='myfile.nc')
142    csml.addFileExtract('NASAAmesExtract', 'myNAextract', variableName='myVar2',arraySize=40,  fileName='myfile2.nc')
143    #csml.addGridSeriesFeature()
144    #csml.addGridSeriesFeature()
145    #csml.addPointFeature()
146    #csml.addDefintion()
147    strCSML=csml.consolidate()
148    print strCSML
149
150main()
Note: See TracBrowser for help on using the repository browser.