コード例 #1
0
ファイル: provider.py プロジェクト: caomw/grass
def RenderProcess3D(imageWidth, imageHeight, tempDir, cmd, region, bgcolor, fileQueue):
    """Renders image with m.nviz.image and writes the
       resulting ppm filename in the provided file queue

    :param imageWidth: image width
    :param imageHeight: image height
    :param tempDir: directory for rendering
    :param cmd: m.nviz.image command as a list
    :param region: region as a dict
    :param bgcolor: background color as a tuple of 3 values 0 to 255
    :param fileQueue: the inter process communication queue
                      storing the file name of the image
    """

    filename = GetFileFromCmd(tempDir, cmd, None)
    os.environ['GRASS_REGION'] = gcore.region_env(region3d=True, **region)
    Debug.msg(1, "Render image to file " + str(filename))

    cmdTuple = CmdToTuple(cmd)
    cmdTuple[1]['output'] = os.path.splitext(filename)[0]
    # set size
    cmdTuple[1]['size'] = '%d,%d' % (imageWidth, imageHeight)
    # set format
    cmdTuple[1]['format'] = 'ppm'
    cmdTuple[1]['bgcolor'] = bgcolor = ':'.join([str(part) for part in bgcolor])
    returncode, stdout, messages = read2_command(cmdTuple[0], **cmdTuple[1])
    if returncode != 0:
        gcore.warning("Rendering failed:\n" + messages)
        fileQueue.put(None)
        os.environ.pop('GRASS_REGION')
        return

    os.environ.pop('GRASS_REGION')
    fileQueue.put(filename)
コード例 #2
0
def RenderProcess3D(imageWidth, imageHeight, tempDir, cmd, region, bgcolor, fileQueue):
    """Renders image with m.nviz.image and writes the
       resulting ppm filename in the provided file queue

    :param imageWidth: image width
    :param imageHeight: image height
    :param tempDir: directory for rendering
    :param cmd: m.nviz.image command as a list
    :param region: region as a dict
    :param bgcolor: background color as a tuple of 3 values 0 to 255
    :param fileQueue: the inter process communication queue
                      storing the file name of the image
    """

    filename = GetFileFromCmd(tempDir, cmd, None)
    os.environ["GRASS_REGION"] = gcore.region_env(region3d=True, **region)
    Debug.msg(1, "Render image to file " + str(filename))

    cmdTuple = cmdlist_to_tuple(cmd)
    cmdTuple[1]["output"] = os.path.splitext(filename)[0]
    # set size
    cmdTuple[1]["size"] = "%d,%d" % (imageWidth, imageHeight)
    # set format
    cmdTuple[1]["format"] = "ppm"
    cmdTuple[1]["bgcolor"] = bgcolor = ":".join([str(part) for part in bgcolor])
    returncode, stdout, messages = read2_command(cmdTuple[0], **cmdTuple[1])
    if returncode != 0:
        gcore.warning("Rendering failed:\n" + messages)
        fileQueue.put(None)
        os.environ.pop("GRASS_REGION")
        return

    os.environ.pop("GRASS_REGION")
    fileQueue.put(filename)
コード例 #3
0
ファイル: provider.py プロジェクト: starseeker/archival
def RenderProcess2D(imageWidth, imageHeight, tempDir, cmd, region, bgcolor, fileQueue):
    """Render raster or vector files as ppm image and write the
       resulting ppm filename in the provided file queue

    :param imageWidth: image width
    :param imageHeight: image height
    :param tempDir: directory for rendering
    :param cmd: d.rast/d.vect command as a list
    :param region: region as a dict or None
    :param bgcolor: background color as a tuple of 3 values 0 to 255
    :param fileQueue: the inter process communication queue
                      storing the file name of the image
    """

    filename = GetFileFromCmd(tempDir, cmd, region)
    transparency = True

    # Set the environment variables for this process
    _setEnvironment(imageWidth, imageHeight, filename,
                    transparent=transparency, bgcolor=bgcolor)
    if region:
        os.environ['GRASS_REGION'] = gcore.region_env(**region)
    cmdTuple = CmdToTuple(cmd)
    returncode, stdout, messages = read2_command(cmdTuple[0], **cmdTuple[1])
    if returncode != 0:
        gcore.warning("Rendering failed:\n" + messages)
        fileQueue.put(None)
        if region:
            os.environ.pop('GRASS_REGION')
        os.remove(filename)
        return

    if region:
        os.environ.pop('GRASS_REGION')
    fileQueue.put(filename)
