当前位置: 首页>>代码示例>>Python>>正文


Python QgsRasterLayer.keywords['title']方法代码示例

本文整理汇总了Python中qgis.core.QgsRasterLayer.keywords['title']方法的典型用法代码示例。如果您正苦于以下问题:Python QgsRasterLayer.keywords['title']方法的具体用法?Python QgsRasterLayer.keywords['title']怎么用?Python QgsRasterLayer.keywords['title']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在qgis.core.QgsRasterLayer的用法示例。


在下文中一共展示了QgsRasterLayer.keywords['title']方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: clip_by_extent

# 需要导入模块: from qgis.core import QgsRasterLayer [as 别名]
# 或者: from qgis.core.QgsRasterLayer import keywords['title'] [as 别名]
def clip_by_extent(layer, extent):
    """Clip a raster using a bounding box using processing.

    Issue https://github.com/inasafe/inasafe/issues/3183

    :param layer: The layer to clip.
    :type layer: QgsRasterLayer

    :param extent: The extent.
    :type extent: QgsRectangle

    :return: Clipped layer.
    :rtype: QgsRasterLayer

    .. versionadded:: 4.0
    """
    parameters = dict()
    # noinspection PyBroadException
    try:
        output_layer_name = quick_clip_steps['output_layer_name']
        output_layer_name = output_layer_name % layer.keywords['layer_purpose']

        output_raster = unique_filename(suffix='.tif', dir=temp_dir())

        # We make one pixel size buffer on the extent to cover every pixels.
        # See https://github.com/inasafe/inasafe/issues/3655
        pixel_size_x = layer.rasterUnitsPerPixelX()
        pixel_size_y = layer.rasterUnitsPerPixelY()
        buffer_size = max(pixel_size_x, pixel_size_y)
        extent = extent.buffered(buffer_size)

        if is_raster_y_inverted(layer):
            # The raster is Y inverted. We need to switch Y min and Y max.
            bbox = [
                str(extent.xMinimum()),
                str(extent.xMaximum()),
                str(extent.yMaximum()),
                str(extent.yMinimum())
            ]
        else:
            # The raster is normal.
            bbox = [
                str(extent.xMinimum()),
                str(extent.xMaximum()),
                str(extent.yMinimum()),
                str(extent.yMaximum())
            ]

        # These values are all from the processing algorithm.
        # https://github.com/qgis/QGIS/blob/master/python/plugins/processing/
        # algs/gdal/ClipByExtent.py
        # Please read the file to know these parameters.
        parameters['INPUT'] = layer.source()
        parameters['NO_DATA'] = ''
        parameters['PROJWIN'] = ','.join(bbox)
        parameters['DATA_TYPE'] = 5
        parameters['COMPRESS'] = 4
        parameters['JPEGCOMPRESSION'] = 75
        parameters['ZLEVEL'] = 6
        parameters['PREDICTOR'] = 1
        parameters['TILED'] = False
        parameters['BIGTIFF'] = 0
        parameters['TFW'] = False
        parameters['EXTRA'] = ''
        parameters['OUTPUT'] = output_raster

        initialize_processing()
        feedback = create_processing_feedback()
        context = create_processing_context(feedback=feedback)

        result = processing.run(
            "gdal:cliprasterbyextent",
            parameters,
            context=context)

        if result is None:
            raise ProcessingInstallationError

        clipped = QgsRasterLayer(result['OUTPUT'], output_layer_name)

        # We transfer keywords to the output.
        clipped.keywords = layer.keywords.copy()
        clipped.keywords['title'] = output_layer_name

        check_layer(clipped)

    except Exception as e:
        # This step clip_raster_by_extent was nice to speedup the analysis.
        # As we got an exception because the layer is invalid, we are not going
        # to stop the analysis. We will return the original raster layer.
        # It will take more processing time until we clip the vector layer.
        # Check https://github.com/inasafe/inasafe/issues/4026 why we got some
        # exceptions with this step.
        LOGGER.exception(parameters)
        LOGGER.exception(
            'Error from QGIS clip raster by extent. Please check the QGIS '
            'logs too !')
        LOGGER.info(
            'Even if we got an exception, we are continuing the analysis. The '
            'layer was not clipped.')
