source: TI02-CSML/trunk/csml2MolesStuff/csml2moles.py @ 1607

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

moving csml2moles.py into different directory

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 paths to csml files
85    e.g. python csml2moles.py csmlfile1.xml csmlfile2.xml csmlfile3.xml
86    main() will aggregate the spatial and temporal bounding box across all the featureCollections.
87    Currently only one featureCollection per csml file is supported '''
88    if args:
89        sys.argv =args
90    csmlfilelist= sys.argv[1:]
91    #use the first file to set up the aggregator:
92    env=_getEnvelopefromFile(csmlfilelist[0])
93    aggregator=EnvelopeAggregator(env)
94    #then aggregate across the rest of the files in the list
95    csmlfilelist=csmlfilelist[1:]
96    for f in csmlfilelist:
97        aggregator.compareEnvelope(_getEnvelopefromFile(f))
98    finalEnvelope=aggregator.getAggregatedEnvelope()
99    #test output - need to decide what we want to do with the aggregated envelope:
100    #options include; Create an empty moles record, write to a text file, add to an existing moles record..?
101    print finalEnvelope.lowerCorner.vals
102    print finalEnvelope.upperCorner.vals
103    print finalEnvelope.timePosition
104    print finalEnvelope.timePosition2
105   
106if __name__=='__main__':
107    main()
Note: See TracBrowser for help on using the repository browser.