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

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

more changes related to module reorganisation

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