source: TI02-CSML/trunk/csml/csmllibs/csmldocument.py @ 3279

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/csmllibs/csmldocument.py@3279
Revision 3279, 5.6 KB checked in by domlowe, 13 years ago (diff)

docstrings

Line 
1''' CSMLDocument.py - simple implementation to return a CSML document containing a single type of feature'''
2
3import csml.parser
4import csml.parser_extra
5
6class CSMLDocument(object):
7    ''' Represents a CSML document '''
8    def __init__(self,docID, docMetaDataProperty):
9        '''
10        @param docID:   gml:id of Dataset element
11        @param docMetaDataProperty:    gml:metaDataProperty of Dataset element  #hmm may need to allow a list here.
12        '''
13        #initialise a new document
14        #Create an Empty Dataset & FeatureCollection
15        self.ds = csml.parser.Dataset()
16        self.fc=csml.parser.FeatureCollection()
17        #Set attributes of dataset
18        #if docMetaDataProperty is a URI, set it as href attribute if MetaDataProperty instance,
19        #else set it as a text attribute of a MetaDataProperty instance.
20        mdp=csml.parser.MetaDataProperty()
21        if csml.parser_extra.isURI(docMetaDataProperty):
22            mdp.href=docMetaDataProperty
23        else: 
24            mdp.text = [docMetaDataProperty]
25        self.ds.metaDataProperty= [mdp]
26        #set the id
27        self.ds.id=docID
28        self.dm=[] #empty list to hold definition members (e.g. UnitDefinitions)
29        self.fm=[] #empty list to hold feature members (e.g. GridFeatures, PointFeatures)
30        self.ad=[] #empty list to hold array descriptors (e.g. NetCDFExtract)
31        #the dataset is now initialised. now you can add features/defintions/arraydescriptors
32        #using the methods supplied.
33        #when finished building CSML objects call the consolidate method to return the document.       
34       
35       #separate build classes for each feature type
36    def addPointFeature(self, id, description,domainReference,rangeSet, parameter):
37        """ addPointFeature
38        domainReference = Position type
39        rangeSet = quantityList type,
40        parameter = Phenomenon type
41        """
42        #TO DO- this needs revising in line with addGridSeriesFeature
43        feat=csml.parser.PointFeature()
44        feat.id=id
45        feat.description=csml.parser.Description(description)
46       
47        #Add the domain to the feature
48        p=csml.parser.Position()
49        p.srsName =domainReference[0]
50        p.axisLabels=domainReference[1]
51        p.uomLabels = domainReference[2]
52        p.location=domainReference[3]
53        p.time=domainReference[4]
54        ptd=csml.parser.PointDomain()
55        ptd.domainReference=p
56        feat.domain=ptd
57       
58        #Add the rangeSet to the feature
59        rs=csml.parser.RangeSet()
60        rs.quantityList=csml.parser.MeasureOrNullList(uom=rangeSet[0],val = rangeSet[1])
61        feat.rangeSet=rs
62       
63        #Add the parameter to the feature
64        feat.parameter=csml.parser.Phenomenon(href=parameter) 
65       
66        self.fm.append(feat)
67    def addProfileFeature(self):
68        pass
69
70    def addGridFeature(self):
71        pass
72           
73    def addPointSeriesFeature(self):
74        pass
75           
76    def addProfileSeriesFeature(self):
77        pass
78
79    def addGridSeriesFeature(self,domain,rangeSet,datasetID=None,featureID=None,description=None):
80        ''' add GridSeriesFeature to document '''
81        fms=[] #empty featureMembers list
82        feat=csml.parser.GridSeriesFeature()
83        if featureID:
84            feat.id= featureID
85        else:
86            feat.id='No ID'
87        if description:
88            feat.description=csml.parser.Description(description)
89        else:
90            feat.description=csml.parser.Description('No Description')
91        feat.domain=domain
92        feat.rangeSet=rangeSet
93        self.fm.append(feat)
94                       
95    def addTrajectoryFeature(self):
96        pass
97
98    def addFileExtract(self,extractType, id, variableName=None,arraySize=None,fileName=None):
99        #handles the addition of file extract classes (can probably get away with one method for all types of extracts for now)
100        if extractType=='NetCDFExtract':
101            ex=csml.parser.NetCDFExtract()
102        if extractType=='NASAAmesExtract':
103            ex=csml.parser.NASAAmesExtract() 
104        if extractType=='GRIBExtract':
105            ex=csml.parser.GRIBExtract() 
106        if extractType=='PPExtract':
107            ex=csml.parser.PPExtract() 
108        ex.id =id
109        if arraySize:
110            ex.arraySize=[arraySize]
111        if fileName:
112            ex.fileName=fileName
113        if variableName:
114            ex.variableName=variableName
115        self.ad.append(ex)
116
117
118    def removeFileExtract(self):
119        #not implemented yet
120        pass
121       
122    def removeFeature(self,featureID):
123        #not implemented yet.
124        pass
125    def addDefintion(self):
126        #not implemented yet. - may need separate methods for different defintion types?
127        pass
128   
129    def removeDefintion(self):
130        #not implemented yet.
131        pass
132   
133    def setBoundingEnvelope(self,lowerCorner,upperCorner,timePosition1,timePosition2):
134        #set the bounding box envelope of the feature collection.
135        etp = csml.parser.EnvelopeWithTimePeriod()
136        etp.lowerCorner=lowerCorner
137        etp.upperCorner=upperCorner
138        etp.timePosition=timePosition1
139        etp.timePosition2=timePosition2
140        self.fc.boundedBy=etp
141
142       
143       
144    def consolidate(self):
145        #when you have finished building the document, need to consolidate it and return a CSML document (string)
146        self.ds.arrayDescriptors=self.ad
147        self.ds.featureCollection=csml.parser.FeatureCollection(members=self.fm)
148        csmlout=self.ds.toXML()
149        #parse and pretty print the result
150        strCSML=csml.parser_extra.PrettyPrint(csmlout)
151        strCSML=csml.parser_extra.removeInlineNS(strCSML)
152        return strCSML
Note: See TracBrowser for help on using the repository browser.