Ejemplo n.º 1
0
def raster_type(raster, tabulate, use_label):
    """Check raster map type (int or double) and return categories for int maps

    :param raster: name of the raster map to check
    :type raster: string
    :param tabulate: check categories for tabulation
    :type tabulate: bool
    :param use_label: use label strings instead of category numbers
    :type use_label: bool
    :returns: string with raster map type and list of categories with labels
    :rmap_type: string
    :returns: logical if rastermap contains valid labels
    :rmap_type: bool
    :rcats: list of category tuples
    :Example:

    >>> raster_type('elevation')
    'double precision', False, []
    """

    valid_lab = False
    r_map = RasterRow(raster)
    r_map.open()
    if not r_map.has_cats() and r_map.mtype != "CELL":
        rmap_type = "double precision"
        rcats = []
    else:
        rmap_type = "int"
        rcats = []
        if tabulate:
            rcats = r_map.cats
            r_map.close()
            if not rcats:
                rcats = []
            if len(rcats) == 0:
                rcats = (grass.read_command(
                    "r.category", map=raster).rstrip("\n").split("\n"))
                rcats = [
                    tuple((rcat.split("\t")[1], rcat.split("\t")[0], None))
                    for rcat in rcats
                ]
            cat_list = [rcat[1] for rcat in rcats]
            label_list = [rcat[0] for rcat in rcats]
            if len(set(cat_list)) != len(set(label_list)):
                rcats = [tuple((rcat[1], rcat[1], None)) for rcat in rcats]
            elif use_label:
                valid_lab = True
        else:
            r_map.close()

    return rmap_type, valid_lab, rcats
def raster_type(raster, tabulate, use_lable):
    """Check raster map type (int or double) and return categories for int maps

    :param raster: name of the raster map to check
    :type raster: string
    :param tabulate: check categories for tabulation
    :type tabulate: bool
    :param use_lable: use label strings instead of category numbers
    :type use_lable: bool
    :returns: string with raster map type and list of categories with lables
    :rmap_type: string
    :rcats: list of category tuples
    :Example:

    >>> raster_type('elevation')
    'double precision', []
    """
    r_map = RasterRow(raster)
    r_map.open()
    if not r_map.has_cats() and r_map.mtype != "CELL":
        rmap_type = 'double precision'
        rcats = []
    else:
        rmap_type = 'int'
        rcats = []
        if tabulate:
            rcats = r_map.cats
            r_map.close()
            if not rcats:
                rcats = []
            if len(rcats) == 0:
                rcats = grass.read_command("r.category",
                                           map=raster).rstrip('\n').split('\n')
                rcats = [
                    tuple((rcat.split('\t')[1], rcat.split('\t')[1], None))
                    for rcat in rcats
                ]
            cat_list = [rcat[0] for rcat in rcats]
            lable_list = [rcat[1] for rcat in rcats]
            if use_lable:
                racts = lable_list if len(set(cat_list)) == len(
                    set(lable_list)) else cat_list
            else:
                racts = cat_list
        else:
            r_map.close()

    return rmap_type, rcats