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

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

fixed schemalocation, datasetid, CMSLfeaturecollection in scanner

Line 
1import csml
2import sys
3
4#Class for building the csml document.
5class csmlBuilder(object):
6    #this class contains all the method calls to create and populate a csmldoc
7    #it needs to be provided with several bits of information to be able to do this.
8    def __init__(self,directory,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage):
9        self.directory = directory # top level directory containing data files
10        self.csmlfeaturetype  = csmlft
11        self.mapping = mapping
12        self.timedimension=timedimension
13        self.outputfile=outputfile
14        self.printscreen=printscreen
15        self.timestorage=timestorage
16        self.spatialstorage=spatialstorage
17        self.valuestorage=valuestorage
18        self.csml = None  # csml object holds the csml document in memory.
19        self.ds_element = None #<Dataset> tag, root node.
20         
21    def build(self):
22        #wrapper method to call methods in correct order.
23        self.createDataset()
24        try:
25            self.addGMLMetadata()
26        except TypeError:
27            #this is caused by an incorrect directory name
28            print 'error: Cannot find a config file at this location.'
29            sys.exit()
30           
31        self.createFeatureFileMap()
32        if self.spatialstorage=='fileextract':
33           self.makeFileExtracts()
34        self.createFeatureCollection()
35        self.createFeatures()
36        self.insertXlinks()
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 = csml.parser.Dataset()
50        #Use random id for dataset id.
51        setattr(self.ds,'id',csml.csmllibs.csmlextra.getRandomID())       
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       
67        #TODO for V2
68        #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)]
69        #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory])
70        #self.ds.description=csml.parser.Description(strGmlDescription)
71   
72    def createCSML(self):
73        print "Create CSML"
74        #create an empty csml document and dataset tag
75        self.csml, self.ds_element = csml.csmllibs.csmlmeta.createDataset()
76        #add gml metadata elements
77        self.csml, self.ds_element = csml.csmllibs.csmlmeta.addGMLMetadata(self.csml, self.directory, self.ds_element)
78
79    def createFeatureFileMap(self):
80        print "Create FFMAP"
81         #    print "creating filemap"
82        fmm = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
83        if self.mapping=='onetomany':
84            self.ffmap = fmm.onetomany()
85        elif self.mapping=='onetoseveral':
86            self.ffmap = fmm.onetoseveral()
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 =csml.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   
100   
101    def createFeatureCollection(self):
102        #creates empty feature collection (list)
103        self.featureCollection = csml.parser.CSMLFeatureCollection()
104 
105    def createFeatures(self):
106        print "create Features"
107        if not hasattr(self, 'extractType'):
108            self.extractType=None
109        if not hasattr(self, 'extractDictionary'):
110            self.extractDictionary=None
111        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage)
112               
113        #self.csml = thefeatures.csml
114        self.ds=thefeatures.ds_element
115        self.featureCollection = thefeatures.featureCollection
116        self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
117        setattr(self.featureCollection, 'id', csml.csmllibs.csmlextra.getRandomID())
118        try:
119            self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
120        except:
121            print 'could not add envelope bounding box'
122          #addEnvelope needs to work for different feature types
123        self.ds.featureCollection=self.featureCollection
124
125                   
126    def insertXlinks(self):
127        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
128       
129       
130    def saveFile(self):
131        print "Creating CSML document and saving file"
132        #call the toXML method of the Dataset object:
133        csmlf = self.ds.toXML()
134        #parse and pretty print csml
135       
136        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
137        #tidy up elementtree style namespaces
138        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
139        #open file, write to it.
140        f = open(self.outputfile, "w")
141        f.write('<?xml version="1.0" encoding="UTF-8"?>')
142        f.write(self.strCSML)
143        f.close()
144
145    def printToScreen(self):
146        if self.printscreen=='1':
147            print self.strCSML
148
Note: See TracBrowser for help on using the repository browser.