/[thuban]/branches/WIP-pyshapelib-bramz/Extensions/wms/parser.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Extensions/wms/parser.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2133 by joey, Wed Mar 24 19:31:54 2004 UTC revision 2167 by joey, Tue Apr 13 17:05:56 2004 UTC
# Line 52  import xml.dom.minidom Line 52  import xml.dom.minidom
52    
53  from domutils import getElementsByName, getElementByName  from domutils import getElementsByName, getElementByName
54    
55    from Thuban import _
56    
57  class WMSCapabilitiesParser:  class WMSCapabilitiesParser:
58      """      """
59      Thuban class to parse capabilities supplied as large string.      Thuban class to parse capabilities supplied as large string.
# Line 68  class WMSCapabilitiesParser: Line 70  class WMSCapabilitiesParser:
70      access = None      access = None
71      formats = None      formats = None
72      srs_discrepancies = None      srs_discrepancies = None
73        error = None
74    
75    
76      def __init__(self):      def __init__(self):
# Line 78  class WMSCapabilitiesParser: Line 81  class WMSCapabilitiesParser:
81          # Note that we must not initialise internal variables of the          # Note that we must not initialise internal variables of the
82          # class in a mutable way or it will be shared among all          # class in a mutable way or it will be shared among all
83          # instances.  None is immutable, [] is not.          # instances.  None is immutable, [] is not.
84          layers = []          self.error = []
85    
86    
87      def grok(self, data):      def grok(self, data):
# Line 89  class WMSCapabilitiesParser: Line 92  class WMSCapabilitiesParser:
92          Information should only be retrieved with the respective          Information should only be retrieved with the respective
93          get*() methods.          get*() methods.
94          """          """
95          root = xml.dom.minidom.parseString(data).documentElement          xml_dom = xml.dom.minidom.parseString(data)
96            root = xml_dom.documentElement
97    
98          # Extract the title          # Extract the title
99          foo = getElementByName(getElementByName(root, 'Service'), 'Title')          foo = getElementByName(getElementByName(root, 'Service'), 'Title')
# Line 104  class WMSCapabilitiesParser: Line 108  class WMSCapabilitiesParser:
108          # Extract fees information          # Extract fees information
109          foo = getElementByName(getElementByName(root, 'Service'), 'Fees')          foo = getElementByName(getElementByName(root, 'Service'), 'Fees')
110          if foo and len(foo.childNodes[0].data) \          if foo and len(foo.childNodes[0].data) \
111                 and lower(foo.childNodes[0].data) != 'none':                 and foo.childNodes[0].data.lower() != 'none':
112              self.fees = foo.childNodes[0].data              self.fees = foo.childNodes[0].data
113                    
114          # Extract access information          # Extract access information
115          foo = getElementByName(getElementByName(root, 'Service'),          foo = getElementByName(getElementByName(root, 'Service'),
116                                 'AccessConstraints')                                 'AccessConstraints')
117          if foo and len(foo.childNodes[0].data) \          if foo and len(foo.childNodes[0].data) \
118                 and lower(foo.childNodes[0].data) != 'none':                 and foo.childNodes[0].data.lower() != 'none':
119              self.access = foo.childNodes[0].data              self.access = foo.childNodes[0].data
120                    
121          # Extract output format information          # Extract output format information
# Line 125  class WMSCapabilitiesParser: Line 129  class WMSCapabilitiesParser:
129          self.peekLayers(getElementByName(getElementByName(          self.peekLayers(getElementByName(getElementByName(
130              root, 'Capability'), 'Layer'), -1)              root, 'Capability'), 'Layer'), -1)
131    
132            xml_dom.unlink()
133    
134    
135      def peekLayers(self, top, parent):      def peekLayers(self, top, parent):
136          """          """
# Line 149  class WMSCapabilitiesParser: Line 155  class WMSCapabilitiesParser:
155          foo = getElementByName(top, 'Title')          foo = getElementByName(top, 'Title')
156          if foo and len(foo.childNodes[0].data):          if foo and len(foo.childNodes[0].data):
157              self.layers[index]['title'] = foo.childNodes[0].data              self.layers[index]['title'] = foo.childNodes[0].data
158            else:
159                # A <Title> is required for each layer, <name> is optional
160                # See OGC 01-068r3, 7.1.4.5.1 and 7.1.4.5.2
161                self.error.append(_("No title found for layer #%d") % index)
162    
163          foo = getElementByName(top, 'Name')          foo = getElementByName(top, 'Name')
164          if foo and len(foo.childNodes[0].data):          if foo and len(foo.childNodes[0].data):
# Line 160  class WMSCapabilitiesParser: Line 170  class WMSCapabilitiesParser:
170                  if srs[0:5] == 'EPSG:':                  if srs[0:5] == 'EPSG:':
171                      srs = srs[5:]                      srs = srs[5:]
172                  try:                  try:
173                      self.layers[index]['srs'].append(srs)                      int(srs)
174                  except KeyError:                      try:
175                      self.layers[index]['srs'] = [srs]                          self.layers[index]['srs'].append(srs)
176                        except KeyError:
177                            self.layers[index]['srs'] = [srs]
178                    except ValueError:
179                        if srs[0:4].upper() == 'AUTO' \
180                               or srs[0:4].upper() == 'NONE':
181                            try:
182                                self.layers[index]['_srs_'].append(srs)
183                            except KeyError:
184                                self.layers[index]['_srs_'] = [srs]
185                        else:
186                            self.error.append(_("SRS '%s' is not numerical and not"
187                                                " AUTO/NONE in layer '%s'") \
188                                              % (srs, self.layers[index]['title']))
189    
190          foo = getElementByName(top, 'LatLonBoundingBox')          foo = getElementByName(top, 'LatLonBoundingBox')
191          if foo is not None:          if foo is not None:
# Line 183  class WMSCapabilitiesParser: Line 206  class WMSCapabilitiesParser:
206                  self.layers[index]['bbox'][srs][corner] \                  self.layers[index]['bbox'][srs][corner] \
207                      = foo.attributes.get(corner).nodeValue                      = foo.attributes.get(corner).nodeValue
208                            
         # Check for integrity  
         self.checkLayerSRS(index)  
   
