Ejemplo n.º 1
0
def local(country):
    pop_elec_in = get_filename(pop_elec_dir, country)
    lv_out = get_filename(local_dir, country)

    try:
        print(f"Local\tstart\t{country}")

        pop_elec_rd = rasterio.open(pop_elec_in)
        pop_elec = pop_elec_rd.read(1)
        affine = pop_elec_rd.transform
        crs = pop_elec_rd.crs

        aoi = admin.loc[admin[code] == country]
        access = aoi[["total", "urban", "rural"]].iloc[0].to_dict()
        peak_kw_pp = 0.1
        people_per_hh = 5
        if access["total"] >= 0.95:
            peak_kw_pp = 2
            people_per_hh = 3

        lengths = ea.apply_lv_length(pop_elec,
                                     peak_kw_pp=peak_kw_pp,
                                     people_per_hh=people_per_hh)
        gf.save_raster(lv_out, lengths, affine, crs)
        total_length = np.sum(lengths)
        msg = f"Local\tDONE\t{country}\tTot length: {total_length} km"
    except Exception as e:
        msg = f"Local\tFAILED\t{country}\t{e}"
        if raise_errors:
            raise
    finally:
        print(msg)
        if log:
            with open(log, "a") as f:
                print(msg, file=f)
Ejemplo n.º 2
0
def pop_elec(country):
    targets_in = get_filename(targets_dir, country)
    pop_elec_out = get_filename(pop_elec_dir, country)
    weight_out = pop_elec_out.parents[0] / (str(pop_elec_out.stem) + "_W.tif")

    try:
        msg = ""
        print(f"\n\nPopElec\tstart\t{country}")
        aoi = admin.loc[admin[code] == country]
        access = aoi[["total", "urban", "rural"]].iloc[0].to_dict()
        if access["total"] == 1:
            return

        pop, urban, ntl, targets, affine, crs = ea.regularise(
            country, aoi, pop_in, urban_in, ntl_ann_in, targets_in)
        pop_elec, access_model_total, weights = ea.estimate(
            pop, urban, ntl, targets, access)
        gf.save_raster(pop_elec_out, pop_elec, affine, crs)
        # gf.save_raster(weight_out, weights, affine, crs)

        msg = f"PopElec\tDONE\t{country}\t\treal: {access['total']:.2f}\tmodel: {access_model_total:.2f}"
    except Exception as e:
        msg = f"PopElec\tFAILED\t{country}\t{e}"
        if raise_errors:
            raise
    finally:
        print(msg)
        if log:
            with open(log, "a") as f:
                print(msg, file=f)
Ejemplo n.º 3
0
def dijk(country):
    this_scratch = scratch / f"dijk_{country}"
    dist_out = this_scratch / "dist.tif"
    targets_in = get_filename(targets_dir, country)
    costs_in = get_filename(costs_dir, country)
    guess_out = get_filename(guess_dir, country)

    try:
        print(f"Dijk\tstart\t{country}")
        this_scratch.mkdir(parents=True, exist_ok=True)

        targets, costs, start, affine = gf.get_targets_costs(
            targets_in, costs_in)
        dist = gf.optimise(targets, costs, start, silent=True)
        gf.save_raster(dist_out, dist, affine)
        guess, affine = gf.threshold(dist_out)
        guess_skel = gf.thin(guess)
        gf.save_raster(guess_out, guess_skel, affine)

        msg = f"Dijk\tDONE\t{country}"
    except Exception as e:
        msg = f"Dijk\tFAILED\t{country}\t{e}"
        if raise_errors:
            raise
    finally:
        shutil.rmtree(this_scratch)
        print(msg)
        if log:
            with open(log, "a") as f:
                print(msg, file=f)
Ejemplo n.º 4
0
def subtract_rast(rast_in, sub_in, subbed_out):
    rast_rd = rasterio.open(rast_in)
    rast = rast_rd.read(1)
    affine = rast_rd.transform
    crs = rast_rd.crs
    sub = rasterio.open(sub_in).read(1)

    subbed = rast - sub
    subbed[subbed <= 0] = 0
    subbed[subbed > 0] = 1

    subbed = subbed.astype(np.int16)
    save_raster(path=subbed_out,
                raster=subbed,
                affine=affine,
                crs=crs,
                nodata=0)
Ejemplo n.º 5
0
def costs(country):
    targets_in = get_filename(targets_dir, country)
    costs_in = get_filename("costs_vec", country, ext="gpkg")
    costs_out = get_filename(costs_dir, country)

    try:
        print(f"Costs\tstart\t{country}")
        aoi = admin.loc[admin[code] == country]
        roads_raster, affine = gf.prepare_roads(costs_in, aoi, targets_in)
        gf.save_raster(costs_out, roads_raster, affine, nodata=-1)

        msg = f"Costs\tDONE\t{country}"
    except Exception as e:
        msg = f"Costs\tFAILED\t{country}\t{e}"
        if raise_errors:
            raise
    finally:
        print(msg)
        if log:
            with open(log, "a") as f:
                print(msg, file=f)
Ejemplo n.º 6
0
def targets(country):
    this_scratch = scratch / f"targets_{country}"
    ntl_out = this_scratch / "ntl"
    ntl_merged_out = this_scratch / "ntl_merged.tif"
    ntl_thresh_out = this_scratch / "ntl_thresh.tif"
    targets_out = get_filename(targets_dir, country)

    try:
        print(f"Targets\tstart\t{country}")
        this_scratch.mkdir(parents=True, exist_ok=True)
        aoi = admin.loc[admin[code] == country]
        buff = aoi.copy()
        buff.geometry = buff.buffer(0.1)

        # Clip NTL rasters and calculate nth percentile values
        gf.clip_rasters(ntl_in, ntl_out, buff)
        raster_merged, affine = gf.merge_rasters(ntl_out,
                                                 percentile=percentile)
        gf.save_raster(ntl_merged_out, raster_merged, affine)

        # Apply filter to NTL
        ntl_filter = gf.create_filter()
        ntl_thresh, affine = gf.prepare_ntl(
            ntl_merged_out,
            buff,
            ntl_filter=ntl_filter,
            upsample_by=1,
            threshold=ntl_threshold,
        )
        gf.save_raster(ntl_thresh_out, ntl_thresh, affine)

        # Clip to actual AOI
        targets, affine, _ = gf.clip_raster(ntl_thresh_out, aoi)
        gf.save_raster(targets_out, targets, affine)
        msg = f"Targets\tDONE\t{country}"

    except Exception as e:
        msg = f"Targets\tFAILED\t{country}\t{e}"
        if raise_errors:
            raise
    finally:
        shutil.rmtree(this_scratch)
        print(msg)
        if log:
            with open(log, "a") as f:
                print(msg, file=f)