source: TI02-CSML/trunk/newparser/test.py @ 655

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

Point Feature complete, Namespaces fixed, EnvelopeWithTimeFeature? & boundedBy added

Line 
1"""Test for GML/CSML parsing code
2"""
3
4from cElementTree import *
5from Parser import *
6from xml.dom.minidom import parseString
7import elementtree.ElementTree as etree
8import sys
9
10
11#this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
12etree._namespace_map.update({
13    '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'})
14
15
16def removeInlineNS(csmlstring):
17    # removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element.
18    #remove all
19    csmlstring=csmlstring.replace(' xmlns:csml="http://ndg.nerc.ac.uk/csml"','')
20    csmlstring=csmlstring.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"','')
21    csmlstring=csmlstring.replace('xmlns:om="http://www.opengis.net/om"','')
22    csmlstring=csmlstring.replace('xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"','')
23    csmlstring=csmlstring.replace('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','')
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   
32   
33   
34
35   
36    return csmlstring
37##############################################################################
38# ElementTree-based parser test
39##############################################################################
40# 1. Test parsing from CSML file
41tree = ElementTree(file='example.xml')
42dataset = Dataset()
43dataset.fromXML(tree.getroot())
44#csml = dataset.toXML(Element('Dataset'))
45csml = dataset.toXML()
46
47strCSML= parseString(tostring(csml)).toprettyxml()
48strCSML=removeInlineNS(strCSML)
49print strCSML
50sys.exit() # stop here.
51
52##############################################################################
53#2. Test construction of objects from scratch
54
55# AW changes made to DL code:
56#  (NB: all these derive from the underlying UML model)
57#  * AbstractFileExtract.arraySize is an array []
58#  * Dataset.metaDataProperty is an array []
59#  * Omit empty attributes entirely
60#  * Dataset.arrayDescriptors is an array holding all file extract objects
61
62#MetaDataProperty Object
63md = MetaDataProperty()
64setattr(md, 'href', 'http://ndg.nerc.ac.uk/Metadata/home/users/domlowe/CSML/demodata')
65
66#An empty list to hold a collection of NASAAmesExtracts:
67nalist=[]
68
69#Create a NASAAmesExtract
70na1 = NASAAmesExtract()
71#set attributes:
72setattr(na1, 'id', 'naextract_one')
73setattr(na1, 'variableName', 'temp')
74setattr(na1, 'index', '1')
75setattr(na1, 'fileName', '/blah/testfile.na')
76setattr(na1, 'arraySize', [50])
77setattr(na1, 'uom', 'celsius')
78#setattr(na1, 'numericType', None)
79#setattr(na1, 'numericTransform', None)
80#setattr(na1,'regExpTransform',None)
81#append NASAAmesExtract to list
82nalist.append(na1)
83
84#Create a second NASAAmesExtract
85na2 = NASAAmesExtract()
86#set attributes:
87setattr(na2, 'id', 'naextract_two')
88setattr(na2, 'variableName', 'pressure')
89setattr(na2, 'index', '1')
90setattr(na2, 'fileName', '/blah/testfile2.na')
91setattr(na2, 'arraySize', [50])
92setattr(na2, 'uom', 'mb')
93#setattr(na2, 'numericType', None)
94#setattr(na2, 'numericTransform', None)
95#setattr(na2,'regExpTransform',None)
96#append second NASAAmesExtract to list
97nalist.append(na2)
98
99#An empty list to hold a collection of NetCDFExtracts:
100nclist=[]
101
102#Create a NetCDFExtract
103nc1 = NetCDFExtract()
104#set attributes:
105setattr(nc1, 'id', 'ncextract_one')
106setattr(nc1, 'variableName', 'temp2')
107setattr(nc1, 'fileName', '/blah/testfile.nc')
108setattr(nc1, 'arraySize', [100])
109setattr(nc1, 'uom', 'celsius')
110#setattr(nc1, 'numericType', None)
111#setattr(nc1, 'numericTransform', None)
112#setattr(nc1,'regExpTransform',None)
113#append NetCDFExtract to list
114nclist.append(nc1)
115
116
117
118#Create an Empty Dataset
119ds = Dataset()
120#Set objects as attributes of dataset
121setattr(ds, 'metaDataProperty', [md])
122setattr(ds, 'arrayDescriptors', nalist+nclist)
123
124
125csml = ds.toXML()
126print parseString(tostring(csml)).toprettyxml()
Note: See TracBrowser for help on using the repository browser.