source: TI02-CSML/trunk/newparser/parser_extra.py @ 864

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/newparser/parser_extra.py@864
Revision 864, 7.3 KB checked in by domlowe, 14 years ago (diff)

csmlio - getDomainComplement,getDomainReference linked up with file extracts

Line 
1#parser_extra.py
2# DL 12 april 2006
3# 'extra' functions to enable parsing.
4#contains:
5#. Namespace fix
6# Function to resolve references in CSML doc.
7
8import Parser
9import sys
10import elementtree.ElementTree as etree
11
12
13#some xml/string manipulation functions. may as well go in this file for now:
14
15
16def PrettyPrint(elem,indent='',html=0,space='   '):
17        '''Lightweight pretty printing of elementTree elements'''
18        def estrip(elem):
19                ''' Just want to get rid of unwanted whitespace '''
20                if elem is None:
21                        return ''
22                else:
23                        return elem.strip()
24        strAttrib=''
25        for att in elem.attrib:
26                strAttrib+=' %s="%s"'%(att,elem.attrib[att])
27        result='%s<%s%s>%s'%(indent,elem.tag,strAttrib,estrip(elem.text))
28        children=len(elem)
29        if children:
30                for item in elem:
31                        result+='\n'+PrettyPrint(item,indent=indent+space)
32                result+='\n%s%s</%s>'%(indent,estrip(item.tail),elem.tag)
33        else:
34                result+='</%s>'%(elem.tag)
35        return result
36
37# fixing up namespaces:
38def removeInlineNS(csmlstring):
39        #  removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element.
40        #TODO: This whole thing is hardcoded and needs reviewing.
41       
42        #first need to update the ElementTree namespace map:
43        etree._namespace_map.update({
44        'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'})
45   
46        #replace any fully qualified namespaces
47        csmlstring=csmlstring.replace('{http://www.opengis.net/gml}', 'gml:')
48        csmlstring=csmlstring.replace('{http://ndg.nerc.ac.uk/csml}','')
49        csmlstring=csmlstring.replace('{http://www.w3.org/1999/xlink}','xlink:')
50        csmlstring=csmlstring.replace('{http://www.opengis.net/om}','om:')       
51        #remove cmsl: prefixes
52        csmlstring=csmlstring.replace('<csml:','<')
53        csmlstring=csmlstring.replace('</csml:','</')
54       
55        #add namespace declarations at top of document
56        csmlstring=csmlstring.replace('<Dataset', '<Dataset xmlns="http://ndg.nerc.ac.uk/csml'  'xmlns:gml="http://www.opengis.net/gml" xmlns:om="http://www.opengis.net/om" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd">')
57       
58        #this is only used when creating a new csml document
59        csmlstring=csmlstring.replace('ns0', 'gml')
60        csmlstring=csmlstring.replace('xmlns:ns1', 'xmlns:xlink')
61        csmlstring=csmlstring.replace('ns1:href', 'xlink:href')
62        csmlstring=csmlstring.replace('ns1:', '')  #the rest of the ns1s are CSML namespaces... due to the way it has been parsed.       
63        return csmlstring
64
65
66class ParserPostProcessor:
67    def __init__(self,dataset):
68        if isinstance(dataset,Parser.Dataset):
69            self.dataset=dataset
70        else: 
71            print 'Error: Could not post-process CSML document'
72            sys.exit()
73   
74    def resolveReferences(self):
75        #if there are any phenomenon defintions then
76         #for every <parameter> element, need to resolve any hrefs.
77        if hasattr(self.dataset, 'phenomenonDefinitions'):
78            for feature in self.dataset.featureCollection.members:
79                if hasattr(feature, 'parameter'):
80                    if hasattr(feature.parameter,'href'):
81                        #search phenomenon definitions for a match.
82                        for definition in self.dataset.phenomenonDefinitions.definitionMembers:
83                            if definition.id == feature.parameter.href[1:]:  #remove '#'
84                                #remove href and replace with object
85                                feature.parameter.href =None
86                                feature.parameter=definition
87                                break
88       
89        #if there are embedded reference system definitions then
90        #for every DomainReference and DomainComplement need to resolve any srsNames
91        if hasattr(self.dataset, 'referenceSystemDefinitions'):
92            for feature in self.dataset.featureCollection.members:
93                if hasattr(feature, 'domain'):  #Trajectory features don't have domains..
94                    if hasattr(feature.domain.domainReference, 'srsName'):
95                        #search reference definitions for a match.
96                        for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
97                            if definition.id == feature.domain.domainReference.srsName[1:]:
98                                #remove srsName attribute and replace withimport pickle Object.
99                                pass #Leave this for now.
100                    if hasattr(feature.domain, 'domainComplement'):
101                        if hasattr(feature.domain.domainComplement, 'srsName'):
102                            #search reference definitions for a match.
103                            for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
104                                if definition.id == feature.domain.domainComplement.srsName[1:]:
105                                        #remove srsName attribute and replace with Object.
106                                    pass #Leave this for now.
107       
108       #NetCDFExtracts etc may be referenced e.g. as <axisvalues> element. Need to fix these too.
109        if hasattr(self.dataset, 'arrayDescriptors'):
110            for feature in self.dataset.featureCollection.members:
111                if hasattr (feature, 'domain'):
112                    if hasattr(feature.domain, 'domainComplement'):
113                        if isinstance(feature.domain.domainComplement, Parser.Grid):
114                                for ordinate in feature.domain.domainComplement.ordinates:
115                                    #search arrayDescriptors for a match.
116                                    for arrayDescriptor  in self.dataset.arrayDescriptors:
117                                        if arrayDescriptor.id == ordinate.axisValues[1:]:  #remove '#'
118                                            ordinate.axisValues  = arrayDescriptor
119                                            break
120                                       
121        #uom may reference UnitDefinitions. Replace these too
122        if hasattr(self.dataset,'arrayDescriptors'):
123            for feature in self.dataset.featureCollection.members:
124                if hasattr (feature, 'rangeSet'):
125                    if hasattr (feature.rangeSet, 'aggregatedArray'):
126                         if hasattr(feature.rangeSet.aggregatedArray, 'uom'):
127                             if hasattr(self.dataset, 'unitDefinitions'):
128                                for definition in self.dataset.unitDefinitions.definitionMembers:
129                                    if definition.id== feature.rangeSet.aggregatedArray.uom[1:]:
130                                        feature.rangeSet.aggregatedArray.uom=definition
131                                        break
132        #TODO need to also take into account the fact that UnitDefinitions could be externally defined.
133       
134        return self.dataset
Note: See TracBrowser for help on using the repository browser.