source: TI02-CSML/trunk/csml/API/ops_Dataset.py @ 2692

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

try except added to sliceing code

Line 
1''' ops_Dataset  contains operations for root Dataset class'''
2
3import codecs
4import csml.csmllibs.xmlEncoding
5import csml.parser_extra
6import csml.API.existDB
7try: #python 2.5
8    from xml.etree import cElementTree as ET
9except ImportError:
10    try:
11        # if you've installed it yourself it comes this way
12        import cElementTree as ET
13    except ImportError:
14        # if you've egged it this is the way it comes
15        from elementtree import cElementTree as ET
16
17def testmethod(self):
18    print 'testmethod for dataset'
19    return 'testmethod dataset'
20
21def parse(self, csmlfile, xmldb=None, collection='/db/ndg_A_metadata', id_field = 'id', nslong='http://ndg.nerc.ac.uk/csml', nsshort='csml', passwords=None):
22    """must be provided with csmlfile   parses the csmlfile into self """
23    if xmldb is not None:     
24        #use xml db (exist)
25        COLLECTION=collection
26        DOC_ID=csmlfile
27        ID_FIELD=id_field
28        NSLONG=nslong
29        NSSHORT=nsshort
30        if passwords is not None:
31            existDB = csml.API.existDB.ndg_eXist(db=xmldb, passwordFile=passwords) 
32        else:
33            existDB = csml.API.existDB.ndg_eXist(db=xmldb) 
34        csmlfile=existDB.getXMLRecord(COLLECTION,ID_FIELD,DOC_ID,att=1,namespaceLong=NSLONG, namespaceShort=NSSHORT)
35        elem=ET.fromstring(csmlfile)
36        tree=ET.ElementTree(element=elem)
37    else:
38        #use filesystem
39        #determine encoding
40        f = open(csmlfile)
41        startoffile=f.readline()
42        encoding=csml.csmllibs.xmlEncoding.autoDetectXMLEncoding(startoffile)
43        f.close()
44        #parse with correct encoding
45        tree = csml.parser_extra.encodingParser(csmlfile,encoding)
46    self.fromXML(tree.getroot())
47    self =csml.parser_extra.ParserPostProcessor(self).resolveReferences()
48   
49def parseElemTree(self, elemtree):
50    ''''alternative parsing method, supply with elementtree instance instead of file/uri'''
51    #import time
52    #t1=time.time()
53   
54    self.fromXML(elemtree)
55    #print 'time 1: ',time.time()-t1     
56    self =csml.parser_extra.ParserPostProcessor(self).resolveReferences()
57   
58def getSecurity(self):
59    """ returns a dictonary containing attributeAuthority: role"""
60    security=[]
61    if hasattr(self, 'accessControlPolicy'):
62        if hasattr(self.accessControlPolicy,'dgSecurityConditions'):
63            for condition in self.accessControlPolicy.dgSecurityConditions:
64                #need to test for these but ok for alpha.
65                cond=[]
66                cond.append(condition.effect)
67                cond.append(condition.simpleCondition.dgAttributeAuthority)
68                cond.append(condition.simpleCondition.attrauthRole)
69                cond.append(condition.conditionExplanationText)
70                security.append(cond)
71    return security
72
73def getFeatureList(self):
74    #returns a list of feature ids for the dataset
75    self.featureList = []
76    for member in csml.csmllibs.csmlextra.listify(self.featureCollection.featureMembers):
77            self.featureList.append(member.id)
78    return self.featureList
79   
80def getFeature(self, featureID):
81    """ returns a single feature object """
82    for member in csml.csmllibs.csmlextra.listify(self.featureCollection.featureMembers):
83            if member.id == featureID:
84                return member   
85
86def getFileExtract(self,extractID):
87        for extract in csml.csmllibs.csmlextra.listify(self.dataset.arrayDescriptors):
88            if  extract.id==extractID:
89                return extract
90
91def getBoundingBox(self):
92    ''' get boundingBox, returns the boundingBox of the CSMLFeatureCollection (assumes there is only one...)'''
93    try:
94        lower1=eval(self.featureCollection.boundedBy.lowerCorner.CONTENT.split()[0])
95        lower2=eval(self.featureCollection.boundedBy.lowerCorner.CONTENT.split()[1])
96        upper1=eval(self.featureCollection.boundedBy.upperCorner.CONTENT.split()[0])
97        upper2=eval(self.featureCollection.boundedBy.upperCorner.CONTENT.split()[1])
98        return [lower1,lower2,upper1,upper2]
99    except:
100        return []
Note: See TracBrowser for help on using the repository browser.