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

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

no need to import getopt

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