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

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

ongoing changes to csml2moles.py - convenient time to commit

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