def provision(bbox: BBOX, run_id: str) -> List[str]:
    run_directory = get_run_data_path(run_id, (CACHE_DIR_NAME, ))
    os.makedirs(run_directory)
    driver = ogr.GetDriverByName("GPKG")
    grid_datasource = driver.Open(get_data_path(("grids.gpkg", )))
    grid_layer = grid_datasource.GetLayerByName("BC-20000")
    grid_layer.SetSpatialFilterRect(bbox.min_x, bbox.min_y, bbox.max_x,
                                    bbox.max_y)
    bbox_cells = list()
    while grid_cell := grid_layer.GetNextFeature():
        cell_name = grid_cell.GetFieldAsString("MAP_TILE")
        cell_parent = re.search(r"^\d{2,3}[a-z]", cell_name, re.IGNORECASE)[0]
        bbox_cells.append(
            GenerationRequest(
                url=
                f"https://pub.data.gov.bc.ca/datasets/177864/tif/bcalb/{cell_parent}/{cell_name}.zip",
                path=get_cache_path((CACHE_DIR_NAME, f"{cell_name}.zip")),
                expected_types=["application/zip"],
                cell_name=cell_name,
                tif_name=f"{cell_name}.tif",
                tif_path=get_cache_path((CACHE_DIR_NAME, f"{cell_name}.tif")),
                prj_path=get_cache_path(
                    (CACHE_DIR_NAME, f"{cell_name}_prj.tif")),
                run_path=get_run_data_path(
                    run_id, (CACHE_DIR_NAME, f"{cell_name}.tif")),
            ))
Exemplo n.º 2
0
def provision(bbox: BBOX, run_id: str) -> List[str]:
    run_directory = get_run_data_path(run_id, (CACHE_DIR_NAME, ))
    os.makedirs(run_directory)
    driver = ogr.GetDriverByName("GPKG")
    grid_datasource = driver.Open(get_data_path(("grids.gpkg", )))
    grid_layer = grid_datasource.GetLayerByName("Canada-50000")
    grid_layer.SetSpatialFilterRect(bbox.min_x, bbox.min_y, bbox.max_x,
                                    bbox.max_y)
    bbox_cells = list()
    while grid_cell := grid_layer.GetNextFeature():
        cell_name = grid_cell.GetFieldAsString("NTS_SNRC")
        cell_parent = re.sub(
            "^0", "",
            re.search(r"^\d{2,3}[a-z]", cell_name, re.IGNORECASE)[0])
        for cardinal in ("e", "w"):
            cell_part_name = f"{cell_name.lower()}_{cardinal}"
            zip_file_name = f"{cell_part_name}.dem.zip"
            bbox_cells.append(
                GenerationRequest(
                    url=
                    f"https://pub.data.gov.bc.ca/datasets/175624/{cell_parent.lower()}/{zip_file_name}",
                    path=get_cache_path((CACHE_DIR_NAME, zip_file_name)),
                    expected_type="application/zip",
                    dem_path=get_cache_path(
                        (CACHE_DIR_NAME, f"{cell_part_name}.dem")),
                    prj_path=get_cache_path(
                        (CACHE_DIR_NAME, f"{cell_part_name}_prj.tif")),
                    hs_path=get_cache_path(
                        (CACHE_DIR_NAME, f"{cell_part_name}_hs.tif")),
                    run_path=get_run_data_path(
                        run_id, (CACHE_DIR_NAME, f"{cell_part_name}.tif")),
                ))
Exemplo n.º 3
0
def provision(
    bbox: BBOX,
    run_id: str,
    src_layer_name: str,
    dst_layer_name: str,
) -> List[str]:
    kmz_driver = ogr.GetDriverByName("LIBKML")
    kmz_datasets = [
        kmz_driver.Open(filename) for filename in glob.iglob(
            get_data_path(("avcan-ates-areas-2020-06-23", "**", "*.kmz")),
            recursive=True,
        )
    ]
    run_directory = get_run_data_path(run_id, (CACHE_DIR_NAME, ))
    os.makedirs(run_directory, exist_ok=True)
    path = os.path.join(run_directory, f"{dst_layer_name}.shp")
    ogr_to_shp(
        bbox,
        [ds.GetLayerByName(src_layer_name) for ds in kmz_datasets],
        path,
        dst_layer_name,
        OUTPUT_CRS_CODE,
    )
    kmz_datasets = None
    return [path]
