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

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

switch to declare time data to be stored inline or in file extract

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.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, self.timestorage)
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.