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

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

Added optional -p printscreen arg #2

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
86    def makeFileExtracts(self):
87        print "make FEs"
88        feBuilder =csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap)
89        #self.csml = feBuilder.csmldoc
90        self.ds = feBuilder.dataset_element
91        #todo, remember ds == dataset_element in this file
92        self.extractType = feBuilder.extractType
93        self.extractPrefix  = feBuilder.extractPrefix
94        self.extractDictionary=feBuilder.fileExtractDictionary
95
96    def createFeatureCollection(self):
97        #creates empty feature collection (list)
98        self.gml_FeatureCollection = csmllibs.Parser.FeatureCollection()
99 
100    def createFeatures(self):
101        print "create Features"
102        thefeatures = csmllibs.csmlfeaturetypes.featureBuilder( self.ds,self.gml_FeatureCollection,self.extractType, self.extractPrefix, self.ffmap, self.extractDictionary,self.timedimension)
103        #self.csml = thefeatures.csml
104        self.ds=thefeatures.ds_element
105        self.gml_FeatureCollection = thefeatures.gml_FeatureCollection_element
106        self.ds.featureCollection=self.gml_FeatureCollection
107
108                   
109    def closeFC(self):
110        print "close FC"
111        self.ds_element=csmllibs.csmlmeta.closeFeatureCollection(self.ds_element,self.gml_FeatureCollection)
112 
113    def closeFC(self):
114        print "close FC"
115        self.ds_element.appendChild(self.gml_FeatureCollection)
116       
117    def closeDS(self):
118        print "close DS"
119        #The DATASET element must be the last thing to be appended to the CSML document
120        #as it is the parent element of all the other elements
121        self.csml.appendChild(self.ds_element) 
122   
123
124
125
126    def saveFile(self):
127        print "Creating CSML document and saving file"
128        #call the toXML method of the Dataset object:
129        csml = self.ds.toXML()
130        #parse and pretty print csml
131        self.strCSML =csmllibs.parser_extra.PrettyPrint(csml)
132        #self.strCSML= parseString(tostring(csml)).toxml()
133       
134        #tidy up elementtree style namespaces
135        self.strCSML=csmllibs.parser_extra.removeInlineNS(self.strCSML)
136        #open file, write to it.
137        f = open(self.outputfile, "w")
138        f.write(self.strCSML)
139        f.close()
140
141    def printToScreen(self):
142        if self.printscreen==1:
143            print self.strCSML
144
Note: See TracBrowser for help on using the repository browser.