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

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

Helping csml work in python 2.5 (finding celementtree :-)

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 getSecurity(self):
50    """ returns a dictonary containing attributeAuthority: role"""
51    security=[]
52    if hasattr(self, 'accessControlPolicy'):
53        if hasattr(self.accessControlPolicy,'dgSecurityConditions'):
54            for condition in self.accessControlPolicy.dgSecurityConditions:
55                #need to test for these but ok for alpha.
56                cond=[]
57                cond.append(condition.effect)
58                cond.append(condition.simpleCondition.dgAttributeAuthority)
59                cond.append(condition.simpleCondition.attrauthRole)
60                cond.append(condition.conditionExplanationText)
61                security.append(cond)
62    return security
63
64def getFeatureList(self):
65    #returns a list of feature ids for the dataset
66    self.featureList = []
67    for member in csml.csmllibs.csmlextra.listify(self.featureCollection.featureMembers):
68            self.featureList.append(member.id)
69    return self.featureList
70   
71def getFeature(self, featureID):
72    """ returns a single feature object """
73    for member in csml.csmllibs.csmlextra.listify(self.featureCollection.featureMembers):
74            if member.id == featureID:
75                return member   
76
77def getFileExtract(self,extractID):
78        for extract in csml.csmllibs.csmlextra.listify(self.dataset.arrayDescriptors):
79            if  extract.id==extractID:
80                return extract
81
82def getBoundingBox(self):
83    ''' get boundingBox, returns the boundingBox of the CSMLFeatureCollection (assumes there is only one...)'''
84    try:
85        lower1=eval(self.featureCollection.boundedBy.lowerCorner.CONTENT.split()[0])
86        lower2=eval(self.featureCollection.boundedBy.lowerCorner.CONTENT.split()[1])
87        upper1=eval(self.featureCollection.boundedBy.upperCorner.CONTENT.split()[0])
88        upper2=eval(self.featureCollection.boundedBy.upperCorner.CONTENT.split()[1])
89        return [lower1,lower2,upper1,upper2]
90    except:
91        return []
Note: See TracBrowser for help on using the repository browser.