source: TI02-CSML/trunk/parser/parser_extra.py @ 1350

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

xlink:href references to local ArrayGenerators are resolved when CSML file is parsed. Also added example feature, feat04a, to example.xml

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
11import string
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        csmlstring=csmlstring.replace('{http://ndg.nerc.ac.uk/moles}','moles:')
52        #remove cmsl: prefixes
53        csmlstring=csmlstring.replace('<csml:','<')
54        csmlstring=csmlstring.replace('</csml:','</')
55       
56        #add namespace declarations at top of document
57        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:moles="http://ndg.nerc.ac.uk/moles"  xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"')
58       
59        #this is only used when creating a new csml document
60        csmlstring=csmlstring.replace('ns0', 'gml')
61        csmlstring=csmlstring.replace('xmlns:ns1', 'xmlns:xlink')
62        csmlstring=csmlstring.replace('ns1:href', 'xlink:href')
63        csmlstring=csmlstring.replace('ns1:moles', 'xlink:moles')
64        csmlstring=csmlstring.replace('ns1:', '')  #the rest of the ns1s are CSML namespaces... due to the way it has been parsed.       
65        return csmlstring
66
67
68def isURI(uri):
69    """ a very simple function to test if a string is a uri
70    if ;// appears in the first 12 characters it is probably a uri """
71    #TODO - a decent uri check!   
72    result = False
73    if string.find(uri[:12], '://') != -1:
74        result = True
75    return result
76
77
78class ParserPostProcessor:
79    def __init__(self,dataset):
80        if isinstance(dataset,Parser.Dataset):
81            self.dataset=dataset
82        else: 
83            print 'Error: Could not post-process CSML document'
84            sys.exit()
85   
86    def resolveReferences(self):
87        #if there are any phenomenon defintions then
88         #for every <parameter> element, need to resolve any hrefs.
89        if hasattr(self.dataset, 'phenomenonDefinitions'):
90            for feature in self.dataset.featureCollection.members:
91                if hasattr(feature, 'parameter'):
92                    if hasattr(feature.parameter,'href'):
93                        #search phenomenon definitions for a match.
94                        for definition in self.dataset.phenomenonDefinitions.definitionMembers:
95                            if definition.id == feature.parameter.href[1:]:  #remove '#'
96                                #remove href and replace with object
97                                feature.parameter.href =None
98                                feature.parameter=definition
99                                break
100       
101        #if there are embedded reference system definitions then
102        #for every DomainReference and DomainComplement need to resolve any srsNames
103        if hasattr(self.dataset, 'referenceSystemDefinitions'):
104            for feature in self.dataset.featureCollection.members:
105                if hasattr(feature, 'domain'):  #Trajectory features don't have domains..
106                    if hasattr(feature.domain.domainReference, 'srsName'):
107                        #search reference definitions for a match.
108                        for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
109                            if definition.id == feature.domain.domainReference.srsName[1:]:
110                                #remove srsName attribute and replace with Object.
111                                pass #Leave this for now.
112                    if hasattr(feature.domain, 'domainComplement'):
113                        if hasattr(feature.domain.domainComplement, 'srsName'):
114                            #search reference definitions for a match.
115                            for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
116                                if definition.id == feature.domain.domainComplement.srsName[1:]:
117                                        #remove srsName attribute and replace with Object.
118                                    pass #Leave this for now.
119       
120       #NetCDFExtracts etc may be referenced e.g. as <axisvalues> element. Need to fix these too.
121        if hasattr(self.dataset, 'arrayDescriptors'):
122            for feature in self.dataset.featureCollection.members:
123                if hasattr (feature, 'domain'):
124                    if hasattr(feature.domain, 'domainComplement'):
125                        if isinstance(feature.domain.domainComplement, Parser.Grid):
126                                for ordinate in feature.domain.domainComplement.ordinates:
127                                    #search arrayDescriptors for a match.
128                                    for arrayDescriptor  in self.dataset.arrayDescriptors:
129                                        if arrayDescriptor.id == ordinate.axisValues[1:]:  #remove '#'
130                                            ordinate.axisValues  = arrayDescriptor
131                                            break
132                                            #also times may reference
133                    if hasattr(feature.domain, 'domainReference'):   
134                        if hasattr(feature.domain.domainReference, 'times'):
135                            if hasattr(feature.domain.domainReference.times, 'href'):
136                            #search descriptors for a match.
137                                for arrayDescriptor in self.dataset.arrayDescriptors:
138                                    if arrayDescriptor.id == feature.domain.domainReference.times.href[1:]:  #remove '#'
139                                        #remove times xlink attribute and replace with Object.
140                                        feature.domain.domainReference.times.timePositions= arrayDescriptor
141                                        break
142                                   
143        #sys.exit()   
144        #uom may reference UnitDefinitions. Replace these too
145        if hasattr(self.dataset,'arrayDescriptors'):
146            for feature in self.dataset.featureCollection.members:
147                if hasattr (feature, 'rangeSet'):
148                    if hasattr (feature.rangeSet, 'aggregatedArray'):
149                         if hasattr(feature.rangeSet.aggregatedArray, 'uom'):
150                             if hasattr(self.dataset, 'unitDefinitions'):
151                                for definition in self.dataset.unitDefinitions.definitionMembers:
152                                    if definition.id== feature.rangeSet.aggregatedArray.uom[1:]:
153                                        feature.rangeSet.aggregatedArray.uom=definition
154                                        break
155        #TODO need to also take into account the fact that UnitDefinitions could be externally defined.
156       
157        return self.dataset
Note: See TracBrowser for help on using the repository browser.