コード例 #4
0
def analyse_subregion(params):
    tmp_clump_cat, subregions, cat, clump, threshold = params
    grast.mapcalc(
        "{new} = if ({reg} == {cat}, {clump}, null())".format(
            new=tmp_clump_cat, reg=subregions, cat=cat, clump=clump),
        overwrite=True,
    )
    env = os.environ.copy()
    env["GRASS_REGION"] = gcore.region_env(zoom=tmp_clump_cat)
    try:
        data = gcore.read_command(
            "r.object.geometry",
            input=tmp_clump_cat,
            flags="m",
            separator="comma",
            env=env,
            quiet=True,
        ).strip()
        data = np.loadtxt(StringIO(data),
                          delimiter=",",
                          usecols=(1, 2),
                          skiprows=1)
        # in case there is just one record
        data = data.reshape((-1, 2))
        return data[data[:, 0] > threshold]
    except CalledModuleError:
        gcore.warning(
            "Subregion {cat} has no changes in development, no patches found.".
            format(cat=cat))
        return np.empty([0, 2])
コード例 #5
0
def patch_analysis_per_subregion(development_diff, subregions, threshold, tmp_clump, tmp_clump_cat):
    gcore.run_command('r.clump', input=development_diff, output=tmp_clump, overwrite=True, quiet=True)
    cats = gcore.read_command("r.describe", flags="1", map=subregions, quiet=True).strip().splitlines()
    subregions_data = {}
    env = os.environ.copy()
    for cat in cats:
        grast.mapcalc('{new} = if ({reg} == {cat}, {clump}, null())'.format(new=tmp_clump_cat, reg=subregions,
                                                                            cat=cat, clump=tmp_clump),
                      overwrite=True)
        env['GRASS_REGION'] = gcore.region_env(zoom=tmp_clump_cat)
        data = gcore.read_command('r.object.geometry', input=tmp_clump_cat,
                                  flags='m', separator='comma', env=env, quiet=True).strip()
        data = np.loadtxt(StringIO(data), delimiter=',', usecols=(1, 2), skiprows=1)
        subregions_data[cat] = data[data[:, 0] > threshold]
    return subregions_data
コード例 #6
0
def get_environment(**kwargs):
    """!Returns environment for running modules.
    All modules for which region is important should
    pass this environment into run_command or similar.

    @param tmp_regions a list of temporary regions
    @param kwargs arguments for g.region

    @return environment as a dictionary
    """
    env = os.environ.copy()
    env['GRASS_OVERWRITE'] = '1'
    env['GRASS_VERBOSE'] = '0'
    env['GRASS_MESSAGE_FORMAT'] = 'standard'
    env['GRASS_REGION'] = gcore.region_env(**kwargs)
    return env
コード例 #7
0
def get_environment(**kwargs):
    """!Returns environment for running modules.
    All modules for which region is important should
    pass this environment into run_command or similar.

    @param tmp_regions a list of temporary regions
    @param kwargs arguments for g.region

    @return environment as a dictionary
    """
    env = os.environ.copy()
    env['GRASS_OVERWRITE'] = '1'
    env['GRASS_VERBOSE'] = '0'
    env['GRASS_MESSAGE_FORMAT'] = 'standard'
    env['GRASS_REGION'] = gcore.region_env(**kwargs)
    return env
コード例 #8
0
def patch_analysis_per_subregion(development_diff, subregions, threshold,
                                 tmp_clump, tmp_clump_cat):
    gcore.run_command("r.clump",
                      input=development_diff,
                      output=tmp_clump,
                      overwrite=True,
                      quiet=True)
    cats = (gcore.read_command("r.describe",
                               flags="1n",
                               map=subregions,
                               quiet=True).strip().splitlines())
    subregions_data = {}
    env = os.environ.copy()
    for cat in cats:
        grast.mapcalc(
            "{new} = if ({reg} == {cat}, {clump}, null())".format(
                new=tmp_clump_cat, reg=subregions, cat=cat, clump=tmp_clump),
            overwrite=True,
        )
        env["GRASS_REGION"] = gcore.region_env(zoom=tmp_clump_cat)
        try:
            data = gcore.read_command(
                "r.object.geometry",
                input=tmp_clump_cat,
                flags="m",
                separator="comma",
                env=env,
                quiet=True,
            ).strip()
            data = np.loadtxt(StringIO(data),
                              delimiter=",",
                              usecols=(1, 2),
                              skiprows=1)
            # in case there is just one record
            data = data.reshape((-1, 2))
            subregions_data[cat] = data[data[:, 0] > threshold]
        except CalledModuleError:
            gcore.warning(
                "Subregion {cat} has no changes in development, no patches found."
                .format(cat=cat))
            subregions_data[cat] = np.empty([0, 2])
    return subregions_data
コード例 #9
0
ファイル: controller.py プロジェクト: jiueranzhi/grass
 def _setRegion(self):
     """Set region according input raster map"""
     self._env['GRASS_REGION'] = gcore.region_env(
         raster=self._backupRasterName)