source: TI02-CSML/trunk/Scanner/csmllibs/csmlbuilder.py @ 847

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

fixed whitespace issues, namespace issues

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