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

RevLine 
[2385]1import csml
[1521]2
3class Container(object):
[1518]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   
[1523]6    def __init__(self,csmlpath='myfile.xml',docID='none',metadata='unspecified'):
7        self.csmlpath=csmlpath
[1518]8        ''' Initialise CSML Dataset and FeatureCollection and populate with basic Metadata'''
[1521]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
[1523]17        #if 'metadata' is a URI, set it as href attribute if MetaDataProperty instance,
[1521]18        #else set it as a text attribute of a MetaDataProperty instance.
[1902]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]
[1521]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)
[2061]31        self.sd=[] #empty list to hold storage descriptors (e.g. NetCDFExtract)
[1521]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.       
[1518]35        '''containerContents contains the CSML Document (first item) and then any NetCDF files that are to be delivered with the document '''
[1521]36        self.containerContents=[]
[1523]37        self.ncfiles=[]
[1518]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
[1521]47        self.fm.append(feature)
[1518]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'''
[1557]51        #append NetCDF file path to containerContents
[1523]52        self.ncfiles.append(ncfile)
[2387]53   
54    def attachDescriptor(self, descriptor):
55        self.sd.append(descriptor)
[1555]56
[2387]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])
[1555]62       
[1557]63   
[1518]64    def getContents(self):
[2387]65        self.ds.storageDescriptor=csml.parser.CSMLStorageDescriptor(descriptors=self.sd)
[2061]66        self.ds.featureCollection=csml.parser.CSMLFeatureCollection(featureMembers=self.fm)
[2385]67        self.ds.featureCollection.id=csml.csmllibs.csmlextra.getRandomID()
[1521]68        csmlout=self.ds.toXML()
[1588]69        #parse and pretty print the result to file
[1521]70        strCSML=csml.parser_extra.PrettyPrint(csmlout)
71        strCSML=csml.parser_extra.removeInlineNS(strCSML)
[1588]72        f=open(self.csmlpath,'w')
73        f.write(strCSML)
74        f.close()
75        self.containerContents.append(self.csmlpath)
[1523]76        for ncfile in self.ncfiles:
77            self.containerContents.append(ncfile)
[1521]78        return self.containerContents
Note: See TracBrowser for help on using the repository browser.