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

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

individual bounding boxes extended to edge of grid cells - not done yet for dataset bounding box

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,datasetid, directory,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage, minaxes):
9        if datasetid is not None:
10            self.datasetid = datasetid
11        else:
12            self.datasetid =csml.csmllibs.csmlextra.getRandomID()
13        self.directory = directory # top level directory containing data files
14        self.csmlfeaturetype  = csmlft
15        self.mapping = mapping
16        self.timedimension=timedimension
17        self.outputfile=outputfile
18        self.printscreen=printscreen
19        self.timestorage=timestorage
20        self.spatialstorage=spatialstorage
21        self.valuestorage=valuestorage
22        self.minaxes=minaxes
23        self.csml = None  # csml object holds the csml document in memory.
24        self.ds_element = None #<Dataset> tag, root node.
25         
26    def build(self):
27        #wrapper method to call methods in correct order.
28        self.createDataset()
29        try:
30            self.addGMLMetadata()
31        except TypeError:
32            #this is caused by an incorrect directory name
33            print 'error: Cannot find a config file at this location.'
34            sys.exit()
35           
36        self.createFeatureFileMap()
37        if self.spatialstorage=='fileextract':
38           self.makeFileExtracts()
39        self.createFeatureCollection()
40        self.createFeatures()
41        self.insertXlinks()
42        self.insertBoundingBoxes()
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 = csml.parser.Dataset()
54        #Use random id for dataset id.
55        setattr(self.ds,'id',self.datasetid)       
56        setattr(self.ds, 'name', csml.parser.csString('NAME OF DATASET GOES HERE'))
57        ########################################
58
59    def addGMLMetadata(self):
60        #*********************************************************************************
61        # Adds main GML Metadata elements to dataset element
62        #*********************************************************************************
63        #Note: TODO: The Parser has a problem with gml:Name from AbstractGML.
64        strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory
65        strCodespace = 'http://ndg/nerc/ac/uk/NDGData'
66        strGmlDescription = 'Dataset for directory ' + self.directory
67       
68        #TODO for V2
69        #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)]
70        #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory])
71        #self.ds.description=csml.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 = csml.csmllibs.csmlmeta.createDataset()
77        #add gml metadata elements
78        self.csml, self.ds_element = csml.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 = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
84        if self.mapping=='onetomany':
85            self.ffmap = fmm.onetomany()
86        elif self.mapping=='onetoseveral':
87            self.ffmap = fmm.onetoseveral()
88        elif self.mapping=='onetoone':
89            self.ffmap = fmm.onetoone()
90        elif self.mapping=='oneonly':
91            self.ffmap = fmm.oneonly()
92       
93    def makeFileExtracts(self):
94        print "make FEs"
95        feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
96        self.ds = feBuilder.dataset_element
97        self.extractType = feBuilder.extractType
98        self.extractDictionary=feBuilder.fileExtractDictionary
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, self.minaxes)
112               
113        #self.csml = thefeatures.csml
114        self.ds=thefeatures.ds_element
115        self.featureCollection = thefeatures.featureCollection
116        #try:
117            #self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
118        #except:
119            #pass
120        setattr(self.featureCollection, 'id', csml.csmllibs.csmlextra.getRandomID())
121       
122        self.ds.featureCollection=self.featureCollection
123               
124
125          #addEnvelope needs to work for different feature types
126        self.ds.featureCollection=self.featureCollection
127
128                   
129    def insertXlinks(self):
130        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
131       
132    def insertBoundingBoxes(self):
133        self.ds.featureCollection =csml.csmllibs.csmlextra.addBoundingBoxes(self.ds) 
134        try:
135            self.ds.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
136        except:
137            print 'could not add envelope bounding box'               
138       
139    def saveFile(self):
140        print "Creating CSML document and saving file"
141        #call the toXML method of the Dataset object:
142        csmlf = self.ds.toXML()
143        #parse and pretty print csml
144       
145        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
146        #tidy up elementtree style namespaces
147        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
148        #open file, write to it.
149        f = open(self.outputfile, "w")
150        f.write('<?xml version="1.0" encoding="UTF-8"?>')
151        f.write(self.strCSML)
152        f.close()
153
154    def printToScreen(self):
155        if self.printscreen=='1':
156            print self.strCSML
157
Note: See TracBrowser for help on using the repository browser.