def _getLegendImages(self, infoList):

        legendImages = []
        for i in range(len(infoList)):
            j = i + 1
            info = infoList[i]

            if info.legendURL is None:
                continue

            req = urllib2.Request(info.legendURL)
            req.add_header('Cookie', request.headers.get('Cookie', ''))

            try:
                filehandle = openURL(req)
                buffer = StringIO(filehandle.read())
                im = Image.open(buffer)
                filehandle.close()
            except Exception, exc:
                log.error("Error retrieving legend from URL %s: %s" % (info.legendURL, exc.__str__()))
            else:
                log.debug("info.legendURL = %s, im.size = %s" % (info.legendURL, im.size,))

                labelImage = self._getLabelImage(j)

                legendImages.append( (labelImage, im) )
def buildImage(layerInfoList, imageCache):
    """Fetches and merges the figure images.
    @param layerInfoList: list of LayerInfo defining layers to include in the figure
    @param imageCache: cache of images that have been generated for layers (used for animations
           where dimensions only vary in the layer over which animation occurs)
    """
    images = []
    st = time.time()
    
    log.debug("Starting buildImage")
    
    size = None
    layerIndex = 0
    for layerInfo in layerInfoList:
        if imageCache == None or imageCache[layerIndex] == None:
            requestURL = wmc_util.parseEndpointString(layerInfo.endpoint, layerInfo.params)

            req = urllib2.Request(requestURL)
            req.add_header('Cookie', request.headers.get('Cookie', ''))

            filehandle = wmc_util.openURL(req)           
            imageString = StringIO(filehandle.read())
            img = Image.open(imageString)
            layerInfo.cachedImage = img
            if imageCache != None:
                imageCache[layerIndex] = img
        else:
            img = imageCache[layerIndex]

        images.append(img)
        size = img.size
        log.debug("img.size = %s, img.mode = %s" % (img.size, img.mode,))
        layerIndex = layerIndex + 1
    
    background = Image.new('RGBA', size, (255,255,255,255))

    log.debug("creating final image...")
    
    images.reverse()
    images.insert(0, background)
    finalImg = merge(*images)
    
    log.debug("finished buildImage in %s" % (time.time() - st,))
    
    return finalImg
Exemple #3
0
def parseCowsCatalog(cowsEndpoint):
    req = urllib2.Request(cowsEndpoint)
    fh = openURL(req)
    htmlString = fh.read()
    fh.close()
    
    doc = libxml2dom.parseString(htmlString, html=1)
    
    COWSLinks = []
    
    for liElt in doc.getElementsByTagName("li"):
    
        childText = ""
        links = {}
    
        for c in liElt.childNodes:
    
            # get the name by adding together all the text elements and then
            # stripping whitespace and [].
            if c.nodeType == 3:
                childText += c.nodeValue
    
            # build a dictionary of the links with their ascociated text
            elif c.nodeName == 'a' and c.hasAttribute('href'):
                liknName = str(''.join(c.textContent.split())) # removing whitespace and converting from unicode
                
                href = c.getAttribute('href')
                    
                linkTarget =  urlparse.urljoin(cowsEndpoint, href)
                links[liknName] = str(linkTarget)
    
        childText = ''.join(childText.split()) # strip the whitespace
        childText = str(childText.replace('[]','')) # remove any angle brackets
    
        COWSLinks.append((childText, links))
    
    return COWSLinks