Ejemplo n.º 1
0
    def create_raster_sld(self, filename, quantiles=False, verbose=False):
        """given a raster file and a predefined SLD template it should find the min,max,nodata values
        for the dataset and create a custom style with a color map using the predefined template sld
        which is located in module sld_template.py
        
        If quantiles is True, 10 quantiles will be used for colour coding. Othewise 10 equidistant intervals will be used.
        """

        pathname, extension = os.path.splitext(filename)
        layername = os.path.basename(pathname)
        
        R = raster.read_coverage(filename)
        levels = R.get_bins(N=10, quantiles=quantiles)
        nodata = R.get_nodata_value()         


        #if verbose:
        #    print 'Styling %s' %layername        
        #    print 'Levels', levels
        #    print 'NoData', nodata    
        
        # Write the SLD file    
        sld = layername+'.sld'
        text = sld_template.sld_template

        text = text.replace('MIN',str(levels[0]))
        text = text.replace('MAX',str(levels[-1]))
        text = text.replace('TEN',str(levels[1]))
        text = text.replace('TWENTY',str(levels[2]))
        text = text.replace('THIRTY',str(levels[3]))
        text = text.replace('FOURTY',str(levels[4]))
        text = text.replace('FIFTY',str(levels[5]))
        text = text.replace('SIXTY',str(levels[6]))
        text = text.replace('SEVENTY',str(levels[7]))
        text = text.replace('EIGHTY',str(levels[8]))
        text = text.replace('NINETY',str(levels[9]))


        # Getting round an sld parsing bug in geoserver
        if nodata >= max:
            text = text.replace('<!--Higher-->', '<ColorMapEntry color="#ffffff" quantity="NODATA" opacity="0"/>')
        else:   
            text = text.replace('<!--Lower-->', '<ColorMapEntry color="#ffffff" quantity="NODATA" opacity="0"/>')
            
        text = text.replace('NODATA', str(nodata))
        fout = open(sld, 'w')
        fout.write(text)
        fout.close()
Ejemplo n.º 2
0
    def get_raster_data(self, 
                        coverage_name,
                        bounding_box=None, 
                        workspace=None, 
                        verbose=False):

        """Retrieve named coverage layer as Python numpy struture
        """

        # Download coverage into ASCII file
        ascii_filename = self.download_coverage(coverage_name, 
                                                bounding_box=bounding_box, 
                                                workspace=workspace, 
                                                format='GeoTIFF',
                                                verbose=verbose)

        # Read resulting ASCII file into internal numerical structure and return
        return raster.read_coverage(ascii_filename)