source: TI02-CSML/trunk/csml/csmllibs/csmlbuilder.py @ 2121

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

New featurefilemap pattern onetomany, tested on DDC data

Line 
1import csml
2
3#Class for building the csml document.
4class csmlBuilder(object):
5    #this class contains all the method calls to create and populate a csmldoc
6    #it needs to be provided with several bits of information to be able to do this.
7    def __init__(self,directory,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage):
8        self.directory = directory # top level directory containing data files
9        self.csmlfeaturetype  = csmlft
10        self.mapping = mapping
11        self.timedimension=timedimension
12        self.outputfile=outputfile
13        self.printscreen=printscreen
14        self.timestorage=timestorage
15        self.spatialstorage=spatialstorage
16        self.valuestorage=valuestorage
17        self.csml = None  # csml object holds the csml document in memory.
18        self.ds_element = None #<Dataset> tag, root node.
19         
20    def build(self):
21        #wrapper method to call methods in correct order.
22        self.createDataset()
23        self.addGMLMetadata()
24        self.createFeatureFileMap()
25        if self.spatialstorage=='fileextract':
26           self.makeFileExtracts()
27        self.createFeatureCollection()
28        self.createFeatures()
29        self.insertXlinks()
30        #self.closeFC()
31        #self.closeDS()
32        self.saveFile()
33        self.printToScreen()
34
35    def createDataset(self):
36        #*************************************************************************************
37        #Creates empty CSML Dataset object with standard attributes
38        #*************************************************************************************
39             
40        ########### The Dataset  ##############
41        #Create an Empty Dataset
42        self.ds = csml.parser.Dataset()
43        #Set gml:id attribute of dataset to be equal to the directory name. (may want to rethink this later)
44        setattr(self.ds,'id',self.directory)
45        #setattr(self.ds, 'arrayDescriptors', adlist)
46        #setattr(self.ds, 'unitDefinitions', uds)
47        #setattr(self.ds, 'phenomenonDefinitions', pds)
48        #setattr(self.ds, 'featureCollection',fc)
49        ########################################
50
51    def addGMLMetadata(self):
52        #*********************************************************************************
53        # Adds main GML Metadata elements to dataset element
54        #*********************************************************************************
55        #Note: TODO: The Parser has a problem with gml:Name from AbstractGML.
56        strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory
57        strCodespace = 'http://ndg/nerc/ac/uk/NDGData'
58        strGmlDescription = 'Dataset for directory ' + self.directory
59       
60        #TODO for V2
61        #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)]
62        #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory])
63        #self.ds.description=csml.parser.Description(strGmlDescription)
64   
65    def createCSML(self):
66        print "Create CSML"
67        #create an empty csml document and dataset tag
68        self.csml, self.ds_element = csml.csmllibs.csmlmeta.createDataset()
69        #add gml metadata elements
70        self.csml, self.ds_element = csml.csmllibs.csmlmeta.addGMLMetadata(self.csml, self.directory, self.ds_element)
71
72    def createFeatureFileMap(self):
73        print "Create FFMAP"
74         #    print "creating filemap"
75        fmm = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
76        if self.mapping=='onetomany':
77            self.ffmap = fmm.onetomany()
78        elif self.mapping=='onetoseveral':
79            self.ffmap = fmm.onetoseveral()
80        elif self.mapping=='onetoone':
81            self.ffmap = fmm.onetoone()
82        elif self.mapping=='oneonly':
83            self.ffmap = fmm.oneonly()
84       
85    def makeFileExtracts(self):
86        print "make FEs"
87        feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
88        self.ds = feBuilder.dataset_element
89        self.extractType = feBuilder.extractType
90        self.extractDictionary=feBuilder.fileExtractDictionary
91
92   
93   
94    def createFeatureCollection(self):
95        #creates empty feature collection (list)
96        self.featureCollection = csml.parser.CSMLFeatureCollection()
97 
98    def createFeatures(self):
99        print "create Features"
100        if not hasattr(self, 'extractType'):
101            self.extractType=None
102        if not hasattr(self, 'extractDictionary'):
103            self.extractDictionary=None
104        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage)
105               
106        #self.csml = thefeatures.csml
107        self.ds=thefeatures.ds_element
108        self.featureCollection = thefeatures.featureCollection
109        self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection)       
110        self.ds.featureCollection=self.featureCollection
111
112                   
113    def insertXlinks(self):
114        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
115       
116       
117    def saveFile(self):
118        print "Creating CSML document and saving file"
119        #call the toXML method of the Dataset object:
120        csmlf = self.ds.toXML()
121        #parse and pretty print csml
122       
123        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
124        #tidy up elementtree style namespaces
125        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
126        #open file, write to it.
127        f = open(self.outputfile, "w")
128        f.write('<?xml version="1.0" encoding="UTF-8"?>')
129        f.write(self.strCSML)
130        f.close()
131
132    def printToScreen(self):
133        if self.printscreen=='1':
134            print self.strCSML
135
Note: See TracBrowser for help on using the repository browser.