#.........这里部分代码省略.........
开发者ID:inasafe,项目名称:inasafe,代码行数:103,代码来源:clip_bounding_box.py

示例2: align_rasters

# 需要导入模块: from qgis.core import QgsRasterLayer [as 别名]
# 或者: from qgis.core.QgsRasterLayer import keywords['title'] [as 别名]
def align_rasters(hazard_layer, exposure_layer, extent):
    """Align hazard and exposure raster layers.

    Align hazard and exposure raster layers so they fit perfectly and so they
    can be used for raster algebra. The method uses QGIS raster alignment tool
    to do the work (which in turn uses GDAL).

    Alignment of layers means that the layers have the same CRS, cell size,
    grid origin and size. That involves clipping and resampling of rasters.
    From the two layers, the layer with finer resolution (smaller cell size)
    will be used as the reference for the alignment (i.e. parameters will
    be set to its CRS, cell size and grid offset).

    - Reproject to the same CRS.
    - Resample to the same cell size and offset in the grid.
    - Clip to a region of interest.

    :param hazard_layer: Hazard layer to be aligned.
    :type hazard_layer: QgsRasterLayer

    :param exposure_layer: Exposure layer to be aligned.
    :type exposure_layer: QgsRasterLayer

    :param extent: Extent in exposure CRS to which raster should be clipped.
    :type extent: QgsRectangle

    :return: Clipped hazard and exposure layers.
    :rtype: QgsRasterLayer, QgsRasterLayer
    """
    output_layer_name = align_steps['output_layer_name']
    processing_step = align_steps['step_name']

    hazard_output = unique_filename(suffix='.tif')
    exposure_output = unique_filename(suffix='.tif')

    # Setup the two raster layers for alignment
    align = QgsAlignRaster()

    inputs = [
        QgsAlignRaster.Item(hazard_layer.source(), hazard_output),
        QgsAlignRaster.Item(exposure_layer.source(), exposure_output)
    ]

    if exposure_layer.keywords.get('exposure_unit') == 'count':
        inputs[1].rescaleValues = True

    align.setRasters(inputs)

    # Find out which layer has finer grid and use it as the reference.
    # This will setup destination CRS, cell size and grid origin
    if exposure_layer.keywords.get('allow_resampling', True):
        index = align.suggestedReferenceLayer()
    else:
        index = 1  # have to use exposure layer as the reference

    if index < 0:
        raise AlignRastersError(tr('Unable to select reference layer'))

    if not align.setParametersFromRaster(
            inputs[index].inputFilename, exposure_layer.crs().toWkt()):
        raise AlignRastersError(align.errorMessage())

    # Setup clip extent
    align.setClipExtent(extent)

    # Everything configured - do the alignment now!
    # For each raster, it will create output file and write resampled values
    if not align.run():
        raise AlignRastersError(align.errorMessage())

    # Load resulting layers
    aligned_hazard_layer = QgsRasterLayer(
        hazard_output, output_layer_name % 'hazard')
    aligned_exposure_layer = QgsRasterLayer(
        exposure_output, output_layer_name % 'exposure')

    aligned_hazard_layer.keywords = dict(hazard_layer.keywords)
    aligned_hazard_layer.keywords['title'] = output_layer_name % 'hazard'
    aligned_exposure_layer.keywords = dict(exposure_layer.keywords)
    aligned_exposure_layer.keywords['title'] = output_layer_name % 'exposure'

    # avoid any possible further rescaling of exposure data by correctly
    # setting original resolution to be the same as current resolution
    aligned_exposure_layer.keywords['resolution'] = (
        align.cellSize().width(), align.cellSize().height())

    check_layer(exposure_layer)
    check_layer(hazard_layer)
    return aligned_hazard_layer, aligned_exposure_layer
