Exemplo n.º 1
0
def start_main(work_path, dem_tif, lakes_shp, river_th):
    start = time.perf_counter()

    # 过程数据存储路径
    process_path = work_path + '/process'
    if not os.path.exists(process_path):
        os.makedirs(process_path)

    # 结果数据存储路径
    result_path = work_path + '/result'
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    print(
        "-------------------------------------DEM Pit Remove---------------------------------"
    )
    stage_time = time.perf_counter()
    dem_filled_tif = process_path + "/dem_filled.tif"
    # DEM填洼
    tu.pit_remove(dem_tif, dem_filled_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "----------------------------Get Direction and Accumulation--------------------------"
    )
    stage_time = time.perf_counter()
    dir_tif = process_path + "/dir.tif"
    acc_tif = process_path + "/acc.tif"
    # 计算流向、汇流累积量
    gda.get_dir_acc(process_path, dem_tif_path, dir_tif, acc_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "-----------------------------------Get Lakes Raster----------------------------------"
    )
    stage_time = time.perf_counter()
    # 将湖泊/水库栅格化同基本数据一致标准
    lakes_tif = process_path + '/lakes_99.tif'
    vr.lake_rasterize(lakes_shp, dir_tif, lakes_tif, -99, -9, 1)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "-------------------------------------Get Rivers-------------------------------------"
    )
    stage_time = time.perf_counter()
    # 提取河网
    re.get_river(process_path, acc_tif, river_th)
    river_tif = process_path + "/stream.tif"
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "------------------------------------Record Rivers------------------------------------"
    )
    stage_time = time.perf_counter()
    # 记录河系信息
    rr.record_rivers(process_path, river_tif, acc_tif)
    river_record = process_path + "/river_record.txt"
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "----------------------------------Get Revised Water----------------------------------"
    )
    stage_time = time.perf_counter()
    water_revised_path = process_path + "/lake_revised.tif"
    cu.copy_tif_data(lakes_tif, water_revised_path)
    # 修正湖泊/水库边界
    wr.water_revise(water_revised_path, river_tif, river_record, dir_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "----------------------------------Get Slope Surface----------------------------------"
    )
    stage_time = time.perf_counter()
    # 提取坡面和湖泊/水库
    sse.get_slope_surface(process_path, water_revised_path, dir_tif, acc_tif,
                          river_th, -9)
    water_s_s_tif_path = process_path + "/water_slope.tif"
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "------------------------------------Get Watershed-----------------------------------"
    )
    stage_time = time.perf_counter()
    # 提取子流域
    we.watershed_extract(process_path, dem_filled_tif, dir_tif, acc_tif,
                         river_tif, water_s_s_tif_path)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "-----------------------------------Copy Result Data----------------------------------"
    )
    # 复制修正后的湖泊/水库、坡面流路、子流域以及河网矢量结果数据到文件夹
    print("-> Copy Water/Slope surface route/Stream...")
    stage_time = time.perf_counter()
    file_list = os.listdir(process_path)
    result_files = ["water_revised", "slope_surface_route", "watershed"]
    for file in file_list:
        file_info = file.split(".")
        if file_info[0] in result_files:
            shutil.copy(process_path + "/" + file, result_path + "/" + file)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制河网栅格结果数据和重分类
    print("-> Copy/Reclassify Stream...")
    stage_time = time.perf_counter()
    river_ds = gdal.Open(river_tif)
    no_data_value = river_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(river_tif, result_path + "/stream.tif", [[0]],
                      [int(no_data_value)])
    river_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制坡面结果数据和重分类
    print("-> Copy/Reclassify Slope surface...")
    stage_time = time.perf_counter()
    w_w_surface_ds = gdal.Open(water_s_s_tif_path)
    no_data_value = w_w_surface_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(water_s_s_tif_path, result_path + "/slope.tif", [[-99]],
                      [int(no_data_value)])
    w_w_surface_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "----------------------------------------Over----------------------------------------"
    )
    end = time.perf_counter()
    print('Total time: ', end - start, 's')
