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

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

bounding box working for profileseries

RevLine 
[1521]1import csml.parser
2import csml.parser_extra
3
4class Container(object):
[1518]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   
[1523]7    def __init__(self,csmlpath='myfile.xml',docID='none',metadata='unspecified'):
8        self.csmlpath=csmlpath
[1518]9        ''' Initialise CSML Dataset and FeatureCollection and populate with basic Metadata'''
[1521]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        #Set attributes of dataset
[1523]18        #if 'metadata' is a URI, set it as href attribute if MetaDataProperty instance,
[1521]19        #else set it as a text attribute of a MetaDataProperty instance.
[1902]20        #commented out temporarily for now (V2 rewrite)
21        #TODO
22        #mdp=csml.parser.MetaDataProperty()
23        #if csml.parser_extra.isURI(metadata):
24            #mdp.href=metadata
25        #else:
26            #mdp.text = [metadata]
27        #self.ds.metaDataProperty= [mdp]
[1521]28        #set the id
29        self.ds.id=docID
30        self.dm=[] #empty list to hold definition members (e.g. UnitDefinitions)
31        self.fm=[] #empty list to hold feature members (e.g. GridFeatures, PointFeatures)
[2061]32        self.sd=[] #empty list to hold storage descriptors (e.g. NetCDFExtract)
[1521]33        #the dataset is now initialised. now you can add features/defintions/arraydescriptors
34        #using the methods supplied.
35        #when finished building CSML objects call the getContents method to return the document.       
[1518]36        '''containerContents contains the CSML Document (first item) and then any NetCDF files that are to be delivered with the document '''
[1521]37        self.containerContents=[]
[1523]38        self.ncfiles=[]
[1518]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
[1521]48        self.fm.append(feature)
[1518]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'''
[1557]52        #append NetCDF file path to containerContents
[1523]53        self.ncfiles.append(ncfile)
[1555]54
[1557]55    def add(self, featureplusfile):
56        #featureplusfile should be a feature, and a filepath to the netcdf file
57        self.appendFeature(featureplusfile[0])
58        self.attachNetCDFFile(featureplusfile[1])
[1555]59       
[1557]60   
[1518]61    def getContents(self):
[2061]62        self.ds.storageDescriptor=self.sd
63        self.ds.featureCollection=csml.parser.CSMLFeatureCollection(featureMembers=self.fm)
[1521]64        csmlout=self.ds.toXML()
[1588]65        #parse and pretty print the result to file
[1521]66        strCSML=csml.parser_extra.PrettyPrint(csmlout)
67        strCSML=csml.parser_extra.removeInlineNS(strCSML)
[1588]68        f=open(self.csmlpath,'w')
69        f.write(strCSML)
70        f.close()
71        self.containerContents.append(self.csmlpath)
[1523]72        for ncfile in self.ncfiles:
73            self.containerContents.append(ncfile)
[1521]74        return self.containerContents
Note: See TracBrowser for help on using the repository browser.