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

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

added code to resolve hrefs and srsName refs to real objects

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                                feature.parameter.href =None
54                                feature.parameter=definition
55       
56        #if there are embedded reference system definitions then
57        #for every DomainReference and DomainComplement need to resolve any srsNames
58        if hasattr(self.dataset, 'referenceSystemDefinitions'):
59            for feature in self.dataset.featureCollection.members:
60                if hasattr(feature, 'domain'):  #Trajectory features don't have domains..
61                    if hasattr(feature.domain.domainReference, 'srsName'):
62                        #search reference definitions for a match.
63                        for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
64                            if definition.id == feature.domain.domainReference.srsName[1:]:
65                                #remove srsName attribute and replace with Object.
66                                pass #Leave this for now.
67                    if hasattr(feature.domain, 'domainComplement'):
68                        if hasattr(feature.domain.domainComplement, 'srsName'):
69                            #search reference definitions for a match.
70                            for definition in self.dataset.referenceSystemDefinitions.definitionMembers:
71                                if definition.id == feature.domain.domainComplement.srsName[1:]:
72                                        #remove srsName attribute and replace with Object.
73                                    pass #Leave this for now.
74       
75       #NetCDFExtracts etc may be referenced e.g. as <axisvalues> element
76        if hasattr(self.dataset, 'arrayDescriptors'):
77            for feature in self.dataset.featureCollection.members:
78               if isinstance(feature.domain.domainComplement, Parser.Grid):
79                    for ordinate in feature.domain.domainComplement.ordinates:
80                        #search arrayDescriptors for a match.
81                        for arrayDescriptor  in self.dataset.arrayDescriptors:
82                            print arrayDescriptor.id
83                       
84                            if arrayDescriptor.id == ordinate.axisValues[1:]:  #remove '#'
85                                ordinate.axisValues = "banana"#arrayDescriptor
86
87       
88           
89                 
90        return self.dataset
Note: See TracBrowser for help on using the repository browser.