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

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

added option to pass dataset ID to csml scanner; use config option of form: [dataset] dsID:myIDabc123

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.saveFile()
42        self.printToScreen()
43
44    def createDataset(self):
45        #*************************************************************************************
46        #Creates empty CSML Dataset object with standard attributes
47        #*************************************************************************************
48             
49        ########### The Dataset  ##############
50        #Create an Empty Dataset
51        self.ds = csml.parser.Dataset()
52        #Use random id for dataset id.
53        setattr(self.ds,'id',self.datasetid)       
54        setattr(self.ds, 'name', csml.parser.csString('NAME OF DATASET GOES HERE'))
55        ########################################
56
57    def addGMLMetadata(self):
58        #*********************************************************************************
59        # Adds main GML Metadata elements to dataset element
60        #*********************************************************************************
61        #Note: TODO: The Parser has a problem with gml:Name from AbstractGML.
62        strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory
63        strCodespace = 'http://ndg/nerc/ac/uk/NDGData'
64        strGmlDescription = 'Dataset for directory ' + self.directory
65       
66        #TODO for V2
67        #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)]
68        #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory])
69        #self.ds.description=csml.parser.Description(strGmlDescription)
70   
71    def createCSML(self):
72        print "Create CSML"
73        #create an empty csml document and dataset tag
74        self.csml, self.ds_element = csml.csmllibs.csmlmeta.createDataset()
75        #add gml metadata elements
76        self.csml, self.ds_element = csml.csmllibs.csmlmeta.addGMLMetadata(self.csml, self.directory, self.ds_element)
77
78    def createFeatureFileMap(self):
79        print "Create FFMAP"
80         #    print "creating filemap"
81        fmm = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
82        if self.mapping=='onetomany':
83            self.ffmap = fmm.onetomany()
84        elif self.mapping=='onetoseveral':
85            self.ffmap = fmm.onetoseveral()
86        elif self.mapping=='onetoone':
87            self.ffmap = fmm.onetoone()
88        elif self.mapping=='oneonly':
89            self.ffmap = fmm.oneonly()
90       
91    def makeFileExtracts(self):
92        print "make FEs"
93        feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
94        self.ds = feBuilder.dataset_element
95        self.extractType = feBuilder.extractType
96        self.extractDictionary=feBuilder.fileExtractDictionary
97
98   
99    def createFeatureCollection(self):
100        #creates empty feature collection (list)
101        self.featureCollection = csml.parser.CSMLFeatureCollection()
102 
103    def createFeatures(self):
104        print "create Features"
105        if not hasattr(self, 'extractType'):
106            self.extractType=None
107        if not hasattr(self, 'extractDictionary'):
108            self.extractDictionary=None
109        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage)
110               
111        #self.csml = thefeatures.csml
112        self.ds=thefeatures.ds_element
113        self.featureCollection = thefeatures.featureCollection
114        try:
115            self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
116        except:
117            pass
118        setattr(self.featureCollection, 'id', csml.csmllibs.csmlextra.getRandomID())
119        try:
120            self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
121        except:
122            print 'could not add envelope bounding box'
123          #addEnvelope needs to work for different feature types
124        self.ds.featureCollection=self.featureCollection
125
126                   
127    def insertXlinks(self):
128        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
129       
130       
131    def saveFile(self):
132        print "Creating CSML document and saving file"
133        #call the toXML method of the Dataset object:
134        csmlf = self.ds.toXML()
135        #parse and pretty print csml
136       
137        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
138        #tidy up elementtree style namespaces
139        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
140        #open file, write to it.
141        f = open(self.outputfile, "w")
142        f.write('<?xml version="1.0" encoding="UTF-8"?>')
143        f.write(self.strCSML)
144        f.close()
145
146    def printToScreen(self):
147        if self.printscreen=='1':
148            print self.strCSML
149
Note: See TracBrowser for help on using the repository browser.