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

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

fixed problems with elementtree namespaces when scanning

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