def cleanup():
    if len(TMP_RAST + TMP_VECT):
        gcore.info(_("Cleaning %d temporary maps...") % len(TMP_RAST + TMP_VECT))

    gcore.run_command('g.remove', rast=','.join(TMP_RAST), quiet=True)
    gcore.run_command('g.remove', vect=','.join(TMP_VECT), quiet=True)
    gcore.del_temp_region()
Exemple #2
0
def run_modules_in_temp_region(module_list, q):
    """Run the modules in a temporary region environment

    This function is the argument for multiprocessing.Process class
    in the MultiModule asynchronous execution.

    :param module_list: The list of modules to run in serial
    :param q: The process queue to put the finished process list
    """
    use_temp_region()
    try:
        for proc in module_list:
            proc.run()
            proc.wait()
    finally:
        q.put(module_list)
        del_temp_region()
Exemple #3
0
def run_modules_in_temp_region(module_list, q):
    """Run the modules in a temporary region environment

    This function is the argument for multiprocessing.Process class
    in the MultiModule asynchronous execution.

    :param module_list: The list of modules to run in serial
    :param q: The process queue to put the finished process list
    """
    use_temp_region()
    try:
        for proc in module_list:
            proc.run()
            proc.wait()
    except:
        raise
    finally:
        q.put(module_list)
        del_temp_region()
Exemple #4
0
 def __del__(self):
     """!Destructor deletes temporary region"""
     gcore.del_temp_region()
 def tearDownClass(cls):
     gcore.del_temp_region()
Exemple #6
0
 def __del__(self):
     """!Destructor deletes temporary region"""
     gcore.del_temp_region()
def main():
    size = int(options['size'])
    gamma = scale = None
    if options['gamma']:
        gamma = float(options['gamma'])
    if options['scaling_factor']:
        scale = float(options['scaling_factor'])
    input_dev = options['input']
    output = options['output']
    method = options['method']

    if method in ('gravity', 'kernel') and (gamma is None or scale is None):
        gcore.fatal(
            _("Methods gravity and kernel require options scaling_factor and gamma"
              ))

    temp_map = 'tmp_futures_devPressure_' + str(os.getpid()) + '_copy'
    temp_map_out = 'tmp_futures_devPressure_' + str(os.getpid()) + '_out'
    temp_map_nulls = 'tmp_futures_devPressure_' + str(os.getpid()) + '_nulls'
    global TMP, TMPFILE
    if flags['n']:
        gcore.message(_("Preparing data..."))
        region = gcore.region()
        gcore.use_temp_region()
        gcore.run_command('g.region',
                          n=region['n'] + size * region['nsres'],
                          s=region['s'] - size * region['nsres'],
                          e=region['e'] + size * region['ewres'],
                          w=region['w'] - size * region['ewres'])
        TMP.append(temp_map)
        TMP.append(temp_map_nulls)
        TMP.append(temp_map_out)
        exp = "{temp_map_nulls} = if(isnull({inp}), 1, null())".format(
            temp_map_nulls=temp_map_nulls, inp=input_dev)
        grast.mapcalc(exp=exp)
        grast.mapcalc(exp="{temp} = if(isnull({inp}), 0, {inp})".format(
            temp=temp_map, inp=input_dev))
        rmfilter_inp = temp_map
        rmfilter_out = temp_map_out
    else:
        rmfilter_inp = input_dev
        rmfilter_out = output

    matrix = distance_matrix(size)
    if method == 'occurrence':
        matrix[matrix > 0] = 1
    elif method == 'gravity':
        with np.errstate(divide='ignore'):
            denom = np.power(matrix, gamma)
            matrix = scale / denom
            matrix[denom == 0] = 0
    else:
        matrix_ = scale * np.exp(-2 * matrix / gamma)
        matrix = np.where(matrix > 0, matrix_, 0)

    path = gcore.tempfile()
    global TMPFILE
    TMPFILE = path

    with open(path, 'w') as f:
        f.write(write_filter(matrix))
    gcore.message(_("Running development pressure filter..."))
    gcore.run_command('r.mfilter',
                      input=rmfilter_inp,
                      output=rmfilter_out,
                      filter=path)

    if flags['n']:
        gcore.run_command(
            'g.region',
            n=region['n'],
            s=region['s'],
            e=region['e'],
            w=region['w'],
        )
        grast.mapcalc(
            exp="{out} = if(isnull({temp_null}), {rmfilter_out}, null())".
            format(temp_null=temp_map_nulls,
                   rmfilter_out=rmfilter_out,
                   out=output))
        gcore.del_temp_region()

    grast.raster_history(output)
Exemple #8
0
def main():
    size = int(options["size"])
    gamma = scale = None
    if options["gamma"]:
        gamma = float(options["gamma"])
    if options["scaling_factor"]:
        scale = float(options["scaling_factor"])
    input_dev = options["input"]
    output = options["output"]
    method = options["method"]

    if method in ("gravity", "kernel") and (gamma is None or scale is None):
        gcore.fatal(
            _("Methods gravity and kernel require options scaling_factor and gamma"
              ))

    temp_map = "tmp_futures_devPressure_" + str(os.getpid()) + "_copy"
    temp_map_out = "tmp_futures_devPressure_" + str(os.getpid()) + "_out"
    temp_map_nulls = "tmp_futures_devPressure_" + str(os.getpid()) + "_nulls"
    global TMP, TMPFILE
    if flags["n"]:
        gcore.message(_("Preparing data..."))
        region = gcore.region()
        gcore.use_temp_region()
        gcore.run_command(
            "g.region",
            n=region["n"] + size * region["nsres"],
            s=region["s"] - size * region["nsres"],
            e=region["e"] + size * region["ewres"],
            w=region["w"] - size * region["ewres"],
        )
        TMP.append(temp_map)
        TMP.append(temp_map_nulls)
        TMP.append(temp_map_out)
        exp = "{temp_map_nulls} = if(isnull({inp}), 1, null())".format(
            temp_map_nulls=temp_map_nulls, inp=input_dev)
        grast.mapcalc(exp=exp)
        grast.mapcalc(exp="{temp} = if(isnull({inp}), 0, {inp})".format(
            temp=temp_map, inp=input_dev))
        rmfilter_inp = temp_map
        rmfilter_out = temp_map_out
    else:
        rmfilter_inp = input_dev
        rmfilter_out = output

    matrix = distance_matrix(size)
    if method == "occurrence":
        matrix[matrix > 0] = 1
    elif method == "gravity":
        with np.errstate(divide="ignore"):
            denom = np.power(matrix, gamma)
            matrix = scale / denom
            matrix[denom == 0] = 0
    else:
        matrix_ = scale * np.exp(-2 * matrix / gamma)
        matrix = np.where(matrix > 0, matrix_, 0)

    path = gcore.tempfile()
    global TMPFILE
    TMPFILE = path

    with open(path, "w") as f:
        f.write(write_filter(matrix))
    gcore.message(_("Running development pressure filter..."))
    gcore.run_command("r.mfilter",
                      input=rmfilter_inp,
                      output=rmfilter_out,
                      filter=path)

    if flags["n"]:
        gcore.run_command(
            "g.region",
            n=region["n"],
            s=region["s"],
            e=region["e"],
            w=region["w"],
        )
        grast.mapcalc(
            exp="{out} = if(isnull({temp_null}), {rmfilter_out}, null())".
            format(temp_null=temp_map_nulls,
                   rmfilter_out=rmfilter_out,
                   out=output))
        gcore.del_temp_region()

    grast.raster_history(output)