Exemplo n.º 2
0
def demo3(workspace_path, dem_tif_path, dir_tif_path, acc_tif_path,
          water_tif_path, river_threshold):

    start = time.perf_counter()

    # 工作空间路径
    process_path = workspace_path + "/process"
    if not os.path.exists(process_path):
        os.makedirs(process_path)

    # 流向数据重分类
    print(
        "--------------------------------Reclassify Direction--------------------------------"
    )
    stage_time = time.perf_counter()
    dir_reclass_tif = process_path + "/dir_reclass.tif"
    dc.dir_reclassify(dir_tif_path, dir_reclass_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 提取河系
    print(
        "-------------------------------------Get Rivers-------------------------------------"
    )
    stage_time = time.perf_counter()
    re.get_river(process_path, acc_tif_path, river_threshold)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 记录河系信息
    print(
        "------------------------------------Record Rivers------------------------------------"
    )
    stage_time = time.perf_counter()
    river_tif_path = process_path + "/stream.tif"
    rr.record_rivers(process_path, river_tif_path, acc_tif_path)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 修正湖泊/水库边界
    print(
        "----------------------------------Get Revised Water----------------------------------"
    )
    stage_time = time.perf_counter()
    water_revised_path = process_path + "/lake_revised.tif"
    cu.copy_tif_data(water_tif_path, water_revised_path)
    wr.water_revise(water_revised_path, river_tif_path,
                    process_path + "/river_record.txt", dir_reclass_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 提取坡面和湖泊/水库
    print(
        "----------------------------------Get Slope Surface----------------------------------"
    )
    stage_time = time.perf_counter()
    sse.get_slope_surface(process_path, water_revised_path, dir_reclass_tif,
                          acc_tif_path, river_threshold, -9)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 提取子流域
    print(
        "------------------------------------Get Watershed-----------------------------------"
    )
    stage_time = time.perf_counter()
    water_s_s_tif_path = process_path + "/water_slope.tif"
    we.watershed_extract(process_path, dem_tif_path, dir_reclass_tif,
                         acc_tif_path, river_tif_path, water_s_s_tif_path)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 结果文件夹
    print(
        "-----------------------------------Copy Result Data----------------------------------"
    )
    result_path = workspace_path + "/result"
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    # 复制修正后的湖泊/水库、坡面流路、子流域以及河网矢量结果数据到文件夹
    print("-> Copy Water/Slope surface route/Stream...")
    stage_time = time.perf_counter()
    file_list = os.listdir(process_path)
    result_files = [
        "water_revised", "slope_surface_route", "watershed", "stream_shp"
    ]
    for file in file_list:
        file_info = file.split(".")
        if file_info[0] in result_files:
            shutil.copy(process_path + "/" + file, result_path + "/" + file)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制河网栅格结果数据和重分类
    print("-> Copy/Reclassify Stream...")
    stage_time = time.perf_counter()
    river_ds = gdal.Open(river_tif_path)
    no_data_value = river_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(river_tif_path, result_path + "/stream.tif", [[0]],
                      [int(no_data_value)])
    river_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制坡面结果数据和重分类
    print("-> Copy/Reclassify Slope surface...")
    stage_time = time.perf_counter()
    w_w_surface_ds = gdal.Open(water_s_s_tif_path)
    no_data_value = w_w_surface_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(water_s_s_tif_path, result_path + "/slope.tif", [[-99]],
                      [int(no_data_value)])
    w_w_surface_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print("------Over------")
    end = time.perf_counter()
    print('Total time: ', end - start, 's')
Exemplo n.º 3
0
def demo6(workspace_path, catalog_path, geojson_path, river_threshold):

    start = time.perf_counter()

    # 工作空间路径
    data_path = workspace_path + "/data"
    if not os.path.exists(data_path):
        os.makedirs(data_path)

    # 查询计算区域的数据
    print(
        "----------------------------------Search Raster Data----------------------------------"
    )
    stage_time = time.perf_counter()
    # DEM数据路径
    dem_tif_path = data_path + "/dem.tif"
    # 流向数据路径
    dir_tif_path = data_path + "/dir.tif"
    # 汇流累积量数据路径
    acc_tif_path = data_path + "/acc.tif"
    # 湖泊/水库数据路径
    water_tif_path = data_path + "/lakes.tif"
    print("Output Data.")
    ds.data_search(catalog_path, geojson_path, dem_tif_path, dir_tif_path,
                   acc_tif_path, water_tif_path)

    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 工作空间路径
    process_path = workspace_path + "/process"
    if not os.path.exists(process_path):
        os.makedirs(process_path)

    # 裁剪研究区域的数据
    print(
        "----------------------------------Crop Raster Data----------------------------------"
    )
    stage_time = time.perf_counter()
    dem_clip = process_path + "/dem_clip.tif"
    dir_clip = process_path + "/dir_clip.tif"
    acc_clip = process_path + "/acc_clip.tif"
    lakes_clip = process_path + "/lakes_clip.tif"
    ct.geojson_clip_tif(geojson_path, dem_tif_path, dem_clip)
    ct.geojson_clip_tif(geojson_path, dir_tif_path, dir_clip)
    ct.geojson_clip_tif(geojson_path, acc_tif_path, acc_clip)
    ct.geojson_clip_tif(geojson_path, water_tif_path, lakes_clip)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 重新计算流向数据和汇流累积量
    print(
        "--------------------------------TauDEM Direction and Accumulation--------------------------------"
    )
    stage_time = time.perf_counter()
    dir_tau_tif = process_path + "/dir_tau.tif"
    acc_tau_tif = process_path + "/acc_tau.tif"
    gda.get_dir_acc(process_path, dem_clip, dir_tau_tif, acc_tau_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 提取河系
    print(
        "-------------------------------------Get Rivers-------------------------------------"
    )
    stage_time = time.perf_counter()
    re.get_river(process_path, acc_tau_tif, river_threshold)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 记录河系信息
    print(
        "------------------------------------Record Rivers------------------------------------"
    )
    stage_time = time.perf_counter()
    river_tif_path = process_path + "/stream.tif"
    rr.record_rivers(process_path, river_tif_path, acc_tau_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 修正湖泊/水库边界
    print(
        "----------------------------------Get Revised Water----------------------------------"
    )
    stage_time = time.perf_counter()
    water_revised_path = process_path + "/lake_revised.tif"
    cu.copy_tif_data(lakes_clip, water_revised_path)
    wr.water_revise(water_revised_path, river_tif_path,
                    process_path + "/river_record.txt", dir_tau_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 提取坡面和湖泊/水库
    print(
        "----------------------------------Get Slope Surface----------------------------------"
    )
    stage_time = time.perf_counter()
    sse.get_slope_surface(process_path, water_revised_path, dir_tau_tif,
                          acc_tau_tif, river_threshold, -9)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 提取子流域
    print(
        "------------------------------------Get Watershed-----------------------------------"
    )
    stage_time = time.perf_counter()
    water_s_s_tif_path = process_path + "/water_slope.tif"
    we.watershed_extract(process_path, dem_clip, dir_tau_tif, acc_tau_tif,
                         river_tif_path, water_s_s_tif_path)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 结果文件夹
    print(
        "-----------------------------------Copy Result Data----------------------------------"
    )
    result_path = workspace_path + "/result"
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    # 复制修正后的湖泊/水库、坡面流路、子流域以及河网矢量结果数据到文件夹
    print("-> Copy Water/Slope surface route/Stream...")
    stage_time = time.perf_counter()
    file_list = os.listdir(process_path)
    result_files = [
        "water_revised", "slope_surface_route", "watershed", "stream_shp"
    ]
    for file in file_list:
        file_info = file.split(".")
        if file_info[0] in result_files:
            shutil.copy(process_path + "/" + file, result_path + "/" + file)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制河网栅格结果数据和重分类
    print("-> Copy/Reclassify Stream...")
    stage_time = time.perf_counter()
    river_ds = gdal.Open(river_tif_path)
    no_data_value = river_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(river_tif_path, result_path + "/stream.tif", [[0]],
                      [int(no_data_value)])
    river_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制坡面结果数据和重分类
    print("-> Copy/Reclassify Slope surface...")
    stage_time = time.perf_counter()
    w_w_surface_ds = gdal.Open(water_s_s_tif_path)
    no_data_value = w_w_surface_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(water_s_s_tif_path, result_path + "/slope.tif", [[-99]],
                      [int(no_data_value)])
    w_w_surface_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "---------------------------------Delete Temporary Files-----------------------------"
    )
    stage_time = time.perf_counter()
    print("Delete Folders...")
    shutil.rmtree(process_path)
    shutil.rmtree(data_path)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')
    print(
        "----------------------------------------Over----------------------------------------"
    )
    end = time.perf_counter()
    print('Total time: ', end - start, 's')
Exemplo n.º 4
0
def start_main(work_path, geojson_path, lakes_area, river_th):
    start = time.perf_counter()

    # 基础数据存储路径
    data_path = work_path + '/data'
    if not os.path.exists(data_path):
        os.makedirs(data_path)

    # 过程数据存储路径
    process_path = work_path + '/process'
    if not os.path.exists(process_path):
        os.makedirs(process_path)

    # 结果数据存储路径
    result_path = work_path + '/result'
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    print(
        "----------------------------------Search Basic Data----------------------------------"
    )
    stage_time = time.perf_counter()
    # 从RDD中获取兴趣范围内的基本数据
    gdfr.get_basic_data(data_path, catalog_path, geojson_path)
    # 得到的基本数据所在路径
    dem_tif = data_path + '/dem.tif'
    acc_tif = data_path + '/acc.tif'
    dir_o_tif = data_path + '/dir_o.tif'
    dir_tif = data_path + '/dir.tif'
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "-----------------------------------Get Lakes Data----------------------------------"
    )
    stage_time = time.perf_counter()
    # 兴趣范围转为shp格式
    # extent_data = process_path + '/extent.shp'
    # cu.geojson_to_shp(geojson_path, extent_data)
    # # 提取兴趣范围内的湖泊/水库范围
    # lakes_shp = data_path + '/lakes_shp.shp'
    # fl.filter_lakes_extent_area(o_lake_data, extent_data, lakes_shp, lakes_area)
    lakes_shp = data_path + '/lakes_shp.shp'
    fls.clip_shp(geojson_path, o_lake_data, lakes_shp)
    # 将湖泊/水库栅格化同基本数据一致标准
    lakes_tif = process_path + '/lakes_99.tif'
    vr.lake_rasterize(lakes_shp, dir_tif, lakes_tif, -99, -9, 1)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "-------------------------------------Get Rivers-------------------------------------"
    )
    stage_time = time.perf_counter()
    # 提取河网
    re.get_river(process_path, acc_tif, river_th)
    river_tif = process_path + "/stream.tif"
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "-------------------------------Add Finals to Rivers---------------------------------"
    )
    stage_time = time.perf_counter()
    # 记录区域内的内流终点
    trace_starts = process_path + "/trace_starts.tif"
    final_record = process_path + '/finals.txt'
    seaside_record = process_path + "/seaside_record.txt"
    lo.get_trace_points(dir_tif,
                        dir_o_tif,
                        trace_starts,
                        seaside_txt=seaside_record,
                        final_txt=final_record)
    raf.add_final_to_river(dir_tif, final_record, river_tif, acc_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "------------------------------------Record Rivers------------------------------------"
    )
    stage_time = time.perf_counter()
    # 记录河系信息
    rr.record_rivers(process_path, river_tif, acc_tif)
    river_record = process_path + "/river_record.txt"
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "----------------------------------Get Revised Water----------------------------------"
    )
    stage_time = time.perf_counter()
    water_revised_path = process_path + "/lake_revised.tif"
    cu.copy_tif_data(lakes_tif, water_revised_path)
    # 修正湖泊/水库边界
    wr.water_revise(water_revised_path, river_tif, river_record, dir_tif)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "----------------------------------Get Slope Surface----------------------------------"
    )
    stage_time = time.perf_counter()
    # 提取坡面和湖泊/水库
    sse.get_slope_surface(process_path, water_revised_path, dir_tif, acc_tif,
                          river_th, -9)
    water_s_s_tif_path = process_path + "/water_slope.tif"
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "------------------------------------Get Watershed-----------------------------------"
    )
    stage_time = time.perf_counter()
    # 提取子流域
    we.watershed_extract(process_path, dem_tif, dir_tif, acc_tif, river_tif,
                         water_s_s_tif_path)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "-----------------------------------Copy Result Data----------------------------------"
    )
    # 复制修正后的湖泊/水库、坡面流路、子流域以及河网矢量结果数据到文件夹
    print("-> Copy Water/Slope surface route/Stream...")
    stage_time = time.perf_counter()
    file_list = os.listdir(process_path)
    result_files = ["water_revised", "slope_surface_route", "watershed"]
    for file in file_list:
        file_info = file.split(".")
        if file_info[0] in result_files:
            shutil.copy(process_path + "/" + file, result_path + "/" + file)
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制河网栅格结果数据和重分类
    print("-> Copy/Reclassify Stream...")
    stage_time = time.perf_counter()
    river_ds = gdal.Open(river_tif)
    no_data_value = river_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(river_tif, result_path + "/stream.tif", [[0]],
                      [int(no_data_value)])
    river_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    # 复制坡面结果数据和重分类
    print("-> Copy/Reclassify Slope surface...")
    stage_time = time.perf_counter()
    w_w_surface_ds = gdal.Open(water_s_s_tif_path)
    no_data_value = w_w_surface_ds.GetRasterBand(1).GetNoDataValue()
    cu.tif_reclassify(water_s_s_tif_path, result_path + "/slope.tif", [[-99]],
                      [int(no_data_value)])
    w_w_surface_ds = None
    over_time = time.perf_counter()
    print("Run time: ", over_time - stage_time, 's')

    print(
        "----------------------------------------Over----------------------------------------"
    )
    end = time.perf_counter()
    print('Total time: ', end - start, 's')