开发者ID:ismailsunni,项目名称:inasafe,代码行数:91,代码来源:align.py

示例3: reclassify

# 需要导入模块: from qgis.core import QgsRasterLayer [as 别名]
# 或者: from qgis.core.QgsRasterLayer import keywords['title'] [as 别名]

#.........这里部分代码省略.........
        Defaults to None.
    :type callback: function

    :return: The classified raster layer.
    :rtype: QgsRasterLayer

    .. versionadded:: 4.0
    """
    output_layer_name = reclassify_raster_steps['output_layer_name']
    processing_step = reclassify_raster_steps['step_name']
    output_layer_name = output_layer_name % layer.keywords['layer_purpose']

    if exposure_key:
        classification_key = active_classification(
            layer.keywords, exposure_key)
        thresholds = active_thresholds_value_maps(layer.keywords, exposure_key)
        layer.keywords['thresholds'] = thresholds
        layer.keywords['classification'] = classification_key
    else:
        classification_key = layer.keywords.get('classification')
        thresholds = layer.keywords.get('thresholds')
    if not thresholds:
        raise InvalidKeywordsForProcessingAlgorithm(
            'thresholds are missing from the layer %s'
            % layer.keywords['layer_purpose'])

    if not classification_key:
        raise InvalidKeywordsForProcessingAlgorithm(
            'classification is missing from the layer %s'
            % layer.keywords['layer_purpose'])

    ranges = {}
    value_map = {}
    hazard_classes = definition(classification_key)['classes']
    for hazard_class in hazard_classes:
        ranges[hazard_class['value']] = thresholds[hazard_class['key']]
        value_map[hazard_class['key']] = [hazard_class['value']]

    if overwrite_input:
        output_raster = layer.source()
    else:
        output_raster = unique_filename(suffix='.tiff', dir=temp_dir())

    driver = gdal.GetDriverByName('GTiff')

    raster_file = gdal.Open(layer.source())
    band = raster_file.GetRasterBand(1)
    no_data = band.GetNoDataValue()
    source = band.ReadAsArray()
    destination = source.copy()

    for value, interval in ranges.iteritems():
        v_min = interval[0]
        v_max = interval[1]

        if v_min is None:
            destination[np.where(source <= v_max)] = value

        if v_max is None:
            destination[np.where(source > v_min)] = value

        if v_min < v_max:
            destination[np.where((v_min < source) & (source <= v_max))] = value

    # Tag no data cells
    destination[np.where(source == no_data)] = no_data_value

    # Create the new file.
    output_file = driver.Create(
        output_raster, raster_file.RasterXSize, raster_file.RasterYSize, 1)
    output_file.GetRasterBand(1).WriteArray(destination)
    output_file.GetRasterBand(1).SetNoDataValue(no_data_value)

    # CRS
    output_file.SetProjection(raster_file.GetProjection())
    output_file.SetGeoTransform(raster_file.GetGeoTransform())
    output_file.FlushCache()

    del output_file

    if not isfile(output_raster):
        raise FileNotFoundError

    reclassified = QgsRasterLayer(output_raster, output_layer_name)

    # We transfer keywords to the output.
    reclassified.keywords = layer.keywords.copy()
    reclassified.keywords['layer_mode'] = 'classified'

    value_map = {}

    hazard_classes = definition(classification_key)['classes']
    for hazard_class in reversed(hazard_classes):
        value_map[hazard_class['key']] = [hazard_class['value']]

    reclassified.keywords['value_map'] = value_map
    reclassified.keywords['title'] = output_layer_name

    check_layer(reclassified)
    return reclassified
开发者ID:akbargumbira,项目名称:inasafe,代码行数:104,代码来源:reclassify.py

示例4: rasterize_vector_layer

# 需要导入模块: from qgis.core import QgsRasterLayer [as 别名]
# 或者: from qgis.core.QgsRasterLayer import keywords['title'] [as 别名]
def rasterize_vector_layer(layer, width, height, extent):
    """Rasterize a vector layer to the grid given by extent and width/height.

    :param layer: The vector layer.
    :type layer: QgsVectorLayer

    :param width: The width of the output.
    :type width: int

    :param height: The height of the output.
    :type height: int

    :param extent: The extent to use.
    :type extent: QgsRectangle

    :return: The new raster layer.
    :rtype: QgsRasterLayer
    """
    name = rasterize_steps['gdal_layer_name']
    output_filename = unique_filename(prefix=name, suffix='.tif')

    extent_str = '%f,%f,%f,%f' % (
        extent.xMinimum(),
        extent.xMaximum(),
        extent.yMinimum(),
        extent.yMaximum())

    keywords = dict(layer.keywords)

    # The layer is in memory, we need to save it to a file for Processing.
    data_store = Folder(mkdtemp())
    data_store.default_vector_format = 'geojson'
    result = data_store.add_layer(layer, 'vector_layer')
    layer = data_store.layer(result[1])
    assert layer.isValid()

    field = layer.keywords['inasafe_fields'][aggregation_id_field['key']]

    # ET 21/02/17. I got some issues using rasterize algorithm from Processing.
    # I keep it in case of we need it later. Let's use gdal command line.
    use_gdal_command_line = True

    if use_gdal_command_line:
        startupinfo = None
        if sys.platform == 'win32':
            # On windows, we don't want to display the bash shell.
            # https://github.com/inasafe/inasafe/issues/3980
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        commands = [which('gdal_rasterize')[0]]
        commands += ['-a', field]
        commands += ['-ts', str(width), str(height)]
        commands += ['-ot', 'Int16']
        commands += ['-a_nodata', "'-1'"]
        commands += [layer.source(), output_filename]

        LOGGER.info(' '.join(commands))
        result = subprocess.check_call(commands, startupinfo=startupinfo)
        LOGGER.info('Result : %s' % result)
    else:
        parameters = dict()
        parameters['INPUT'] = layer
        parameters['FIELD'] = field
        parameters['DIMENSIONS'] = 0  # output size is given in pixels
        parameters['WIDTH'] = width
        parameters['HEIGHT'] = height
        parameters['RASTER_EXT'] = extent_str
        parameters['TFW'] = False  # force generation of ESRI TFW
        parameters['RTYPE'] = 1  # raster type: Int16
        parameters['NO_DATA'] = '-1'   # nodata value
        parameters['COMPRESS'] = 4  # GeoTIFF compression: DEFLATE
        parameters['JPEGCOMPRESSION'] = 75  # JPEG compression level: 75
        parameters['ZLEVEL'] = 6  # DEFLATE compression level
        parameters['PREDICTOR'] = 1  # predictor for JPEG/DEFLATE
        parameters['TILED'] = False  # Tiled GeoTIFF?
        parameters['BIGTIFF'] = 0  # whether to make big TIFF
        parameters['EXTRA'] = ''  # additional creation parameters
        parameters['OUTPUT'] = output_filename

        result = runalg('gdalogr:rasterize', parameters)
        if result is None:
            # Let's try be removing a new parameter added between 2.14 and 2.16
            del parameters['RASTER_EXT']

        result = runalg('gdalogr:rasterize', parameters)
        assert result is not None

    layer_aligned = QgsRasterLayer(output_filename, name, 'gdal')
    assert layer_aligned.isValid()

    layer_aligned.keywords = keywords
    layer_aligned.keywords['title'] = (
        rasterize_steps['output_layer_name'] % 'aggregation')
    layer_aligned.keywords['layer_purpose'] = (
        layer_purpose_aggregation_summary['key'])
    del layer_aligned.keywords['inasafe_fields']

    check_layer(layer_aligned)
    return layer_aligned
开发者ID:ismailsunni,项目名称:inasafe,代码行数:101,代码来源:rasterize.py


注:本文中的qgis.core.QgsRasterLayer.keywords['title']方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。