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

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

outline of data-granule aggregation tool to aggregaate bounding boxes

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
26    def _compareLowerCorners(self,lowerCorner):
27        return lowerCorner
28    def _compareUpperCorners(self,upperCorner):
29        return upperCorner
30    def _compareLowerTimes(self,timeposlist):
31        return timeposlist
32    def _compareUpperTimes(self,timeposlist):
33        return timeposlist
34   
35    def compareEnvelope(self,envtocheck):
36        #compares new envelope, and if necessary changes the aggregated envelope.
37        if envtocheck.srsName!=self.envelope.srsName:
38            print 'can currently only perform aggregation within the same spatio-temporal reference system.'
39            sys.exit()
40        else: 
41            self.envelope.lowerCorner=self._compareLowerCorners(envtocheck.lowerCorner)
42            self.envelope.upperCorner=self._compareUpperCorners(envtocheck.upperCorner)
43            self.envelope.timePosition=self._compareLowerTimes(envtocheck.timePosition)
44            self.envelope.timePosition2=self._compareUpperTimes(envtocheck.timePosition2)
45   
46    def getAggregatedEnvelope(self):
47        return self.envelope
48   
49
50
51def main(args=None):
52    #Get command line arguments, should be csml files: 'file1' 'file2'
53    if args:
54        sys.argv =args
55    csmlfilelist= sys.argv[1:]
56    #use the first file to set up the aggregator:
57    env=_getEnvelopefromFile(csmlfilelist[0])
58    aggregator=EnvelopeAggregator(env)
59    #then aggregate across the rest of the files in the list
60    csmlfilelist=csmlfilelist[1:]
61    for f in csmlfilelist:
62        aggregator.compareEnvelope(_getEnvelopefromFile(f))
63    finalEnvelope=aggregator.getAggregatedEnvelope()
64
65if __name__=='__main__':
66    main()
Note: See TracBrowser for help on using the repository browser.