Changeset 2168 for TI02-CSML/trunk


Ignore:
Timestamp:
14/02/07 14:54:40 (13 years ago)
Author:
domlowe
Message:

updated csml2moles.py for version 2 schema, also some slight changes to molesreadwrite.py

Location:
TI02-CSML/trunk/csml/csml2Moles
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/csml/csml2Moles/csml2moles.py

    r1838 r2168  
    1515        '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'}) 
    1616 
    17 def _strToDate(ymd): 
     17def strToDate(ymd): 
    1818    '''given a string in form '2006-01-01' returns a datetime object''' 
    1919    return datetime.date(*map(int, ymd.split('-'))) 
     
    5050            self.term=somename 
    5151            self.vocab='' 
    52  
    5352class EnvelopeAggregator(object): 
    5453    def __init__(self,envelope): 
    5554        '''start with aggregated envelope equal to the initialising envelope 
    5655        envelope must be of type csml.parser.EnvelopeWithTimePeriod''' 
    57         self.envelope=envelope 
    58         self.minX=self.envelope.lowerCorner.vals.split()[0] 
    59         self.minY=self.envelope.lowerCorner.vals.split()[1] 
    60         self.maxX=self.envelope.upperCorner.vals.split()[0] 
    61         self.maxY=self.envelope.upperCorner.vals.split()[1] 
    62         self.t1= _strToDate(self.envelope.timePosition) 
    63         self.t2= _strToDate(self.envelope.timePosition) 
     56        self.envelope=envelope  
     57        self.minX=self.envelope.lowerCorner.CONTENT.split()[0] 
     58        self.minY=self.envelope.lowerCorner.CONTENT.split()[1] 
     59        self.maxX=self.envelope.upperCorner.CONTENT.split()[0] 
     60        self.maxY=self.envelope.upperCorner.CONTENT.split()[1] 
     61        self.t1= strToDate(self.envelope.timePosition[0].CONTENT) 
     62        self.t2= strToDate(self.envelope.timePosition[1].CONTENT) 
    6463  
    6564    def _compareLowerCorners(self,lowerCorner): 
    66         minX,minY=lowerCorner.vals.split()[0],lowerCorner.vals.split()[1] 
     65        minX,minY=lowerCorner.CONTENT.split()[0],lowerCorner.CONTENT.split()[1] 
    6766        if float(minX) < float(self.minX): 
    68             self.envelope.lowerCorner.vals=str(minX +' '+ self.minY) 
     67            self.envelope.lowerCorner.CONTENT=str(minX +' '+ self.minY) 
    6968            self.minX=minX 
    7069        if float(minY) < float(self.minY): 
    71             self.envelope.lowerCorner.vals=str(self.minX +' '+ minY) 
     70            self.envelope.lowerCorner.CONTENT=str(self.minX +' '+ minY) 
    7271            self.minY=minY 
    7372             
    7473    def _compareUpperCorners(self,upperCorner): 
    75         maxX,maxY=upperCorner.vals.split()[0],upperCorner.vals.split()[1] 
     74        maxX,maxY=upperCorner.CONTENT.split()[0],upperCorner.CONTENT.split()[1] 
    7675        if float(maxX) > float(self.maxX): 
    77             self.envelope.upperCorner.vals=str(maxX +' '+ self.maxY) 
     76            self.envelope.upperCorner.CONTENT=str(maxX +' '+ self.maxY) 
    7877            self.maxX=maxX 
    7978        if float(maxY) > float(self.maxY): 
    80             self.envelope.upperCorner.vals=str(self.maxX +' '+ maxY) 
     79            self.envelope.upperCorner.CONTENT=str(self.maxX +' '+ maxY) 
    8180            self.maxY=maxY 
    8281             
    8382    def _compareLowerTimes(self,timepos): 
    84         t=_strToDate(timepos) 
     83        t=strToDate(timepos) 
    8584        if t<self.t1: 
    8685            self.t1=t 
    8786         
    8887    def _compareUpperTimes(self,timepos): 
    89         t=_strToDate(timepos) 
     88        t=strToDate(timepos) 
    9089        if t>self.t2: 
    9190            self.t2=t  
     
    9998            self._compareLowerCorners(envtocheck.lowerCorner) 
    10099            self._compareUpperCorners(envtocheck.upperCorner) 
    101             self._compareLowerTimes(envtocheck.timePosition) 
    102             self._compareUpperTimes(envtocheck.timePosition2) 
     100            self._compareLowerTimes(envtocheck.timePosition[0].CONTENT) 
     101            self._compareUpperTimes(envtocheck.timePosition[1].CONTENT) 
    103102     
    104103    def getAggregatedEnvelope(self): 
    105         self.envelope.timePosition=str(self.t1)  #convert from datetime types 
    106         self.envelope.timePosition2=str(self.t2) 
     104        self.envelope.timePosition=[csml.parser.csString(str(self.t1)), csml.parser.csString(str(self.t2))] #convert from datetime types 
    107105        return self.envelope 
    108106 
     
    152150        #print dir(dataset.featureCollection) 
    153151         
    154         for feature in dataset.featureCollection.members: 
     152        for feature in dataset.featureCollection.featureMembers: 
    155153            if hasattr(feature.parameter, 'href'): 
    156154                sn=StandardName(feature.parameter.href) 
    157155                if hasattr(feature, 'description'): 
    158                     sn.text=feature.description 
     156                    sn.text=feature.description.CONTENT 
    159157            VTID=M.dgValidTermID( ParentListID=sn.vocab, TermID=sn.term) 
    160158             
     
    165163    #get aggregated envelope: 
    166164    finalEnvelope=aggregator.getAggregatedEnvelope() 
    167     limitN=finalEnvelope.upperCorner.vals.split()[0] 
    168     limitS=finalEnvelope.lowerCorner.vals.split()[0] 
    169     limitE=finalEnvelope.upperCorner.vals.split()[1] 
    170     limitW=finalEnvelope.lowerCorner.vals.split()[1] 
     165    limitN=finalEnvelope.upperCorner.CONTENT.split()[0] 
     166    limitS=finalEnvelope.lowerCorner.CONTENT.split()[0] 
     167    limitE=finalEnvelope.upperCorner.CONTENT.split()[1] 
     168    limitW=finalEnvelope.lowerCorner.CONTENT.split()[1] 
     169    tmin=finalEnvelope.timePosition[0].CONTENT 
     170    tmax=finalEnvelope.timePosition[1].CONTENT 
     171     
    171172    #get other aggregations: 
    172173     
    173  
    174174     
    175175    ''' The MOLES document: 
     
    188188 
    189189    #create coverage 
     190    dRng=M.DateRange(DateRangeStart=tmin, DateRangeEnd=tmax) 
    190191    dgBB=M.dgBoundingBox(LimitNorth = limitN, LimitSouth=limitS,LimitEast=limitE, LimitWest=limitW) 
    191     dgSc=M.dgSpatialCoverage(dgBoundingBox=dgBB) 
    192     dgTc=M.dgTemporalCoverage(mNS) 
    193     dgST=M.dgSpatioTemporalCoverage(dgSpatialCoverage = dgSc, dgTemporalCoverage=dgTc) 
     192    dgSc=M.dgSpatialCoverage(BoundingBox=dgBB) 
     193    dgTc=M.dgTemporalCoverage(DateRange=dRng) 
     194    dgSrng=M.dgSpatioTemporalRange(dgSpatialCoverage = dgSc, dgTemporalCoverage=dgTc) 
     195    dgST=M.dgSpatioTemporalCoverage(dgSpatioTemporalRange=dgSrng) 
    194196    dgCv=M.dgCoverage(dgSpatioTemporalCoverage=dgST) 
    195      
     197            
    196198    #create data summary: 
    197     DS = M.dgDataSummary(dgCoverage=dgCv, dgParameterSummary=parameterSummaries) 
     199    DS = M.dgDataSummary(dgDataCoverage=dgCv, dgParameterSummary=parameterSummaries) 
    198200    #create data entity: 
    199201    dgDE= M.dgDataEntity(dgDataGranule=datagranules, dgDataSummary=DS) 
     202  
    200203     
    201204    #create metadata description: 
     
    215218    f.close() 
    216219 
     220    sys.exit() 
    217221    
    218222    #produce XML again (round trip) 
     
    222226    molestree=dgMeta.toXML() 
    223227    moles=csml.parser_extra.PrettyPrint(molestree) 
     228    print molestree 
    224229    print '\n \n \n AFTER PARSING' 
    225230    print moles    
    226     print dir(molestree) 
    227     
     231 
    228232if __name__=='__main__': 
    229233    main() 
  • TI02-CSML/trunk/csml/csml2Moles/molesReadWrite.py

    r2166 r2168  
    6464         
    6565    def __checkChildren(self, parentclassname, elem):      
    66         print 'checking %s'%parentclassname 
    6766        #if parentclassname == 'dgMetadataProvenance': 
    6867            #pdb.set_trace() 
     
    111110        self.sequences['dummy']=None 
    112111     
    113     def lookupOrder(self, dict,classname): 
    114         '''takes the attributes in a dictionary and orders them according to the schema sequence''' 
    115         try:         
    116             order=self.sequences[classname] 
    117         except KeyError: 
    118             order = [] 
    119             for key in dict: 
    120                 #print key error 
    121                 print 'KEY ERROR %s'%classname 
    122                 if key is not 'schema': 
    123                     if key is not 'ns': 
    124                          order.append(key) # if it can't be found an unordered list is returned from the original dictionary items 
    125             #print 'returning %s'%order 
    126         return order 
    127  
    128  
    129 #this is the old XML schema class. hopefully we can throw this away if the other one works better 
    130 class OLDxmlSchema(object): 
    131     ''' this class creates a mapping of the xml schema sequences so that it can be referred to when  
    132     writing out a new moles document - this enables elements to be written in the order specified by the schema 
    133     If a particular sequence mapping can't be found then the elements will be written in whichever order python sees fit. 
    134     It tries various searches of the schema to pick out the sequences (which can be nested) 
    135     ''' 
    136     def __init__(self, schema): 
    137         self.sequences={} 
    138         def __addSequence(molesname, sequence): 
    139             '''adds a new sequences to the list, also handles xs:choice''' 
    140             seqlist=[] 
    141             for elem in sequence[:]: 
    142                 if elem.attrib.has_key('name'): 
    143                     seqlist.append(elem.attrib['name']) 
    144                 if elem.attrib.has_key('ref'): 
    145                     seqlist.append(elem.attrib['ref'].split(':')[1]) 
    146                 if elem.tag == '{http://www.w3.org/2001/XMLSchema}choice': 
    147                     for subelem in elem[:]: 
    148                         if subelem.attrib.has_key('name'): 
    149                             seqlist.append(subelem.attrib['name']) 
    150             self.sequences[molesname]=seqlist 
    151          
    152         def __checkSubElem(elem): 
    153             for subelem in elem[:]: 
    154                 if subelem.tag=='{http://www.w3.org/2001/XMLSchema}sequence': 
    155                     __addSequence(molesclassname,subelem) 
    156                     __checkSubElem(subelem) 
    157          
    158         def __checkDeepSubElem(elem): 
    159             for subelem in elem[:]: 
    160                 if subelem.tag == '{http://www.w3.org/2001/XMLSchema}complexType': 
    161                     for subsubelem in subelem[:]: 
    162                         if subsubelem.tag=='{http://www.w3.org/2001/XMLSchema}sequence': 
    163                             __addSequence(molesclassname,subsubelem) 
    164                             __checkDeepSubElem(subsubelem)     
    165  
    166         for event, elem in ET.iterparse(schema): 
    167             '''look for sequences in complexTypes''' 
    168             if elem.tag == '{http://www.w3.org/2001/XMLSchema}complexType': 
    169                 if elem.attrib.has_key('name'): 
    170                     molesclassname= elem.attrib['name'] 
    171                     if elem.attrib['name'][-4:]=='Type': 
    172                         molesclassname=molesclassname[:-4] 
    173                     __checkSubElem(elem)                     
    174  
    175             if elem.tag == '{http://www.w3.org/2001/XMLSchema}element': 
    176                 '''Look for parents of complex types - sometimes a complexType is declared but does not have a name and 
    177               the name belongs to the parent 'element' element.''' 
    178                 if elem.attrib.has_key('name'): 
    179                     molesclassname= elem.attrib['name'] 
    180                     __checkDeepSubElem(elem) 
    181  
    182             '''this bit handles the use of types. If some element has a name and a type attribute then if that type is complex it has 
    183             the same sequence as the type e.g. <xs:element name="dataModelID" type="moles:dgMetadataIDType"> 
    184             need to run iterparse again as this needs to be done when all elements have already been parsed. ''' 
    185             for event, elem in ET.iterparse(schema): 
    186                 if elem.tag == '{http://www.w3.org/2001/XMLSchema}element': 
    187                     if elem.attrib.has_key('name'): 
    188                         molesclassname= elem.attrib['name'] 
    189                         if elem.attrib.has_key('type'): 
    190                             typename=elem.attrib['type'] 
    191                             if typename[-4:]=='Type': 
    192                                  typename=typename[:-4].split(':')[1]  #may also be prefixed by moles 
    193                             try: 
    194                                 seq=self.sequences[typename] 
    195                                 self.sequences[molesclassname]=seq 
    196                             except KeyError: 
    197                                 pass  #can't find anything 
    198          
    199              
    200112    def lookupOrder(self, dict,classname): 
    201113        '''takes the attributes in a dictionary and orders them according to the schema sequence''' 
     
    342254        'dgCoverage', \ 
    343255        'dgDataCoverage',\ 
     256        'dgSpatioTemporalRange',\ 
    344257        'dgSpatioTemporalCoverage', \ 
    345258        'dgSpatialCoverage', \ 
Note: See TracChangeset for help on using the changeset viewer.