source: TI02-CSML/trunk/csml/csmllibs/csmlbuilder.py @ 3286

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

overhaul of scanning command args

Line 
1import csml
2import sys
3
4class csmlBuilder(object):
5    '''this class contains all the method calls to create and populate a csml document. Calling the build method will start the build process
6    it needs to be provided with several bits of information to be able to do this. Csmlscan uses this class to run the build process.'''
7    def __init__(self,datasetid, directory,selection,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage, minaxes):
8        ''' The entry point for building a csml document.
9        @param datasetid:   identifier for the Dataset id attributes   
10        @param directory:   directory in which to start scanning
11        @param selection:   individual files paths to scan (do not need this and a directory)
12        @param csmlft:    name of the feature type
13        @param mapping:     name of the feature to file mapping (e.g. 'onetomany')
14        @param timedimension:    name of the time dimension in the data to be scanned
15        @param outputfile:    name of the csml outputfile
16        @param printscreen:    boolean - whether to print to std out or not
17        @param timestorage:     whether to store times inline ('inline') or in file extracts ('fileextract')
18        @param spatialstorage:    whether to store spatial domain  inline ('inline') or in file extracts ('fileextract')
19        @param valuestorage:    whether to store rangeset inline ('inline') or in file extracts ('fileextract')
20        @param minaxes:    the minimum dimensionality of features to be found.
21        @param secallow:    security information 
22        @param secdeny:    security information
23        '''
24       
25        if datasetid is not None:
26            self.datasetid = datasetid
27        else:
28            self.datasetid =csml.csmllibs.csmlextra.getRandomID()
29        if selection:
30            if type(selection) is list:
31                self.selection=selection
32            else:
33                self.selection=selection.split()
34            self.directory=self.selection[0]
35        else:
36            self.directory = directory # top level directory containing data files   
37        self.selection
38        self.csmlfeaturetype  = csmlft
39        self.mapping = mapping
40        self.timedimension=timedimension
41        self.outputfile=outputfile
42        self.printscreen=printscreen
43        self.timestorage=timestorage
44        self.spatialstorage=spatialstorage
45        self.valuestorage=valuestorage
46        self.minaxes=minaxes
47        self.csml = None  # csml object holds the csml document in memory.
48        self.ds_element = None #<Dataset> tag, root node.
49         
50    def build(self):
51        ''' Wrapper method to call methods in correct order. Controls the scanning process'''
52        self.createDataset()
53        try:
54            self.addGMLMetadata()
55        except TypeError:
56            #this is caused by an incorrect directory name
57            print 'error: Cannot find a config file at this location.'
58            sys.exit()
59        #self.addSecurity()
60        self.createFeatureFileMap()
61        if self.spatialstorage=='fileextract':
62           self.makeFileExtracts()
63        self.createFeatureCollection()
64        self.createFeatures()
65        self.insertXlinks()
66        try:
67            self.insertBoundingBoxes()
68        except:
69            print 'warning, scanner could not calculate bounding boxes'
70        self.saveFile()
71        self.printToScreen()
72
73    def createDataset(self):
74        '''Creates empty CSML Dataset object with standard attributes'''
75             
76        ########### The Dataset  ##############
77        #Create an Empty Dataset
78        self.ds = csml.parser.Dataset()
79        #Use random id for dataset id.
80        setattr(self.ds,'id',self.datasetid)       
81        setattr(self.ds, 'name', csml.parser.csString('NAME OF DATASET GOES HERE'))
82        ########################################
83
84    def addGMLMetadata(self):
85        ''' Adds main GML Metadata elements to dataset element '''
86       
87        #Note: TODO: The Parser has a problem with gml:Name from AbstractGML.
88        strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory
89        strCodespace = 'http://ndg/nerc/ac/uk/NDGData'
90        strGmlDescription = 'Dataset for directory ' + self.directory
91       
92        #TODO for V2
93        #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)]
94        #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory])
95        #self.ds.description=csml.parser.Description(strGmlDescription)
96   
97    #def addSecurity(self):       
98        #'''Adds NDG style AccessControlPolicy to the dataset based on info in config file'''
99        #def _buildSimpleCondition(condition):
100            ##builds the tag structure for a SimpleCondition
101            #scond=csml.parser.SimpleCondition()
102            #parts=condition.split(' ')
103            #attauth=csml.parser.csString(parts[0])
104            #role=csml.parser.csString(parts[1])
105            #scond.attrauthRole=role
106            #scond.dgAttributeAuthority=attauth
107            #return scond
108       
109        ##don't do anything if no config options are set.
110        #if not self.secallow:
111            #if not self.secdeny:
112                #return None
113           
114       
115        #acp=csml.parser.AccessControlPolicy()
116        #if self.secallow:
117            #for condition in self.secallow.split(','):
118                #dgSecurityCondition=csml.parser.DgSecurityCondition()
119                #dgSecurityCondition.effect=csml.parser.csString('allow')
120                #dgSecurityCondition.addChildElem('simpleCondition', _buildSimpleCondition(condition))
121                #acp.addChildElem('dgSecurityCondition', dgSecurityCondition)
122        #if self.secdeny:
123            #for condition in self.secallow.split(','):
124                #dgSecurityCondition=csml.parser.DgSecurityCondition()
125                #dgSecurityCondition.effect=csml.parser.csString('deny')
126                #dgSecurityCondition.addChildElem('simpleCondition', _buildSimpleCondition(condition))
127                #acp.addChildElem('dgSecurityCondition', dgSecurityCondition)
128        #self.ds.accessControlPolicy=acp       
129       
130    def createFeatureFileMap(self):
131        ''' creates the  FeatureFileMap'''
132        print "Creating feature to file mapping..."
133        if self.selection is not None:
134            #create a 'virtual' directory object that behaves like a normal directory.
135            directorytree=csml.csmllibs.csmldirectory.VirtualDirectoryTree(self.selection)
136        else:
137            directorytree=csml.csmllibs.csmldirectory.DirectoryTree(self.directory)
138        fmm = csml.csmllibs.csmlfiles.FileMapMaker(directorytree, self.csmlfeaturetype)
139        if self.mapping=='onetomany':
140            self.ffmap = fmm.onetomany()
141        elif self.mapping=='onetoseveral':
142            self.ffmap = fmm.onetoseveral()
143        elif self.mapping=='onetoone':
144            self.ffmap = fmm.onetoone()
145        elif self.mapping=='oneonly':
146            self.ffmap = fmm.oneonly()
147       
148    def makeFileExtracts(self):
149        ''' creates the file extracts'''
150        print "Making file extract dictionary..."
151        feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
152        self.ds = feBuilder.dataset_element
153        self.extractType = feBuilder.extractType
154        self.extractDictionary=feBuilder.fileExtractDictionary
155
156   
157    def createFeatureCollection(self):
158        ''' creates empty feature collection (list)'''
159        self.featureCollection = csml.parser.CSMLFeatureCollection()
160 
161    def createFeatures(self):
162        ''' creates the features'''
163        print "Creating features..."
164        if not hasattr(self, 'extractType'):
165            self.extractType=None
166        if not hasattr(self, 'extractDictionary'):
167            self.extractDictionary=None
168        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage, self.minaxes)
169               
170        #self.csml = thefeatures.csml
171        self.ds=thefeatures.ds_element
172        self.featureCollection = thefeatures.featureCollection
173        #try:
174            #self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
175        #except:
176            #pass
177        setattr(self.featureCollection, 'id', csml.csmllibs.csmlextra.getRandomID())
178       
179        self.ds.featureCollection=self.featureCollection
180               
181
182          #addEnvelope needs to work for different feature types
183        self.ds.featureCollection=self.featureCollection
184
185                   
186    def insertXlinks(self):
187        ''' creates xlinks '''
188        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
189       
190    def insertBoundingBoxes(self):
191        ''' creates bounding boxes '''
192        self.ds.featureCollection =csml.csmllibs.csmlextra.addBoundingBoxes(self.ds) 
193        self.ds.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection) 
194       
195    def saveFile(self):
196        ''' saves the csml file'''
197        print "Creating CSML document and saving file"
198        #call the toXML method of the Dataset object:
199        csmlf = self.ds.toXML()
200        #parse and pretty print csml
201       
202        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
203        #tidy up elementtree style namespaces
204        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
205        #open file, write to it.
206        f = open(self.outputfile, "w")
207        f.write('<?xml version="1.0" encoding="UTF-8"?>')
208        f.write(self.strCSML)
209        f.close()
210
211    def printToScreen(self):
212        ''' prints the finished csml'''
213        if self.printscreen:
214            print self.strCSML
215
Note: See TracBrowser for help on using the repository browser.