209          # Traverse subsidiary layers          # Traverse subsidiary layers
210          sublayer = getElementsByName(top, 'Layer')          sublayer = getElementsByName(top, 'Layer')
211          for l in sublayer:          for l in sublayer:
212              self.peekLayers(l, index)              self.peekLayers(l, index)
213    
214    
     def checkLayerSRS(self, index):  
         """  
         Checks the integrity of the underlying XML data.  
   
         This is done by comparing the <SRS> elements with the  
         calculated list from the BoundingBox elements.  
   
         index -- position in the layers array to check  
         """  
   
         pivot = index  
         calculated = []  
         while pivot != -1:  
             if 'bbox' in self.layers[pivot]:  
                 for srs in self.layers[pivot]['bbox'].keys():  
                     if srs not in calculated:  
                         calculated.append(srs)  
             pivot = self.layers[pivot]['parent']  
   
         pivot = index  
         specified = []  
         while pivot != -1:  
             if 'srs' in self.layers[pivot]:  
                 for srs in self.layers[pivot]['srs']:  
                     if srs not in specified:  
                         specified.append(srs)  
             pivot = self.layers[pivot]['parent']  
   
         equal = True  
         # Check for same number of elements  
         if len(calculated) != len(specified):  
             equal = False  
   
         # Loop through all elements for existance  
         for elm in calculated:  
             if elm not in specified:  
                 equal = False  
   
         if not equal:  
             if self.srs_discrepancies is None:  
                 self.srs_discrepancies = []  
             if 'name' in self.layers[index]:  
                 id = "name:%s" % self.layers[index]['name']  
             else:  
                 id = "title:%s" % self.layers[index]['title']  
             self.srs_discrepancies.append(id)  
   
   
215      def getTitle(self):      def getTitle(self):
216          """          """
217          Returns the main title of the WMS object.          Returns the main title of the WMS object.
# Line 385  class WMSCapabilitiesParser: Line 357  class WMSCapabilitiesParser:
357          - Layers may optionally add to the global SRS list, or to the          - Layers may optionally add to the global SRS list, or to the
358            list inherited from a parent layer.            list inherited from a parent layer.
359    
360            - A server which has the ability to transform data to
361              different SRSes may choose not to provide an explicit
362              BoundingBox for every possible SRS available for each Layer.
363              Thus the list of <SRS> elements are authoritative.
364    
365          This implementation returns the list of SRS for the given          This implementation returns the list of SRS for the given
366          layer, calculated by looking at BoundingBoxes defined in the          layer, calculated by looking at BoundingBoxes defined in the
367          named layer and all layers higher in the hierarchy up to the          named layer and all layers higher in the hierarchy up to the
# Line 400  class WMSCapabilitiesParser: Line 377  class WMSCapabilitiesParser:
377    
378          result = []          result = []
379          while pivot != -1:          while pivot != -1:
380              if 'bbox' in self.layers[pivot]:              if 'srs' in self.layers[pivot]:
381                  for srs in self.layers[pivot]['bbox'].keys():                  for srs in self.layers[pivot]['srs']:
382                      if srs not in result:                      if srs not in result:
383                          result.append(srs)                          result.append(srs)
384              pivot = self.layers[pivot]['parent']              pivot = self.layers[pivot]['parent']
# Line 525  if __name__ == "__main__": Line 502  if __name__ == "__main__":
502    
503      import os      import os
504    
505        sample = "test/sample.xml"
506      try:      try:
507          f = open("test/sample.xml", "r")          f = open(sample, "r")
508      except IOError:      except IOError:
509          try:          try:
510              f = open(os.path.dirname(__file__) + "/test/sample.xml", "r")              f = open(os.path.dirname(__file__) + "/" + sample, "r")
511          except IOError:          except IOError:
512              print "Cannot open sample.xml for reading"              print "Cannot open %s for reading" % sample
513    
514      if f is not None:      if f is not None:
515          sample = f.read();          sample = f.read();

Legend:
Removed from v.2133  
changed lines
  Added in v.2167

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26