Exemple #1
0
def add_raster_info_from_bufferArea(polygons_shp, raster_file, raster_name,
                                    b_buffer_size):
    """
    calculate the raster information such elevation, then add toeach polygon
    Args:
        polygons_shp: input shapfe file
        raster_file:  raster file, should have the same projection of shapefile
        raster_name: the name of raster, should less than four letters, will be used as part of the attribute name
        b_buffer_size: the size of buffer area in meters

    Returns: True if successful, False Otherwise
    """
    if io_function.is_file_exist(polygons_shp) is False:
        return False
    if io_function.is_file_exist(raster_file) is False:
        return False
    operation_obj = shape_opeation()

    ## calculate the topography information from the buffer area
    basic.outputlogMessage(
        "info: calculate the raster information from the buffer area")
    buffer_polygon_shp = io_function.get_name_by_adding_tail(
        polygons_shp, 'buffer')
    # if os.path.isfile(buffer_polygon_shp) is False:
    if vector_features.get_buffer_polygons(polygons_shp, buffer_polygon_shp,
                                           b_buffer_size) is False:
        raise IOError("error, failed in producing the buffer_polygon_shp")

    # replace the polygon shape file
    polygons_shp_backup = polygons_shp
    polygons_shp = buffer_polygon_shp

    # all_touched: bool, optional
    #     Whether to include every raster cell touched by a geometry, or only
    #     those having a center point within the polygon.
    #     defaults to `False`
    #   Since the dem usually is coarser, so we set all_touched = True
    all_touched = True

    stats_list = ['min', 'max', 'mean',
                  'std']  #['min', 'max', 'mean', 'count','median','std']
    if operation_obj.add_fields_from_raster(polygons_shp,
                                            raster_file,
                                            raster_name,
                                            band=1,
                                            stats_list=stats_list,
                                            all_touched=all_touched) is False:
        return False

    # copy the information to the original shape file
    operation_obj.add_fields_shape(polygons_shp_backup, buffer_polygon_shp,
                                   polygons_shp_backup)

    return True
Exemple #2
0
def calculate_polygon_topography(polygons_shp,
                                 para_file,
                                 dem_files,
                                 slope_files,
                                 aspect_files=None,
                                 dem_diffs=None):
    """
    calculate the topography information such elevation and slope of each polygon
    Args:
        polygons_shp: input shapfe file
        dem_files: DEM raster file or tiles, should have the same projection of shapefile
        slope_files: slope raster file or tiles  (can be drived from dem file by using QGIS or ArcGIS)
        aspect_files: aspect raster file or tiles (can be drived from dem file by using QGIS or ArcGIS)

    Returns: True if successful, False Otherwise
    """
    if io_function.is_file_exist(polygons_shp) is False:
        return False
    operation_obj = shape_opeation()

    ## calculate the topography information from the buffer area

    # the para file was set in parameters.set_saved_parafile_path(options.para_file)
    b_use_buffer_area = parameters.get_bool_parameters(
        para_file, 'b_topo_use_buffer_area')

    if b_use_buffer_area is True:

        b_buffer_size = 5  # meters (the same as the shape file)

        basic.outputlogMessage(
            "info: calculate the topography information from the buffer area")
        buffer_polygon_shp = io_function.get_name_by_adding_tail(
            polygons_shp, 'buffer')
        # if os.path.isfile(buffer_polygon_shp) is False:
        if vector_features.get_buffer_polygons(
                polygons_shp, buffer_polygon_shp, b_buffer_size) is False:
            basic.outputlogMessage(
                "error, failed in producing the buffer_polygon_shp")
            return False
        # else:
        #     basic.outputlogMessage("warning, buffer_polygon_shp already exist, skip producing it")
        # replace the polygon shape file
        polygons_shp_backup = polygons_shp
        polygons_shp = buffer_polygon_shp
    else:
        basic.outputlogMessage(
            "info: calculate the topography information from the inside of each polygon"
        )

    # all_touched: bool, optional
    #     Whether to include every raster cell touched by a geometry, or only
    #     those having a center point within the polygon.
    #     defaults to `False`
    #   Since the dem usually is coarser, so we set all_touched = True
    all_touched = True
    process_num = 4

    # #DEM
    if dem_files is not None:
        stats_list = ['min', 'max', 'mean', 'median',
                      'std']  #['min', 'max', 'mean', 'count','median','std']
        # if operation_obj.add_fields_from_raster(polygons_shp, dem_file, "dem", band=1,stats_list=stats_list,all_touched=all_touched) is False:
        #     return False
        if zonal_stats_multiRasters(polygons_shp,
                                    dem_files,
                                    stats=stats_list,
                                    prefix='dem',
                                    band=1,
                                    all_touched=all_touched,
                                    process_num=process_num) is False:
            return False
    else:
        basic.outputlogMessage(
            "warning, DEM file not exist, skip the calculation of DEM information"
        )

    # #slope
    if slope_files is not None:
        stats_list = ['min', 'max', 'mean', 'median', 'std']
        if zonal_stats_multiRasters(polygons_shp,
                                    slope_files,
                                    stats=stats_list,
                                    prefix='slo',
                                    band=1,
                                    all_touched=all_touched,
                                    process_num=process_num) is False:
            return False
    else:
        basic.outputlogMessage(
            "warning, slope file not exist, skip the calculation of slope information"
        )

    # #aspect
    if aspect_files is not None:
        stats_list = ['min', 'max', 'mean', 'std']
        if zonal_stats_multiRasters(polygons_shp,
                                    aspect_files,
                                    stats=stats_list,
                                    prefix='asp',
                                    band=1,
                                    all_touched=all_touched,
                                    process_num=process_num) is False:
            return False
    else:
        basic.outputlogMessage(
            'warning, aspect file not exist, ignore adding aspect information')

    # elevation difference
    if dem_diffs is not None:
        stats_list = ['min', 'max', 'mean', 'median', 'std', 'area']
        # only count the pixel within this range when do statistics
        dem_diff_range_str = parameters.get_string_list_parameters(
            para_file, 'dem_difference_range')
        range = [
            None if item.upper() == 'NONE' else float(item)
            for item in dem_diff_range_str
        ]

        # expand the polygon when doing dem difference statistics
        buffer_size_dem_diff = parameters.get_digit_parameters(
            para_file, 'buffer_size_dem_diff', 'float')

        if zonal_stats_multiRasters(polygons_shp,
                                    dem_diffs,
                                    stats=stats_list,
                                    prefix='demD',
                                    band=1,
                                    all_touched=all_touched,
                                    process_num=process_num,
                                    range=range,
                                    buffer=buffer_size_dem_diff) is False:
            return False
    else:
        basic.outputlogMessage(
            'warning, dem difference file not exist, ignore adding dem diff information'
        )

    # # hillshape

    # copy the topography information
    if b_use_buffer_area is True:
        operation_obj.add_fields_shape(polygons_shp_backup, buffer_polygon_shp,
                                       polygons_shp_backup)

    return True