Exemplo n.º 4
0
def provision(bbox: BBOX, run_id: str) -> List[str]:
    run_directory = get_run_data_path(run_id, (CACHE_DIR_NAME, ))
    os.makedirs(run_directory)
    driver = ogr.GetDriverByName("ESRI Shapefile")
    datasource = driver.Open(
        get_data_path(("FTEN_ROAD_SECTION_LINES_SVW", "FTEN_RS_LN_line.shp")))
    result = ogr_to_shp(
        bbox,
        [datasource.GetLayerByIndex(0)],
        os.path.join(run_directory, "bc_resource_roads.shp"),
        "bc_resource_roads",
        OUTPUT_CRS_CODE,
    )
    datasource = None
    return result
Exemplo n.º 5
0
def provision(bbox: BBOX, run_id: str) -> List[str]:
    run_directory = get_run_data_path(run_id, (CACHE_DIR_NAME, ))
    os.makedirs(run_directory)
    driver = ogr.GetDriverByName("OpenFileGDB")
    datasource = driver.Open(
        get_data_path(("FTEN_ROAD_SEGMENT_LINES_SVW.gdb", )))
    path = os.path.join(run_directory, "bc_resource_roads.shp")
    ogr_to_shp(
        bbox,
        [
            datasource.GetLayerByName(
                "WHSE_FOREST_TENURE_FTEN_ROAD_SEGMENT_LINES_SVW")
        ],
        path,
        "bc_resource_roads",
        OUTPUT_CRS_CODE,
    )
    datasource = None
    return [path]
Exemplo n.º 6
0
def provision(bbox: BBOX, run_id: str) -> List[str]:
    run_directory = get_run_data_path(run_id, (CACHE_DIR_NAME, ))
    os.makedirs(run_directory)
    driver = ogr.GetDriverByName("OpenFileGDB")
    datasource = driver.Open(get_data_path(("FTEN_RECREATION_POLY_SVW.gdb", )))
    rec_sites_layer = datasource.GetLayerByName(
        "WHSE_FOREST_TENURE_FTEN_RECREATION_POLY_SVW")
    rec_sites_layer.SetAttributeFilter(
        "LIFE_CYCLE_STATUS_CODE IN ('ACTIVE','PENDING')")
    path = os.path.join(run_directory, "bc_rec_sites.shp")
    ogr_to_shp(
        bbox,
        [rec_sites_layer],
        path,
        "bc_rec_sites",
        OUTPUT_CRS_CODE,
    )
    rec_sites_layer = None
    datasource = None
    return [path]
def provision(bbox: BBOX, run_id: str) -> List[str]:
    run_directory = get_run_data_path(run_id, (CACHE_DIR_NAME, ))
    os.makedirs(run_directory)
    driver = ogr.GetDriverByName("OpenFileGDB")
    datasource = driver.Open(
        get_data_path(("pmbc_parcel_fabric_poly_svw.gdb", )))
    parcels_layer = datasource.GetLayerByName("pmbc_parcel_fabric_poly_svw")
    parcels_layer.SetAttributeFilter(
        "OWNER_TYPE IN ('First Nation','Mixed Ownership','Municipal','Private','Unknown')"
    )
    path = os.path.join(run_directory, "bc_parcels.shp")
    ogr_to_shp(
        bbox,
        [parcels_layer],
        path,
        "bc_parcels",
        OUTPUT_CRS_CODE,
    )
    parcels_layer = None
    datasource = None
    return [path]
        filter(
            lambda generation_request: not skip_file_creation(
                generation_request.prj_path),
            bbox_cells,
        ))
    retrieve(to_generate, HTTP_RETRIEVAL_CONCURRENCY)

    for generation_request in to_generate:
        try:
            with zipfile.ZipFile(generation_request.path, "r") as zip_ref:
                zip_ref.extract(generation_request.tif_name,
                                get_cache_path((CACHE_DIR_NAME, )))
            Warp(
                generation_request.prj_path,
                generation_request.tif_path,
                cutlineDSName=get_data_path(("grids.gpkg", )),
                cutlineLayer="BC-20000",
                cutlineWhere=f"MAP_TILE = '{generation_request.cell_name}'",
                cropToCutline=False,
                cutlineBlend=1,
                dstNodata=-1,
                dstSRS=OUTPUT_CRS_CODE,
                resampleAlg="lanczos",
            )
            if remove_intermediaries():
                os.remove(generation_request.path)
                os.remove(generation_request.tif_path)
        except Exception as ex:
            logging.error(ex)

    for generation_request in bbox_cells: