source: TI02-CSML/trunk/Scanner/csmllibs/csmlbuilder.py @ 1342

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

adding XML version info to CSML

Line 
1
2
3import xml.dom.ext
4from xml.dom.minidom import parseString
5from cElementTree import tostring
6import csmllibs
7
8
9import sys
10
11
12#Class for building the csml document.
13
14
15class csmlBuilder:
16    #this class contains all the method calls to create and populate a csmldoc
17    #it needs to be provided with several bits of information to be able to do this.
18    def __init__(self,directory,csmlft, mapping, timedimension ,outputfile,printscreen):
19        self.directory = directory # top level directory containing data files
20        self.csmlfeaturetype  = csmlft
21        self.mapping = mapping
22        self.timedimension=timedimension
23        self.outputfile=outputfile
24        self.printscreen=printscreen
25        self.csml = None  # csml object holds the csml document in memory.
26        self.ds_element = None #<Dataset> tag, root node.
27         
28    def build(self):
29        #wrapper method to call methods in correct order.
30        print "Builder"
31        self.createDataset()
32        self.addGMLMetadata()
33        self.createFeatureFileMap()
34        self.makeFileExtracts()
35        self.createFeatureCollection()
36        self.createFeatures()
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 = csmllibs.Parser.Dataset()
50        #Set gml:id attribute of dataset to be equal to the directory name. (may want to rethink this later)
51        setattr(self.ds,'id',self.directory)
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        self.ds.metaDataProperty=[csmllibs.Parser.MetaDataProperty(href=strMetaDataProperty)]
67        self.ds.name=csmllibs.Parser.Name( codeSpace=strCodespace, val=[self.directory])
68        self.ds.description=csmllibs.Parser.Description(strGmlDescription)
69   
70    def createCSML(self):
71        print "Create CSML"
72        #create an empty csml document and dataset tag
73        self.csml, self.ds_element = csmllibs.csmlmeta.createDataset()
74        #add gml metadata elements
75        self.csml, self.ds_element = csmllibs.csmlmeta.addGMLMetadata(self.csml, self.directory, self.ds_element)
76
77    def createFeatureFileMap(self):
78        print "Create FFMAP"
79         #    print "creating filemap"
80        fmm = csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
81        if self.mapping=='onetomany':
82            self.ffmap = fmm.onetomany()
83        elif self.mapping=='onetoone':
84            self.ffmap = fmm.onetoone()
85        elif self.mapping=='oneonly':
86            self.ffmap = fmm.oneonly()
87
88    def makeFileExtracts(self):
89        print "make FEs"
90        feBuilder =csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap)
91        #self.csml = feBuilder.csmldoc
92        self.ds = feBuilder.dataset_element
93        #todo, remember ds == dataset_element in this file
94        self.extractType = feBuilder.extractType
95        self.extractPrefix  = feBuilder.extractPrefix
96        self.extractDictionary=feBuilder.fileExtractDictionary
97
98    def createFeatureCollection(self):
99        #creates empty feature collection (list)
100        self.gml_FeatureCollection = csmllibs.Parser.FeatureCollection()
101 
102    def createFeatures(self):
103        print "create Features"
104        thefeatures = csmllibs.csmlfeaturetypes.featureBuilder( self.ds,self.gml_FeatureCollection,self.extractType, self.extractPrefix, self.ffmap, self.extractDictionary,self.timedimension)
105        #self.csml = thefeatures.csml
106        self.ds=thefeatures.ds_element
107        self.gml_FeatureCollection = thefeatures.gml_FeatureCollection_element
108        self.ds.featureCollection=self.gml_FeatureCollection
109
110                   
111    def saveFile(self):
112        print "Creating CSML document and saving file"
113        #call the toXML method of the Dataset object:
114        csml = self.ds.toXML()
115        #parse and pretty print csml
116        self.strCSML =csmllibs.parser_extra.PrettyPrint(csml)
117        #tidy up elementtree style namespaces
118        self.strCSML=csmllibs.parser_extra.removeInlineNS(self.strCSML)
119        #open file, write to it.
120        f = open(self.outputfile, "w")
121        f.write('<?xml version="1.0" encoding="UTF-8"?>')
122        f.write(self.strCSML)
123        f.close()
124
125    def printToScreen(self):
126        if self.printscreen==1:
127            print self.strCSML
128
Note: See TracBrowser for help on using the repository browser.