source: TI02-CSML/trunk/Examples/parsing/csml2moles.py @ 1505

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/Examples/parsing/csml2moles.py@1505
Revision 1505, 3.5 KB checked in by domlowe, 13 years ago (diff)

bounding box aggregation working

Line 
1#!/usr/bin/env python
2
3import sys,getopt
4import csml.parser
5import cElementTree
6import elementtree.ElementTree as etree
7#this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
8etree._namespace_map.update({
9        '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'})
10
11           
12def _getEnvelopefromFile(f):
13    #parses a file and gets the bounding box from the feature collection
14    tree = cElementTree.ElementTree(file=f)
15    dataset = csml.parser.Dataset()
16    dataset.fromXML(tree.getroot())
17    envelope= dataset.featureCollection.boundedBy
18    return envelope
19
20class EnvelopeAggregator(object):
21    def __init__(self,envelope):
22        #start with aggregated envelope equal to the initialising envelope
23        #envelope must be of type csml.parser.EnvelopeWithTimePeriod
24        self.envelope=envelope
25        self.minX=self.envelope.lowerCorner.vals.split()[0]
26        self.minY=self.envelope.lowerCorner.vals.split()[1]
27        self.maxX=self.envelope.upperCorner.vals.split()[0]
28        self.maxY=self.envelope.upperCorner.vals.split()[1]
29 
30    def _compareLowerCorners(self,lowerCorner):
31        minX,minY=lowerCorner.vals.split()[0],lowerCorner.vals.split()[1]
32        if float(minX) < float(self.minX):
33            self.envelope.lowerCorner.vals=str(minX +' '+ self.minY)
34            self.minX=minX
35        if float(minY) < float(self.minY):
36            self.envelope.lowerCorner.vals=str(self.minX +' '+ minY)
37            self.minY=minY
38    def _compareUpperCorners(self,upperCorner):
39        maxX,maxY=upperCorner.vals.split()[0],upperCorner.vals.split()[1]
40        if float(maxX) > float(self.maxX):
41            self.envelope.upperCorner.vals=str(maxX +' '+ self.maxY)
42            self.maxX=maxX
43        if float(maxY) > float(self.maxY):
44            self.envelope.upperCorner.vals=str(self.maxX +' '+ maxY)
45            self.maxY=maxY
46        return upperCorner
47    def _compareLowerTimes(self,timeposlist):
48        return timeposlist
49    def _compareUpperTimes(self,timeposlist):
50        return timeposlist
51   
52    def compareEnvelope(self,envtocheck):
53        #compares new envelope, and if necessary changes the aggregated envelope.
54        if envtocheck.srsName!=self.envelope.srsName:
55            print 'can currently only perform aggregation within the same spatio-temporal reference system.'
56            sys.exit()
57        else: 
58            self._compareLowerCorners(envtocheck.lowerCorner)
59            self._compareUpperCorners(envtocheck.upperCorner)
60            self._compareLowerTimes(envtocheck.timePosition)
61            self._compareUpperTimes(envtocheck.timePosition2)
62   
63    def getAggregatedEnvelope(self):
64        return self.envelope
65   
66
67
68def main(args=None):
69    #Get command line arguments, should be csml files: 'file1' 'file2'
70    if args:
71        sys.argv =args
72    csmlfilelist= sys.argv[1:]
73    #use the first file to set up the aggregator:
74    env=_getEnvelopefromFile(csmlfilelist[0])
75    aggregator=EnvelopeAggregator(env)
76    #then aggregate across the rest of the files in the list
77    csmlfilelist=csmlfilelist[1:]
78    for f in csmlfilelist:
79        aggregator.compareEnvelope(_getEnvelopefromFile(f))
80    finalEnvelope=aggregator.getAggregatedEnvelope()
81
82    #test output
83    print finalEnvelope.lowerCorner.vals
84    print finalEnvelope.upperCorner.vals
85    print finalEnvelope.timePosition
86    print finalEnvelope.timePosition2
87   
88if __name__=='__main__':
89    main()
Note: See TracBrowser for help on using the repository browser.