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

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

parsing from elementree (parseElemTree) method added, and speeded up getAttr method

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