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

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

added ability to filter out low dimension variables in scanner. Added some test crs definitions for testing rotated grids. Added axis identification by standard name

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        try:
125            self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
126        except:
127            print 'could not add envelope bounding box'
128          #addEnvelope needs to work for different feature types
129        self.ds.featureCollection=self.featureCollection
130
131                   
132    def insertXlinks(self):
133        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
134       
135    def insertBoundingBoxes(self):
136        self.ds.featureCollection =csml.csmllibs.csmlextra.addBoundingBoxes(self.ds)               
137       
138    def saveFile(self):
139        print "Creating CSML document and saving file"
140        #call the toXML method of the Dataset object:
141        csmlf = self.ds.toXML()
142        #parse and pretty print csml
143       
144        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
145        #tidy up elementtree style namespaces
146        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
147        #open file, write to it.
148        f = open(self.outputfile, "w")
149        f.write('<?xml version="1.0" encoding="UTF-8"?>')
150        f.write(self.strCSML)
151        f.close()
152
153    def printToScreen(self):
154        if self.printscreen=='1':
155            print self.strCSML
156
Note: See TracBrowser for help on using the repository browser.