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

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

Fixed problem with relative paths in CSML documents

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,outputdir, csmlname='myfile.xml',docID='none',metadata='unspecified'):
7        self.outputdir =outputdir
8        self.csmlname=csmlname
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        #Set attributes of dataset
18        #if 'metadata' is a URI, set it as href attribute if MetaDataProperty instance,
19        #else set it as a text attribute of a MetaDataProperty instance.
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]
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)
32        self.sd=[] #empty list to hold storage descriptors (e.g. NetCDFExtract)
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.       
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 path to containerContents
53        self.ncfiles.append(ncfile)
54   
55    def attachDescriptor(self, descriptor):
56        self.sd.append(descriptor)
57
58    def add(self, featureplusfileplusdescriptor):
59        #featureplusfileplusdescriptor should be a feature, and a filepath to the netcdf file, and a storage descriptor
60        self.appendFeature(featureplusfileplusdescriptor[0])
61        self.attachNetCDFFile(featureplusfileplusdescriptor[1])
62        self.attachDescriptor(featureplusfileplusdescriptor[2])
63       
64   
65    def getContents(self):
66        self.ds.storageDescriptor=csml.parser.CSMLStorageDescriptor(descriptors=self.sd)
67        self.ds.featureCollection=csml.parser.CSMLFeatureCollection(featureMembers=self.fm)
68        self.ds.featureCollection.id=csml.csmllibs.csmlextra.getRandomID()
69        csmlout=self.ds.toXML()
70        #parse and pretty print the result to file
71        strCSML=csml.parser_extra.PrettyPrint(csmlout)
72        strCSML=csml.parser_extra.removeInlineNS(strCSML)
73        csml.csmllibs.csmlextra.checkDirExists(self.outputdir)
74        filepath=self.outputdir + '/' + self.csmlname
75        f=open(filepath,'w')
76        f.write(strCSML)
77        f.close()
78        self.containerContents.append(self.csmlname)
79        for ncfile in self.ncfiles:
80            self.containerContents.append(ncfile)
81        return self.containerContents
Note: See TracBrowser for help on using the repository browser.