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

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

Bounding boxes now correctly determined by scanner

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