def calculate_gully_topography(polygons_shp,
                               dem_file,
                               slope_file,
                               aspect_file=None):
    """
    calculate the topography information such elevation and slope of each polygon
    Args:
        polygons_shp: input shapfe file
        dem_file: DEM raster file, should have the same projection of shapefile
        slope_file: slope raster file (can be drived from dem file by using QGIS or ArcGIS)
        aspect_file: aspect raster file (can be drived from dem file by using QGIS or ArcGIS)

    Returns: True if successful, False Otherwise
    """
    if io_function.is_file_exist(polygons_shp) is False:
        return False
    operation_obj = shape_opeation()

    ## calculate the topography information from the buffer area
    b_use_buffer_area = True
    b_buffer_size = 5  # meters (the same as the shape file)
    if b_use_buffer_area is True:
        basic.outputlogMessage(
            "info: calculate the topography information from the buffer area")
        buffer_polygon_shp = io_function.get_name_by_adding_tail(
            polygons_shp, 'buffer')
        # if os.path.isfile(buffer_polygon_shp) is False:
        if vector_features.get_buffer_polygons(
                polygons_shp, buffer_polygon_shp, b_buffer_size) is False:
            basic.outputlogMessage(
                "error, failed in producing the buffer_polygon_shp")
            return False
        # else:
        #     basic.outputlogMessage("warning, buffer_polygon_shp already exist, skip producing it")
        # replace the polygon shape file
        polygons_shp_backup = polygons_shp
        polygons_shp = buffer_polygon_shp
    else:
        basic.outputlogMessage(
            "info: calculate the topography information from the inside of each polygon"
        )

    # all_touched: bool, optional
    #     Whether to include every raster cell touched by a geometry, or only
    #     those having a center point within the polygon.
    #     defaults to `False`
    #   Since the dem usually is coarser, so we set all_touched = True
    all_touched = True

    # #DEM
    if io_function.is_file_exist(dem_file):
        stats_list = ['min', 'max', 'mean',
                      'std']  #['min', 'max', 'mean', 'count','median','std']
        if operation_obj.add_fields_from_raster(
                polygons_shp,
                dem_file,
                "dem",
                band=1,
                stats_list=stats_list,
                all_touched=all_touched) is False:
            return False
    else:
        basic.outputlogMessage(
            "warning, DEM file not exist, skip the calculation of DEM information"
        )

    # #slope
    if io_function.is_file_exist(slope_file):
        stats_list = ['min', 'max', 'mean', 'std']
        if operation_obj.add_fields_from_raster(
                polygons_shp,
                slope_file,
                "slo",
                band=1,
                stats_list=stats_list,
                all_touched=all_touched) is False:
            return False
    else:
        basic.outputlogMessage(
            "warning, slope file not exist, skip the calculation of slope information"
        )

    # #aspect
    if aspect_file is not None and os.path.isfile(aspect_file):
        if io_function.is_file_exist(aspect_file) is False:
            return False
        stats_list = ['min', 'max', 'mean', 'std']
        if operation_obj.add_fields_from_raster(
                polygons_shp,
                aspect_file,
                "asp",
                band=1,
                stats_list=stats_list,
                all_touched=all_touched) is False:
            return False
    else:
        basic.outputlogMessage(
            'warning, aspect file not exist, ignore adding aspect information')

    # # hillshape

    # copy the topography information
    if b_use_buffer_area is True:
        operation_obj.add_fields_shape(polygons_shp_backup, buffer_polygon_shp,
                                       polygons_shp_backup)

    return True