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

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

some changes to allow cdml to be used as data source, not working properly yet though

Line 
1import csml.parser
2import csml.parser_extra
3
4class Container(object):
5    '''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 '''
6   
7    def __init__(self,csmlpath='myfile.xml',docID='none',metadata='unspecified'):
8        self.csmlpath=csmlpath
9        ''' Initialise CSML Dataset and FeatureCollection and populate with basic Metadata'''
10        '''
11        docID = gml:id of Dataset element
12        docMetaDataProperty = gml:metaDataProperty of Dataset element  #hmm may need to allow a list here.
13        '''
14        #initialise a new document
15        #Create an Empty Dataset & FeatureCollection
16        self.ds = csml.parser.Dataset()
17        self.fc=csml.parser.FeatureCollection()
18        #Set attributes of dataset
19        #if 'metadata' is a URI, set it as href attribute if MetaDataProperty instance,
20        #else set it as a text attribute of a MetaDataProperty instance.
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.ad=[] #empty list to hold array 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
36        '''containerContents contains the CSML Document (first item) and then any NetCDF files that are to be delivered with the document '''
37        self.containerContents=[]
38        self.ncfiles=[]
39   
40    def appendFileExtract(self, fileextract):
41        ''' Append File Extract to Dataset '''
42        #append to Dataset instance
43        pass
44   
45    def appendFeature(self, feature):
46        ''' Append feature to FeatureCollection'''
47        #append to FeatureCollection instance
48        self.fm.append(feature)
49   
50    def attachNetCDFFile(self, ncfile):
51        ''' Attach a NetCDF file to the container object. Multiple NetCDF files can be referenced from multiple features and returned with the CSML document'''
52        #append NetCDF file object to containerContents
53        #need to think about whether it is best to append the file object or just a path to the file
54        self.ncfiles.append(ncfile)
55
56       
57    def getContents(self):
58        self.ds.arrayDescriptors=self.ad
59        self.ds.featureCollection=csml.parser.FeatureCollection(members=self.fm)
60        print self.fm
61        csmlout=self.ds.toXML()
62        #parse and pretty print the result
63        strCSML=csml.parser_extra.PrettyPrint(csmlout)
64        strCSML=csml.parser_extra.removeInlineNS(strCSML)
65        self.containerContents.append(strCSML)
66        for ncfile in self.ncfiles:
67            self.containerContents.append(ncfile)
68        return self.containerContents
Note: See TracBrowser for help on using the repository browser.