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

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

Bounding boxes now correctly determined by 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,datasetid, directory,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage):
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.csml = None  # csml object holds the csml document in memory.
23        self.ds_element = None #<Dataset> tag, root node.
24         
25    def build(self):
26        #wrapper method to call methods in correct order.
27        self.createDataset()
28        try:
29            self.addGMLMetadata()
30        except TypeError:
31            #this is caused by an incorrect directory name
32            print 'error: Cannot find a config file at this location.'
33            sys.exit()
34           
35        self.createFeatureFileMap()
36        if self.spatialstorage=='fileextract':
37           self.makeFileExtracts()
38        self.createFeatureCollection()
39        self.createFeatures()
40        self.insertXlinks()
41        self.insertBoundingBoxes()
42        self.saveFile()
43        self.printToScreen()
44
45    def createDataset(self):
46        #*************************************************************************************
47        #Creates empty CSML Dataset object with standard attributes
48        #*************************************************************************************
49             
50        ########### The Dataset  ##############
51        #Create an Empty Dataset
52        self.ds = csml.parser.Dataset()
53        #Use random id for dataset id.
54        setattr(self.ds,'id',self.datasetid)       
55        setattr(self.ds, 'name', csml.parser.csString('NAME OF DATASET GOES HERE'))
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    def createFeatureCollection(self):
101        #creates empty feature collection (list)
102        self.featureCollection = csml.parser.CSMLFeatureCollection()
103 
104    def createFeatures(self):
105        print "create Features"
106        if not hasattr(self, 'extractType'):
107            self.extractType=None
108        if not hasattr(self, 'extractDictionary'):
109            self.extractDictionary=None
110        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage)
111               
112        #self.csml = thefeatures.csml
113        self.ds=thefeatures.ds_element
114        self.featureCollection = thefeatures.featureCollection
115        try:
116            self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
117        except:
118            pass
119        setattr(self.featureCollection, 'id', csml.csmllibs.csmlextra.getRandomID())
120       
121        self.ds.featureCollection=self.featureCollection
122               
123        try:
124            self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
125        except:
126            print 'could not add envelope bounding box'
127          #addEnvelope needs to work for different feature types
128        self.ds.featureCollection=self.featureCollection
129
130                   
131    def insertXlinks(self):
132        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
133       
134    def insertBoundingBoxes(self):
135        self.ds.featureCollection =csml.csmllibs.csmlextra.addBoundingBoxes(self.ds)               
136       
137    def saveFile(self):
138        print "Creating CSML document and saving file"
139        #call the toXML method of the Dataset object:
140        csmlf = self.ds.toXML()
141        #parse and pretty print csml
142       
143        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
144        #tidy up elementtree style namespaces
145        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
146        #open file, write to it.
147        f = open(self.outputfile, "w")
148        f.write('<?xml version="1.0" encoding="UTF-8"?>')
149        f.write(self.strCSML)
150        f.close()
151
152    def printToScreen(self):
153        if self.printscreen=='1':
154            print self.strCSML
155
Note: See TracBrowser for help on using the repository browser.