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

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

Added new FeatureFileMap? option to Scanner. The option is called oneonly and means that the structure of just one file represents a feature spannign mulptiple directories.

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