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

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

removed need to keep passing extracttype around

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