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

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

added csmlio.py to provide easy wrapper to parser, also ioDemo.py to show how to make calls. Neither are complete

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
10
11
12#string manipulation function. may as well go in this file for now.
13def removeInlineNS(csmlstring):
14        #  removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element.
15       
16
17        #remove all
18        csmlstring=csmlstring.replace(' xmlns:csml="http://ndg.nerc.ac.uk/csml"','')
19        csmlstring=csmlstring.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"','')
20        csmlstring=csmlstring.replace('xmlns:om="http://www.opengis.net/om"','')
21        csmlstring=csmlstring.replace('xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"','')
22        csmlstring=csmlstring.replace('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','')
23       
24       
25        #remove cmsl: prefixes
26        csmlstring=csmlstring.replace('<csml:','<')
27        csmlstring=csmlstring.replace('</csml:','</')
28        #add namespace declarations at top of document, after gml namespace declaration
29        csmlstring=csmlstring.replace('xmlns:gml="http://www.opengis.net/gml">', '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">')
30       
31        return csmlstring
32
33
34class ParserPostProcessor:
35    def __init__(self,dataset):
36        if isinstance(dataset,Parser.Dataset):
37            self.dataset=dataset
38        else: 
39            print 'Error: Could not post-process CSML document'
40            sys.exit()
41   
42    def resolveReferences(self):
43        #if there are any phenomenon defintions then
44         #for every <parameter> element, need to resolve any hrefs.
45        if hasattr(self.dataset, 'phenomenonDefinitions'):
46            for feature in self.dataset.featureCollection.members:
47                if hasattr(feature, 'parameter'):
48                    if hasattr(feature.parameter,'href'):
49                        #search phenomenon definitions for a match.
50                        for definition in self.dataset.phenomenonDefinitions.definitionMembers:
51                            if definition.id == feature.parameter.href[1:]:  #remove '#'
52                                #remove href and replace with object
53                                print dir(definition)
54                                feature.parameter.href =None
55                                feature.parameter=definition
56                                break
57       
58        #if there are embedded reference system definitions then
59        #for every DomainReference and DomainComplement need to resolve any srsNames
60        if hasattr(self.dataset, 'referenceSystemDefinitions'):
61            for feature in self.dataset.featureCollection.members:
62                if hasattr(feature, 'domain'):  #Trajectory features don't have domains..
63                    if hasattr(feature.domain.domainReference, 'srsName'):
64                        #search reference definitions for a match.
65                        for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
66                            if definition.id == feature.domain.domainReference.srsName[1:]:
67                                #remove srsName attribute and replace withimport pickle Object.
68                                pass #Leave this for now.
69                    if hasattr(feature.domain, 'domainComplement'):
70                        if hasattr(feature.domain.domainComplement, 'srsName'):
71                            #search reference definitions for a match.
72                            for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
73                                if definition.id == feature.domain.domainComplement.srsName[1:]:
74                                        #remove srsName attribute and replace with Object.
75                                    pass #Leave this for now.
76       
77       #NetCDFExtracts etc may be referenced e.g. as <axisvalues> element. Need to fix these too.
78        if hasattr(self.dataset, 'arrayDescriptors'):
79            for feature in self.dataset.featureCollection.members:
80                if hasattr (feature, 'domain'):
81                    if hasattr(feature.domain, 'domainComplement'):
82                        if isinstance(feature.domain.domainComplement, Parser.Grid):
83                                for ordinate in feature.domain.domainComplement.ordinates:
84                                    #search arrayDescriptors for a match.
85                                    for arrayDescriptor  in self.dataset.arrayDescriptors:
86                                        print arrayDescriptor.id
87                                        if arrayDescriptor.id == ordinate.axisValues[1:]:  #remove '#'
88                                            ordinate.axisValues  = arrayDescriptor
89                                            break
90                                       
91        #uom may reference UnitDefinitions. Replace these too
92        if hasattr(self.dataset,'arrayDescriptors'):
93            for feature in self.dataset.featureCollection.members:
94                if hasattr (feature, 'rangeSet'):
95                    if hasattr (feature.rangeSet, 'aggregatedArray'):
96                         if hasattr(feature.rangeSet.aggregatedArray, 'uom'):
97                             for definition in self.dataset.unitDefinitions.definitionMembers:
98                                 if definition.id== feature.rangeSet.aggregatedArray.uom[1:]:
99                                     feature.rangeSet.aggregatedArray.uom=definition
100                                     break
101        #TODO need to also take into account the fact that UnitDefinitions could be externally defined.
102       
103        return self.dataset
Note: See TracBrowser for help on using the repository browser.