source: TI02-CSML/trunk/csml/API/csmlContainer.py @ 2387

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

storage descriptor separated from feature collection

Line 
1import csml
2
3class Container(object):
4    '''CSMLContainer object is used to hold features before generating a CSML document. This is useful because it allows you to subset several features sequentially then build a single CSML document containing all the subsetted features '''
5   
6    def __init__(self,csmlpath='myfile.xml',docID='none',metadata='unspecified'):
7        self.csmlpath=csmlpath
8        ''' Initialise CSML Dataset and FeatureCollection and populate with basic Metadata'''
9        '''
10        docID = gml:id of Dataset element
11        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        #Set attributes of dataset
17        #if 'metadata' is a URI, set it as href attribute if MetaDataProperty instance,
18        #else set it as a text attribute of a MetaDataProperty instance.
19        #commented out temporarily for now (V2 rewrite)
20        #TODO
21        #mdp=csml.parser.MetaDataProperty()
22        #if csml.parser_extra.isURI(metadata):
23            #mdp.href=metadata
24        #else:
25            #mdp.text = [metadata]
26        #self.ds.metaDataProperty= [mdp]
27        #set the id
28        self.ds.id=docID
29        self.dm=[] #empty list to hold definition members (e.g. UnitDefinitions)
30        self.fm=[] #empty list to hold feature members (e.g. GridFeatures, PointFeatures)
31        self.sd=[] #empty list to hold storage descriptors (e.g. NetCDFExtract)
32        #the dataset is now initialised. now you can add features/defintions/arraydescriptors
33        #using the methods supplied.
34        #when finished building CSML objects call the getContents method to return the document.       
35        '''containerContents contains the CSML Document (first item) and then any NetCDF files that are to be delivered with the document '''
36        self.containerContents=[]
37        self.ncfiles=[]
38   
39    def appendFileExtract(self, fileextract):
40        ''' Append File Extract to Dataset '''
41        #append to Dataset instance
42        pass
43   
44    def appendFeature(self, feature):
45        ''' Append feature to FeatureCollection'''
46        #append to FeatureCollection instance
47        self.fm.append(feature)
48   
49    def attachNetCDFFile(self, ncfile):
50        ''' Attach a NetCDF file to the container object. Multiple NetCDF files can be referenced from multiple features and returned with the CSML document'''
51        #append NetCDF file path to containerContents
52        self.ncfiles.append(ncfile)
53   
54    def attachDescriptor(self, descriptor):
55        self.sd.append(descriptor)
56
57    def add(self, featureplusfileplusdescriptor):
58        #featureplusfileplusdescriptor should be a feature, and a filepath to the netcdf file, and a storage descriptor
59        self.appendFeature(featureplusfileplusdescriptor[0])
60        self.attachNetCDFFile(featureplusfileplusdescriptor[1])
61        self.attachDescriptor(featureplusfileplusdescriptor[2])
62       
63   
64    def getContents(self):
65        self.ds.storageDescriptor=csml.parser.CSMLStorageDescriptor(descriptors=self.sd)
66        self.ds.featureCollection=csml.parser.CSMLFeatureCollection(featureMembers=self.fm)
67        self.ds.featureCollection.id=csml.csmllibs.csmlextra.getRandomID()
68        csmlout=self.ds.toXML()
69        #parse and pretty print the result to file
70        strCSML=csml.parser_extra.PrettyPrint(csmlout)
71        strCSML=csml.parser_extra.removeInlineNS(strCSML)
72        f=open(self.csmlpath,'w')
73        f.write(strCSML)
74        f.close()
75        self.containerContents.append(self.csmlpath)
76        for ncfile in self.ncfiles:
77            self.containerContents.append(ncfile)
78        return self.containerContents
Note: See TracBrowser for help on using the repository browser.