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 | |
---|
8 | import Parser |
---|
9 | import sys |
---|
10 | import pickle |
---|
11 | |
---|
12 | #string manipulation function. may as well go in this file for now. |
---|
13 | def 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 | |
---|
34 | class 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 | |
---|
57 | #if there are embedded reference system definitions then |
---|
58 | #for every DomainReference and DomainComplement need to resolve any srsNames |
---|
59 | if hasattr(self.dataset, 'referenceSystemDefinitions'): |
---|
60 | for feature in self.dataset.featureCollection.members: |
---|
61 | if hasattr(feature, 'domain'): #Trajectory features don't have domains.. |
---|
62 | if hasattr(feature.domain.domainReference, 'srsName'): |
---|
63 | #search reference definitions for a match. |
---|
64 | for definition in self.dataset.referenceSystemDefinitions.definitionMembers: |
---|
65 | if definition.id == feature.domain.domainReference.srsName[1:]: |
---|
66 | #remove srsName attribute and replace with Object. |
---|
67 | pass #Leave this for now. |
---|
68 | if hasattr(feature.domain, 'domainComplement'): |
---|
69 | if hasattr(feature.domain.domainComplement, 'srsName'): |
---|
70 | #search reference definitions for a match. |
---|
71 | for definition in self.dataset.referenceSystemDefinitions.definitionMembers: |
---|
72 | if definition.id == feature.domain.domainComplement.srsName[1:]: |
---|
73 | #remove srsName attribute and replace with Object. |
---|
74 | pass #Leave this for now. |
---|
75 | |
---|
76 | #NetCDFExtracts etc may be referenced e.g. as <axisvalues> element. Need to fix these too. |
---|
77 | if hasattr(self.dataset, 'arrayDescriptors'): |
---|
78 | for feature in self.dataset.featureCollection.members: |
---|
79 | if hasattr (feature, 'domain'): |
---|
80 | if hasattr(feature.domain, 'domainComplement'): |
---|
81 | if isinstance(feature.domain.domainComplement, Parser.Grid): |
---|
82 | for ordinate in feature.domain.domainComplement.ordinates: |
---|
83 | #search arrayDescriptors for a match. |
---|
84 | for arrayDescriptor in self.dataset.arrayDescriptors: |
---|
85 | print arrayDescriptor.id |
---|
86 | if arrayDescriptor.id == ordinate.axisValues[1:]: #remove '#' |
---|
87 | ordinate.axisValues = arrayDescriptor |
---|
88 | break |
---|
89 | |
---|
90 | |
---|
91 | return self.dataset |
---|