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

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

storage descriptor code working in scanner

Line 
1import csml
2
3#Class for building the csml document.
4class csmlBuilder(object):
5    #this class contains all the method calls to create and populate a csmldoc
6    #it needs to be provided with several bits of information to be able to do this.
7    def __init__(self,directory,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage):
8        self.directory = directory # top level directory containing data files
9        self.csmlfeaturetype  = csmlft
10        self.mapping = mapping
11        self.timedimension=timedimension
12        self.outputfile=outputfile
13        self.printscreen=printscreen
14        self.timestorage=timestorage
15        self.spatialstorage=spatialstorage
16        self.valuestorage=valuestorage
17        self.csml = None  # csml object holds the csml document in memory.
18        self.ds_element = None #<Dataset> tag, root node.
19         
20    def build(self):
21        #wrapper method to call methods in correct order.
22        self.createDataset()
23        self.addGMLMetadata()
24        self.createFeatureFileMap()
25        if self.spatialstorage=='fileextract':
26           self.makeFileExtracts()
27        self.createFeatureCollection()
28        self.createFeatures()
29        #self.insertXlinks()
30        #self.closeFC()
31        #self.closeDS()
32        self.saveFile()
33        self.printToScreen()
34
35    def createDataset(self):
36        #*************************************************************************************
37        #Creates empty CSML Dataset object with standard attributes
38        #*************************************************************************************
39             
40        ########### The Dataset  ##############
41        #Create an Empty Dataset
42        self.ds = csml.parser.Dataset()
43        #Set gml:id attribute of dataset to be equal to the directory name. (may want to rethink this later)
44        setattr(self.ds,'id',self.directory)
45        #setattr(self.ds, 'arrayDescriptors', adlist)
46        #setattr(self.ds, 'unitDefinitions', uds)
47        #setattr(self.ds, 'phenomenonDefinitions', pds)
48        #setattr(self.ds, 'featureCollection',fc)
49        ########################################
50
51    def addGMLMetadata(self):
52        #*********************************************************************************
53        # Adds main GML Metadata elements to dataset element
54        #*********************************************************************************
55        #Note: TODO: The Parser has a problem with gml:Name from AbstractGML.
56        strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory
57        strCodespace = 'http://ndg/nerc/ac/uk/NDGData'
58        strGmlDescription = 'Dataset for directory ' + self.directory
59       
60        #TODO for V2
61        #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)]
62        #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory])
63        #self.ds.description=csml.parser.Description(strGmlDescription)
64   
65    def createCSML(self):
66        print "Create CSML"
67        #create an empty csml document and dataset tag
68        self.csml, self.ds_element = csml.csmllibs.csmlmeta.createDataset()
69        #add gml metadata elements
70        self.csml, self.ds_element = csml.csmllibs.csmlmeta.addGMLMetadata(self.csml, self.directory, self.ds_element)
71
72    def createFeatureFileMap(self):
73        print "Create FFMAP"
74         #    print "creating filemap"
75        fmm = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
76        if self.mapping=='onetomany':
77            self.ffmap = fmm.onetomany()
78        elif self.mapping=='onetoone':
79            self.ffmap = fmm.onetoone()
80        elif self.mapping=='oneonly':
81            self.ffmap = fmm.oneonly()
82       
83    def makeFileExtracts(self):
84        print "make FEs"
85        feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
86        self.ds = feBuilder.dataset_element
87        self.extractType = feBuilder.extractType
88        self.extractDictionary=feBuilder.fileExtractDictionary
89
90    def createFeatureCollection(self):
91        #creates empty feature collection (list)
92        self.featureCollection = csml.parser.CSMLFeatureCollection()
93 
94    def createFeatures(self):
95        print "create Features"
96        if not hasattr(self, 'extractType'):
97            self.extractType=None
98        if not hasattr(self, 'extractDictionary'):
99            self.extractDictionary=None
100        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage)
101               
102        #self.csml = thefeatures.csml
103        self.ds=thefeatures.ds_element
104        self.featureCollection = thefeatures.featureCollection
105        self.ds.featureCollection=self.featureCollection
106
107                   
108    def insertXlinks(self):
109        self.ds.featureCollection=csml.csmllibs.csmlxlink.createXlinks(self.ds.featureCollection)
110       
111       
112    def saveFile(self):
113        print "Creating CSML document and saving file"
114        #call the toXML method of the Dataset object:
115        csmlf = self.ds.toXML()
116        #parse and pretty print csml
117       
118        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
119        #tidy up elementtree style namespaces
120        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
121        #open file, write to it.
122        f = open(self.outputfile, "w")
123        f.write('<?xml version="1.0" encoding="UTF-8"?>')
124        f.write(self.strCSML)
125        f.close()
126
127    def printToScreen(self):
128        if self.printscreen=='1':
129            print self.strCSML
130
Note: See TracBrowser for help on using the repository browser.