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

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

propagating additional args/configs through Scanner code

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,timestorage,spatialstorage,valuestorage):
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.timestorage=timestorage
26        self.spatialstorage=spatialstorage
27        self.valuestorage=valuestorage
28        self.csml = None  # csml object holds the csml document in memory.
29        self.ds_element = None #<Dataset> tag, root node.
30         
31    def build(self):
32        #wrapper method to call methods in correct order.
33        print "Builder"
34        self.createDataset()
35        self.addGMLMetadata()
36        self.createFeatureFileMap()
37        self.makeFileExtracts()
38        self.createFeatureCollection()
39        self.createFeatures()
40        #self.closeFC()
41        #self.closeDS()
42        self.saveFile()
43        self.printToScreen()
44
45    def createDataset(self):
46        #*************************************************************************************
47        #Creates empty CSML Dataset object with standard attributes
48        #*************************************************************************************
49             
50        ########### The Dataset  ##############
51        #Create an Empty Dataset
52        self.ds = csmllibs.Parser.Dataset()
53        #Set gml:id attribute of dataset to be equal to the directory name. (may want to rethink this later)
54        setattr(self.ds,'id',self.directory)
55        #setattr(self.ds, 'arrayDescriptors', adlist)
56        #setattr(self.ds, 'unitDefinitions', uds)
57        #setattr(self.ds, 'phenomenonDefinitions', pds)
58        #setattr(self.ds, 'featureCollection',fc)
59        ########################################
60
61    def addGMLMetadata(self):
62        #*********************************************************************************
63        # Adds main GML Metadata elements to dataset element
64        #*********************************************************************************
65        #Note: TODO: The Parser has a problem with gml:Name from AbstractGML.
66        strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory
67        strCodespace = 'http://ndg/nerc/ac/uk/NDGData'
68        strGmlDescription = 'Dataset for directory ' + self.directory
69        self.ds.metaDataProperty=[csmllibs.Parser.MetaDataProperty(href=strMetaDataProperty)]
70        self.ds.name=csmllibs.Parser.Name( codeSpace=strCodespace, val=[self.directory])
71        self.ds.description=csmllibs.Parser.Description(strGmlDescription)
72   
73    def createCSML(self):
74        print "Create CSML"
75        #create an empty csml document and dataset tag
76        self.csml, self.ds_element = csmllibs.csmlmeta.createDataset()
77        #add gml metadata elements
78        self.csml, self.ds_element = csmllibs.csmlmeta.addGMLMetadata(self.csml, self.directory, self.ds_element)
79
80    def createFeatureFileMap(self):
81        print "Create FFMAP"
82         #    print "creating filemap"
83        fmm = csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
84        if self.mapping=='onetomany':
85            self.ffmap = fmm.onetomany()
86        elif self.mapping=='onetoone':
87            self.ffmap = fmm.onetoone()
88        elif self.mapping=='oneonly':
89            self.ffmap = fmm.oneonly()
90
91    def makeFileExtracts(self):
92        print "make FEs"
93        if self.spatialstorage=='fileextract':
94            feBuilder =csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
95            self.ds = feBuilder.dataset_element
96            self.extractType = feBuilder.extractType
97            self.extractPrefix  = feBuilder.extractPrefix
98            self.extractDictionary=feBuilder.fileExtractDictionary
99
100    def createFeatureCollection(self):
101        #creates empty feature collection (list)
102        self.gml_FeatureCollection = csmllibs.Parser.FeatureCollection()
103 
104    def createFeatures(self):
105        print "create Features"
106        thefeatures = csmllibs.csmlfeaturetypes.featureBuilder( self.ds,self.gml_FeatureCollection,self.extractType, self.extractPrefix, self.ffmap, self.extractDictionary,self.timedimension)
107        #self.csml = thefeatures.csml
108        self.ds=thefeatures.ds_element
109        self.gml_FeatureCollection = thefeatures.gml_FeatureCollection_element
110        self.ds.featureCollection=self.gml_FeatureCollection
111
112                   
113    def saveFile(self):
114        print "Creating CSML document and saving file"
115        #call the toXML method of the Dataset object:
116        csml = self.ds.toXML()
117        #parse and pretty print csml
118        self.strCSML =csmllibs.parser_extra.PrettyPrint(csml)
119        #tidy up elementtree style namespaces
120        self.strCSML=csmllibs.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.