1 | import csml |
---|
2 | |
---|
3 | #Class for building the csml document. |
---|
4 | class csmlBuilder(object): |
---|
5 | #this class contains all the method calls to create and populate a csmldoc |
---|
6 | #it needs to be provided with several bits of information to be able to do this. |
---|
7 | def __init__(self,directory,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage): |
---|
8 | self.directory = directory # top level directory containing data files |
---|
9 | self.csmlfeaturetype = csmlft |
---|
10 | self.mapping = mapping |
---|
11 | self.timedimension=timedimension |
---|
12 | self.outputfile=outputfile |
---|
13 | self.printscreen=printscreen |
---|
14 | self.timestorage=timestorage |
---|
15 | self.spatialstorage=spatialstorage |
---|
16 | self.valuestorage=valuestorage |
---|
17 | self.csml = None # csml object holds the csml document in memory. |
---|
18 | self.ds_element = None #<Dataset> tag, root node. |
---|
19 | |
---|
20 | def build(self): |
---|
21 | #wrapper method to call methods in correct order. |
---|
22 | self.createDataset() |
---|
23 | self.addGMLMetadata() |
---|
24 | self.createFeatureFileMap() |
---|
25 | if self.spatialstorage=='fileextract': |
---|
26 | self.makeFileExtracts() |
---|
27 | self.createFeatureCollection() |
---|
28 | self.createFeatures() |
---|
29 | self.insertXlinks() |
---|
30 | #self.closeFC() |
---|
31 | #self.closeDS() |
---|
32 | self.saveFile() |
---|
33 | self.printToScreen() |
---|
34 | |
---|
35 | def createDataset(self): |
---|
36 | #************************************************************************************* |
---|
37 | #Creates empty CSML Dataset object with standard attributes |
---|
38 | #************************************************************************************* |
---|
39 | |
---|
40 | ########### The Dataset ############## |
---|
41 | #Create an Empty Dataset |
---|
42 | self.ds = csml.parser.Dataset() |
---|
43 | #Set gml:id attribute of dataset to be equal to the directory name. (may want to rethink this later) |
---|
44 | setattr(self.ds,'id',self.directory) |
---|
45 | #setattr(self.ds, 'arrayDescriptors', adlist) |
---|
46 | #setattr(self.ds, 'unitDefinitions', uds) |
---|
47 | #setattr(self.ds, 'phenomenonDefinitions', pds) |
---|
48 | #setattr(self.ds, 'featureCollection',fc) |
---|
49 | ######################################## |
---|
50 | |
---|
51 | def addGMLMetadata(self): |
---|
52 | #********************************************************************************* |
---|
53 | # Adds main GML Metadata elements to dataset element |
---|
54 | #********************************************************************************* |
---|
55 | #Note: TODO: The Parser has a problem with gml:Name from AbstractGML. |
---|
56 | strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory |
---|
57 | strCodespace = 'http://ndg/nerc/ac/uk/NDGData' |
---|
58 | strGmlDescription = 'Dataset for directory ' + self.directory |
---|
59 | |
---|
60 | #TODO for V2 |
---|
61 | #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)] |
---|
62 | #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory]) |
---|
63 | #self.ds.description=csml.parser.Description(strGmlDescription) |
---|
64 | |
---|
65 | def createCSML(self): |
---|
66 | print "Create CSML" |
---|
67 | #create an empty csml document and dataset tag |
---|
68 | self.csml, self.ds_element = csml.csmllibs.csmlmeta.createDataset() |
---|
69 | #add gml metadata elements |
---|
70 | self.csml, self.ds_element = csml.csmllibs.csmlmeta.addGMLMetadata(self.csml, self.directory, self.ds_element) |
---|
71 | |
---|
72 | def createFeatureFileMap(self): |
---|
73 | print "Create FFMAP" |
---|
74 | # print "creating filemap" |
---|
75 | fmm = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype) |
---|
76 | if self.mapping=='onetomany': |
---|
77 | self.ffmap = fmm.onetomany() |
---|
78 | elif self.mapping=='onetoone': |
---|
79 | self.ffmap = fmm.onetoone() |
---|
80 | elif self.mapping=='oneonly': |
---|
81 | self.ffmap = fmm.oneonly() |
---|
82 | |
---|
83 | def makeFileExtracts(self): |
---|
84 | print "make FEs" |
---|
85 | feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension) |
---|
86 | self.ds = feBuilder.dataset_element |
---|
87 | self.extractType = feBuilder.extractType |
---|
88 | self.extractDictionary=feBuilder.fileExtractDictionary |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | def createFeatureCollection(self): |
---|
93 | #creates empty feature collection (list) |
---|
94 | self.featureCollection = csml.parser.CSMLFeatureCollection() |
---|
95 | |
---|
96 | def createFeatures(self): |
---|
97 | print "create Features" |
---|
98 | if not hasattr(self, 'extractType'): |
---|
99 | self.extractType=None |
---|
100 | if not hasattr(self, 'extractDictionary'): |
---|
101 | self.extractDictionary=None |
---|
102 | thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage) |
---|
103 | |
---|
104 | #self.csml = thefeatures.csml |
---|
105 | self.ds=thefeatures.ds_element |
---|
106 | self.featureCollection = thefeatures.featureCollection |
---|
107 | self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection) |
---|
108 | self.ds.featureCollection=self.featureCollection |
---|
109 | |
---|
110 | |
---|
111 | def insertXlinks(self): |
---|
112 | self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds) |
---|
113 | |
---|
114 | |
---|
115 | def saveFile(self): |
---|
116 | print "Creating CSML document and saving file" |
---|
117 | #call the toXML method of the Dataset object: |
---|
118 | csmlf = self.ds.toXML() |
---|
119 | #parse and pretty print csml |
---|
120 | |
---|
121 | self.strCSML = csml.parser_extra.PrettyPrint(csmlf) |
---|
122 | #tidy up elementtree style namespaces |
---|
123 | self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML) |
---|
124 | #open file, write to it. |
---|
125 | f = open(self.outputfile, "w") |
---|
126 | f.write('<?xml version="1.0" encoding="UTF-8"?>') |
---|
127 | f.write(self.strCSML) |
---|
128 | f.close() |
---|
129 | |
---|
130 | def printToScreen(self): |
---|
131 | if self.printscreen=='1': |
---|
132 | print self.strCSML |
---|
133 | |
---|