Example #1
0
def main(argc, argv):
    country = 'NGA'
    # in_files = get_dataset_paths(country)
    in_files = {
        'humdata':
        os.path.join(get_project_path(), 'data', 'out', 'example_humdata.tif'),
        'grid3':
        os.path.join(get_project_path(), 'data', 'out', 'example_grid3.tif')
    }

    patch_size = 60
    thresholds = [0.2, 0.5, 0.8]
    threads = 12
    impl = AggregatedMetrics

    for threshold in thresholds:
        print("Compute metrics for patch_size=", patch_size, ", threshold=",
              threshold)
        # Build output filename:
        dirpath = os.path.join(get_project_path(), "data", "out")
        filename = '%s_metrics_p%d_t%d.tif' % (country.lower(), patch_size,
                                               int(threshold * 100))

        scheduler = RasterTableScheduler(in_files['humdata'],
                                         in_files['grid3'],
                                         patch_size,
                                         threshold,
                                         threads,
                                         fake=False,
                                         metrics_impl=impl)

        scheduler.run()
        scheduler.save(dirpath, filename)

    return 0
 def setUp(self):
     with open(
             path.join(get_project_path(), "test", "data",
                       "ny_coord.json")) as file:
         self.gt_coords_single = json.load(file)
     with open(
             path.join(get_project_path(), "test", "data",
                       "list_coord.json")) as file:
         self.gt_coords_list = json.load(file)
     return
    def test_raster_table_size_aligned_iterator_exhaustiveness(self):
        pairs = [(1800, 1800), (103, 160), (303, 254), (55, 11)]
        for i, pair in enumerate(pairs):
            for limit in self.memory_limits:
                outfile1 = path.join(get_project_path(), "test", "data", "tmp",
                                     "example_salign_h%s.tif" % i)
                outfile2 = path.join(get_project_path(), "test", "data", "tmp",
                                     "example_salign_g%s.tif" % i)
                table = RasterTableSizeAligned(self.nga_example_humdata,
                                               self.nga_example_grid3, pair[0],
                                               pair[1])
                table.set_memory_limit(limit)

                with rasterio.open(
                        outfile1,
                        'w',
                        driver='GTiff',
                        width=table.get_raster()[0].width,
                        height=table.get_raster()[0].height,
                        count=1,
                        dtype=rasterio.float64) as dst1, rasterio.open(
                            outfile2,
                            'w',
                            driver='GTiff',
                            width=table.get_raster()[1].width,
                            height=table.get_raster()[1].height,
                            count=1,
                            dtype=rasterio.ubyte) as dst2:

                    for w_hum, w_grid3 in table:
                        dst1.write(w_hum.data, window=w_hum.window, indexes=1)
                        dst2.write(w_grid3.data,
                                   window=w_grid3.window,
                                   indexes=1)

                with rasterio.open(
                        self.nga_example_humdata) as dataset, rasterio.open(
                            outfile1) as bypatch:
                    X = dataset.read()
                    Y = bypatch.read()
                    X = np.nan_to_num(X)
                    Y = np.nan_to_num(Y)
                    self.assertTrue((X == Y).all())

                with rasterio.open(
                        self.nga_example_grid3) as dataset, rasterio.open(
                            outfile2) as bypatch:
                    X = dataset.read()
                    Y = bypatch.read()
                    self.assertTrue((X == Y).astype(np.uint8).mean() >= 0.95)
def main(argc, argv):
    country = 'NGA'
    fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(15, 10))
    cmap = plt.cm.magma

    bbox = box(3.35-0.1, 6.5-0.1, 3.35+0.1, 6.5+0.1)
    geo = gpd.GeoDataFrame({'geometry': bbox}, index=[0], crs='EPSG:4326')
    coords = geo['geometry']

    # Worldpop:
    wp_file = os.path.join(get_project_path(), "data/worldpop", '%s/%s_ppp_2015.tif' % (country, country.lower()))
    with rasterio.open(wp_file) as pop:
        X = pop.read(1)
        out_img, out_transform = mask.mask(pop, coords, crop=True)
        pop_wp = np.float32(out_img[0].copy())
        pop_wp[np.where(pop_wp==-99999)] = 0
        im1 = ax1.imshow(pop_wp, cmap=cmap, norm=LogNorm())

    # Humdata:
    fb_file = os.path.join(get_project_path(), "data/humdata", '%s/population_%s_2018-10-01.tif' % (country, country.lower()))
    with rasterio.open(fb_file) as pop:
        X = pop.read(1)
        out_img, out_transform = mask.mask(pop, coords, crop=True)
        pop_fb = np.float32(out_img[0].copy())
        im2 = ax2.imshow(pop_fb, cmap=cmap, norm=LogNorm())

    # GRID3:
    grid_file = os.path.join(get_project_path(), "data/grid", '%s/%s_population.tif' % (country, country.lower()))
    with rasterio.open(grid_file) as pop:
        X = pop.read(1)
        sum = np.mean(X)
        out_img, out_transform = mask.mask(pop, coords, crop=True)
        pop_fb = np.float32(out_img[0].copy())
        im3 = ax3.imshow(pop_fb, cmap=cmap, norm=LogNorm())

    # Satelite:
    g = GoogleVisibleMap(x=[3.35-0.1, 3.35+0.1], y=[6.5-0.1, 6.5+0.1],
            size_x = 500, size_y = 500,
            #size_x=img_arr1.shape[0], size_y=img_arr1.shape[1],
            scale=4,  # scale is for more details
            maptype='satellite'
        )  # try out also: 'terrain'

    ggl_img = g.get_vardata()
    ax4.imshow(ggl_img)

    plt.show()
    return 0
def main(argc, argv):

    country = 'NGA'
    wp_file = os.path.join(get_project_path(), "data/worldpop", '%s_ppp_2015.tif' % country.lower())

    with rasterio.open(wp_file) as pop:
        X = pop.read(1)

        bbox = box(3.35-0.1, 6.5-0.1, 3.35+0.1, 6.5+0.1)
        geo = gpd.GeoDataFrame({'geometry': bbox}, index=[0], crs='EPSG:4326')

        coords = geo['geometry']
        out_img, out_transform = mask.mask(pop, coords, crop=True)
        pop_wp = np.float32(out_img[0].copy())

        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 10))
        cmap = plt.cm.magma

        im1 = ax1.imshow(pop_wp, cmap=cmap, norm=LogNorm())
        cbar = fig.colorbar(im1, ax=ax1, pad=0.005, fraction=0.1)
        cbar.set_label('Population density', rotation=90)
        ax1.axis('on')

        ax2.imshow(X, cmap=cmap, norm=LogNorm())
        
        plt.xticks([])
        plt.yticks([])
        plt.show()

    return 0
def main(argc, argv):
    country = 'NGA'
    wp_file = os.path.join(get_project_path(), "data", "humdata",
                           'population_%s_2018-10-01.tif' % country.lower())

    patch_size = 8192  # [px]
    output_dirpath = os.path.join(get_project_path(), "data", "humdata",
                                  '%s' % country, "patches")

    # Create directory if doesn't exist:
    os.system('mkdir -p %s' % output_dirpath)
    # Run splitting with gdal:
    os.system('gdal_retile.py -ps %d %d -targetDir %s %s' %
              (patch_size, patch_size, output_dirpath, wp_file))

    return 0
    def test_raster_table_iterator_exhaustiveness(self):
        pairs = [(1, 1), (2, 2), (3, 11), (103, 10), (7, 4)]
        for j, filename in enumerate(self.infiles):
            for limit in self.memory_limits:
                for i, pair in enumerate(pairs):
                    outfile = path.join(get_project_path(), "test", "data",
                                        "tmp", "example0_%s_%s.tif" % (j, i))
                    table = RasterTable(filename, pair[0], pair[1])
                    table.set_memory_limit(limit)

                    with rasterio.open(outfile,
                                       'w',
                                       driver='GTiff',
                                       width=table.get_raster().width,
                                       height=table.get_raster().height,
                                       count=1,
                                       dtype=rasterio.ubyte) as dst:

                        for window in table:
                            data = np.array(window.data, dtype=rasterio.ubyte)
                            width, height = window.size
                            row, col = window.pos
                            dst.write(data, window=window.window, indexes=1)

                    with rasterio.open(filename) as dataset, rasterio.open(
                            outfile) as bypatch:
                        X = dataset.read()
                        Y = bypatch.read()
                        self.assertTrue((X == Y).all())
Example #8
0
def get_shdi(country_iso_code='NGA'):
    hdi_path = os.path.join(get_project_path(), 'data', 'shdi',
                            'SHDI Complete 4.0 (1).csv')
    data = pd.read_csv(
        hdi_path, usecols=['iso_code', 'year', 'level', 'GDLCODE', 'shdi'])
    data = data.set_index('iso_code', drop=False)
    data = data.loc[(data['iso_code'] == country_iso_code)
                    & (data['year'] == 2018) & (data['level'] == 'Subnat')]
    return np.array(data.loc[:, ['GDLCODE', 'shdi']])
Example #9
0
def main(argc, argv):
    in_files = get_dataset_paths('NGA')
    names = ['True positive', 'False positive', 'False negative', 
        'True negative', 'Accuracy', 'Recall', 'Precision', 'F1 Score']
    
    run_editor = True
    
    m_inx = 0
    metrics_path = [
        os.path.join(get_project_path(), 'data', 'metrics', 'pipeline-counts.tif'),
        os.path.join(get_project_path(), 'data', 'results', 'nga_metrics_p30_t20.tif'),
        os.path.join(get_project_path(), 'data', 'results', 'nga_metrics_p30_t50.tif'),
        os.path.join(get_project_path(), 'data', 'results', 'nga_metrics_p30_t80.tif')
    ]
    
    if not run_editor:
        # Plot the entire dataset:
        fig2, ax2 = plt.subplots(2, 2, figsize=(15, 10))
        fig1, ax1 = plt.subplots(2, 2, figsize=(15, 10))

        table = RasterTable(metrics_path[m_inx], 1, 1)
        for layer, ax in enumerate(ax1.ravel()):
            ax.imshow(table(layer + 1).get(0, 0).data, cmap='magma', norm=LogNorm())
            ax.set_title(names[layer])

        for layer, ax in enumerate(ax2.ravel()):
            ax.imshow(table(4 + layer + 1).get(0, 0).data, cmap='jet')
            ax.set_title(names[4 + layer])
        plt.show()

    else:
        # lat, lon, zoom = (6.541456, 3.312719, 109) # Lagos
        # lat, lon, zoom = (6.457581, 3.380313, 54) # Lagos
        lat, lon, zoom = (8.499714, 3.423570, 27) # Ago-Are
        # lat, lon, zoom = (7.382932, 3.929635, 432) # Ibadan
        # lat, lon, zoom = (4.850891, 6.993961, 109) # Port Harcourt
        # lat, lon, zoom = (11.961825, 8.540164, 213) # Kano

        editor = AlignMapsEditor(
            in_files['grid3'], metrics_path[1], (lat, lon, zoom), 
            'GRID3', 'NORM', index2=5) # 5 is accuracy
        editor.wait()

    return 0
 def setUp(self):
     self.infile = path.join(get_project_path(), "test", "data",
                             "example1.tif")
     self.infiles = [
         "example0.tif", "example1.tif", "example2.tif", "example3.tif",
         "example4.tif"
     ]
     self.infiles = [
         path.join(get_project_path(), "test", "data", filename)
         for filename in self.infiles
     ]
     self.nga_example_humdata = path.join(get_project_path(), "test",
                                          "data", "align",
                                          "example_humdata.tif")
     self.nga_example_grid3 = path.join(get_project_path(), "test", "data",
                                        "align", "example_grid3.tif")
     self.memory_limits = [
         128, 1024, 3 * 1024, 1024 * 1024, 1024 * 1024 * 1024
     ]
Example #11
0
def main(argc, argv):

    country = 'NGA'
    in_file = os.path.join(
        get_project_path(), "data", "humdata",
        '%s/population_%s_2018-10-01.tif' % (country, country.lower()))
    out_file = os.path.join(get_project_path(), "data", "humdata",
                            '%s' % country,
                            'population_%s_out.tif' % country.lower())

    ds = gdal.Open(in_file)
    geo_s = ds.GetGeoTransform()
    x_size = ds.RasterXSize  # Raster xsize
    y_size = ds.RasterYSize  # Raster ysize
    # ds = gdal.Warp('', in_file, dstSRS='EPSG:4326')

    # Read this!!!: https://gdal.org/user/virtual_file_systems.html
    # mem_drv = gdal.GetDriverByName('MEM')

    # ds = gdal.Warp(out_file, in_file, dstSRS='EPSG:4326', format='GTiff')
    ds = gdal.Translate(out_file,
                        ds,
                        projWin=[3.35 - 0.1, 6.5 + 0.1, 3.35 + 0.1, 6.5 - 0.1],
                        projWinSRS='EPSG:4326')
    ds = None

    with rasterio.open(out_file) as pop:
        X = pop.read(1)

        fig, ax1 = plt.subplots()
        cmap = plt.cm.magma

        im1 = ax1.imshow(X, cmap=cmap, norm=LogNorm())
        cbar = fig.colorbar(im1, ax=ax1, pad=0.005, fraction=0.1)
        cbar.set_label('Population density', rotation=90)
        ax1.axis('on')

        plt.show()

    return 0
Example #12
0
 def __before(self):
     # Create a copy of dataset per thread:
     dest_path = os.path.join(get_project_path(), "data", "tmp")
     os.system('mkdir -p %s' % dest_path)
     for tix in range(self.n_threads):
         # Copy hrsl:
         dest_filepath = os.path.join(dest_path, 'hrsl_%d.tif' % (tix + 1))
         self.hrsl_path_thread.append(dest_filepath)
         os.system('cp "%s" "%s"' % (self.hrsl_path, dest_filepath))
         # Copy grid3:
         dest_filepath = os.path.join(dest_path, 'grid3_%d.tif' % (tix + 1))
         self.grid3_path_thread.append(dest_filepath)
         os.system('cp "%s" "%s"' % (self.grid3_path, dest_filepath))
Example #13
0
    def setUp(self):
        self.cities = ['lagos1', 'lagos2', 'ago_are', 'ibadan', 'port_harcourt']
        self.spreads = ['003', '01', '05']

        self.small_grid3_path = [os.path.join(get_project_path(), 'test', 'data', 'metrics', '%s_%s_grid3.tif' % (city, self.spreads[0])) for city in self.cities]
        self.middle_grid3_path = [os.path.join(get_project_path(), 'test', 'data', 'metrics', '%s_%s_grid3.tif' % (city, self.spreads[1])) for city in self.cities]
        self.large_grid3_path = [os.path.join(get_project_path(), 'test', 'data', 'metrics', '%s_%s_grid3.tif' % (city, self.spreads[2])) for city in self.cities]

        self.small_humdata_path = [os.path.join(get_project_path(), 'test', 'data', 'metrics', '%s_%s_humdata.tif' % (city, self.spreads[0])) for city in self.cities]
        self.middle_humdata_path = [os.path.join(get_project_path(), 'test', 'data', 'metrics', '%s_%s_humdata.tif' % (city, self.spreads[1])) for city in self.cities]
        self.large_humdata_path = [os.path.join(get_project_path(), 'test', 'data', 'metrics', '%s_%s_humdata.tif' % (city, self.spreads[2])) for city in self.cities]
def get_shapes_rasterstats(country_of_interest='Nigeria'):
    """
    Extracts shapes of admin 1 level for a given country.
    Shapes source SHDI shape files(maps)
    Returns regions, a list of original shapes grouped by a country
    """
    shapefile_name = 'GDL Shapefiles V4.shp' # all the countries in the world
    shapefile_path = os.path.join(get_project_path(), 'data/shapefiles', shapefile_name)

    with fiona.open(shapefile_path) as shapes:

        regions = []

        for shp in shapes:
            if(shp['properties']['country']== country_of_interest):
                regions.append(shp)
    return regions
def get_shapes(country_of_interest='Nigeria'):
    """
    Extracts shapes of admin 1 level for a given country.
    Shapes source SHDI shape files(maps)
    Returns regions, a dictionary where key=GDLcode(e.g. NGAr101)
    and value=shape geometry(e.g. shapely.geometry.multipolygon.MultiPolygon or Polygon)
    """
    shapefile_name = 'GDL Shapefiles V4.shp' # all the countries in the world
    shapefile_path = os.path.join(get_project_path(), 'data/shapefiles', shapefile_name)

    with fiona.open(shapefile_path) as shapes:

        regions = {}

        for shp in shapes:
            if(shp['properties']['country']== country_of_interest):
                regions[shp['properties']['GDLcode']] = shp['geometry']
    return regions
Example #16
0
def main(argc, argv):

    country = 'NGA'
    # in_file = os.path.join(
    #     get_project_path(), "data", "humdata", '%s' % country, 'population_%s_2018-10-01.tif' % country.lower())

    in_file = os.path.join(get_project_path(), "data", "grid3",
                           'NGA - population - v1.2 - mastergrid.tif')

    # cmap = plt.cm.magma
    geo = GeoLocation()
    with rasterio.open(in_file) as dataset:
        # Loop through your list of coords
        # for i, (lon, lat) in enumerate(coordinates):

        # Get pixel coordinates from map
        coords = geo.get_coordinates('Lagos, Nigeria')
        lon, lat = coords['lon'], coords['lat']
        px, py = dataset.index(lon, lat)
        print('Pixel Y, X coords: {}, {}'.format(py, px))

        # window = get_window_px(dataset, px - 500, py - 500, 1000, 1000)
        # rows, cols = rasterio.transform.rowcol(dataset.transform, [3.35-0.1, 3.35+0.1], [6.5-0.1, 6.5+0.1])
        window = get_window_geo(
            dataset, box(3.35 - 0.1, 6.5 - 0.1, 3.35 + 0.1, 6.5 + 0.1))
        window = window * 255
        fig, ax = plt.subplots()
        ax.imshow(window)
        plt.show()

        # Build an NxN window
        # window = rasterio.windows.Window(px - N // 2, py - N // 2, N, N)
        # print(window)

        # # Read the data in the window
        # # clip is a nbands * N * N numpy array
        # clip = dataset.read(window=window)

        # # You can then write out a new file
        # meta = dataset.meta
        # meta['width'], meta['height'] = N, N
        # meta['transform'] = rio.windows.transform(window, dataset.transform)

    return 0
Example #17
0
def main(argc, argv):
    # First run split_example.py

    country = 'NGA'
    wp_dir = os.path.join(get_project_path(), "data", "humdata",
                          '%s' % country, 'patches', '*.tif')

    # Iterates over smaller patches and plots entire patch:
    for filepath in glob.iglob(wp_dir):
        print(filepath)
        with rasterio.open(filepath) as pop:
            X = pop.read(1)

            fig, ax1 = plt.subplots()
            cmap = plt.cm.magma

            im1 = ax1.imshow(X, cmap=cmap, norm=LogNorm())
            cbar = fig.colorbar(im1, ax=ax1, pad=0.005, fraction=0.1)
            cbar.set_label('Population density', rotation=90)
            ax1.axis('on')

            plt.show()

    return 0
Example #18
0
 def __after(self):
     # Remove all copies of datasets:
     dest_path = os.path.join(get_project_path(), "data", "tmp")
     os.system('rm -r %s' % dest_path)
import rasterio
from rasterio.warp import calculate_default_transform, reproject, Resampling
from fiona.crs import from_epsg
from humset.utils.definitions import get_project_path
import os

country = 'NGA'
in_file = os.path.join(get_project_path(), "data", "humdata", 'population_%s_2018-10-01.tif' % country.lower())
out_file = os.path.join(get_project_path(), "data", "humdata", "out", 'population_%s_2018-10-01.tif' % country.lower())

with rasterio.Env():
    gdal_data = os.environ['GDAL_DATA']
    print(gdal_data)
    proj_lib = os.environ['PROJ_LIB']
    print(proj_lib)

    dst_crs = {'init': 'EPSG:3857'}

    with rasterio.open(in_file) as src:
        # transform, width, height = calculate_default_transform(src.crs, dst_crs, 
        #                                                     src.width, 
        #                                                     src.height, 
        #                                                     *src.bounds)
        kwargs = src.meta.copy()
        # kwargs.update({'crs': dst_crs,'transform': transform, 'width': width,'height': height})
        print(kwargs['transform'])

        calculate_default_transform

        with rasterio.open(out_file, 'w', **kwargs) as dst:
                reproject(source=rasterio.band(src, 1),destination=rasterio.band(dst, 1),
 def tearDownClass(cls):
     tmpdir = path.join(get_project_path(), "test", "data", "tmp", "*.tif")
     for tif in glob.glob(tmpdir):
         os.remove(tif)
Example #21
0
import numpy as np
import rasterio
import rasterio.mask as mask
import geopandas as gpd
import pandas as pd
import humset.shapefiles_by_country as sbc
from fiona.crs import from_epsg
from shapely.geometry import box
import matplotlib.pyplot as plt
import rasterstats
from scipy import stats

from humset.utils.definitions import get_project_path
from rasterstats import zonal_stats

hdi_path = os.path.join(get_project_path(), 'data/hdi',
                        'GDL-Sub-national-HDI-data.csv')
hdi_nigeria = pd.read_csv(hdi_path, usecols=['GDLCODE', '2018'])
region_codes = np.array(hdi_nigeria)[1:, 0]
ix = np.argsort(region_codes)
region_codes = region_codes[ix]
region_hdis = np.array(hdi_nigeria)[1:, 1]
region_hdis = region_hdis[ix]
code_hdi = list(zip(region_codes, region_hdis))

optimistic = 'nga_metrics_p30_t20.tif'
pesimistic = 'nga_metrics_p30_t80.tif'
medium = 'nga_metrics_p30_t50.tif'

metrics_path = os.path.join(get_project_path(), 'data/metrics', optimistic)
with rasterio.open(metrics_path) as src:
Example #22
0
shdi = region_shdi[:, 1]
# print(region_shdi)

# hdi_nigeria = pd.read_csv(hdi_path, usecols=['GDLCODE','2018'])
# region_codes = np.array(hdi_nigeria)[1:,0]
# ix = np.argsort(region_codes)
# region_codes = region_codes[ix]
# region_hdis =  np.array(hdi_nigeria)[1:,1]
# region_hdis = region_hdis[ix]
# code_hdi = list(zip(region_codes, region_hdis))

optimistic = 'nga_metrics_p30_t20.tif'
pesimistic = 'nga_metrics_p30_t80.tif'
medium = 'nga_metrics_p30_t50.tif'

metrics_path = os.path.join(get_project_path(), 'data', 'results', optimistic)
with rasterio.open(metrics_path) as src:
    affine = src.transform
    tp_layer = src.read(1)
    fp_layer = src.read(2)
    fn_layer = src.read(3)
    tn_layer = src.read(4)
    acc_layer = src.read(5)
    recall_layer = src.read(6)
    prec_layer = src.read(7)
    f1_layer = src.read(8)

region_shapes = sbc.get_shapes_rasterstats('Nigeria')

# confusion matrix
true_positive = zonal_stats(region_shapes,
import itertools
import rasterio
from salem import get_demo_file, DataLevels, GoogleVisibleMap, Map, GoogleCenterMap
import salem
from shapely.geometry import box
from matplotlib.colors import LogNorm
from utils.location import GeoLocation

from humset.utils.definitions import get_project_path
from humset.utils.raster import RasterTable, RasterTableSize, get_window_px, get_window_geo
from humset.visualization import AlignMapsEditor

### PARAMS ###
country = 'NGA'
in_files = [
    os.path.join(get_project_path(), "data", "humdata",
                 'population_%s_2018-10-01.tif' % country.lower()),
    os.path.join(get_project_path(), "data", "worldpop",
                 '%s_ppp_2015.tif' % country.lower()),
    os.path.join(get_project_path(), "data", "grid3",
                 '%s - population - v1.2 - mastergrid.tif' % country)
]

# lon, lat = (3.36, 6.49)  # Lagos 1
# lat, lon = (6.541456, 3.312719)  # Lagos 2
lat, lon = (8.499714, 3.423570)  # Ago-Are
# lat, lon = (7.382932, 3.929635) # Ibadan
# lat, lon = (4.850891, 6.993961) # Port Harcourt


def get_grid_shape(img, patch_shape):
import numpy as np
import cv2, rasterio, salem, os
from salem import get_demo_file, DataLevels, GoogleVisibleMap, Map, GoogleCenterMap
from shapely.geometry import box
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

from humset.utils.definitions import get_project_path
from humset.utils.raster import get_window_geo

country = 'NGA'
in_files = [
    os.path.join(get_project_path(), "data", "humdata",
                 'population_%s_2018-10-01.tif' % country.lower()),
    os.path.join(get_project_path(), "data", "worldpop",
                 '%s_ppp_2015.tif' % country.lower()),
    os.path.join(get_project_path(), "data", "grid3",
                 '%s - population - v1.2 - mastergrid.tif' % country)
]

lat, lon = (6.458601, 3.379998)  # Lagos 1
# lat, lon = (6.541456, 3.312719)  # Lagos 2
# lat, lon = (8.499714, 3.423570) # Ago-Are
# lat, lon = (7.382932, 3.929635) # Ibadan
# lat, lon = (4.850891, 6.993961) # Port Harcourt

box_spread = 0.01
with rasterio.open(in_files[0]) as humdata, rasterio.open(
        in_files[2]) as grid3:

    h_data, h_window = get_window_geo(
Example #25
0
def main(argc, argv):

    country = 'NGA'
    fb_infile = os.path.join(get_project_path(), "data", "humdata",
                             'population_%s_2018-10-01.tif' % country.lower())

    grid_infile = os.path.join(
        get_project_path(), "data", "grid3",
        '%s - population - v1.2 - mastergrid.tif' % country)

    xsplit, ysplit = 200, 200
    fb = RasterTable(fb_infile, xsplit, ysplit)
    grid = RasterTable(grid_infile, xsplit, ysplit)

    metrics = np.zeros((xsplit, ysplit, 8))
    for fb_win in fb:
        # (fb_win, (row, col), (width, height))
        row, col = fb_win.pos
        width, height = fb_win.size
        grid_win = grid.get(row, col)

        # Process windows:
        w, h = min(fb_win.size[1],
                   grid_win.size[1]), min(fb_win.size[0], grid_win.size[0])
        fb_win.data = np.nan_to_num(fb_win.data)
        fb_win.data[np.where(fb_win.data > 0)] = 255
        fb_win.data = fb_win.data[:h, :w]
        grid_win.data = grid_win.data * 255
        grid_win.data = upsize(
            grid_win.data, 3,
            3)  # fb-dataset has 1-arcsec resolution, grid has 3-arcsec
        grid_win.data = grid_win.data[:h, :w]

        diff_win = fb_win.data - grid_win.data

        FP = (diff_win > 0).astype(int)
        FN = (diff_win < 0).astype(int)

        metrics[row, col, 0] = FP.mean()
        metrics[row, col, 1] = FN.mean()
        coo_grid = np.array(grid.find_geo_coords(row, col, 0, 0))
        coo_fb = np.array(fb.find_geo_coords(row, col, 0, 0))
        shift = coo_grid - coo_fb
        metrics[row, col, 2] = coo_grid[0]
        metrics[row, col, 3] = coo_grid[1]
        metrics[row, col, 4] = coo_fb[0]
        metrics[row, col, 5] = coo_fb[1]
        metrics[row, col, 6] = shift[0]
        metrics[row, col, 7] = shift[1]

        # if fb_win.data.mean() > 50:
        #     # coo_grid = np.array(grid.get_coords(0, 0))
        #     # coo_fb = np.array(fb.get_coords(0, 0))
        #     # shift = coo_grid - coo_fb
        #     print("GRID3: {}".format((coo_grid[0], coo_grid[1])))
        #     print("FB: {}".format((coo_fb[0], coo_fb[1])))
        #     print("Shift: {}".format((shift[0], shift[1])))

        #     fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 10))
        #     ax1.imshow(fb_win.data, cmap='gray')
        #     ax1.set_title('Facebook')
        #     ax2.imshow(grid_win.data, cmap='gray')
        #     ax2.set_title('GRID3')
        #     ax3.imshow(FP, cmap='gray')
        #     ax3.set_title('FP')
        #     ax4.imshow(FN, cmap='gray')
        #     ax4.set_title('FN')
        #     plt.show()

    c = plt.imshow(metrics[:, :, 0])
    # plt.colorobar(c)
    plt.suptitle('HRSL-Grid3 FP rate Nigeria')
    plt.show()

    #np.savetxt('NGA-FP.csv', metrics[:,:,0], delimiter=',')
    #np.savetxt('NGA-FN.csv', metrics[:,:,1], delimiter=',')

    return 0
Example #26
0
import matplotlib.pyplot as plt

from humset.utils.definitions import get_project_path


def getFeatures(gdf):
    """Function to parse features from GeoDataFrame in such a manner that rasterio wants them"""
    import json
    return [json.loads(gdf.to_json())['features'][0]['geometry']]


optimistic = 'nga_metrics_p30_t20.tif'
pesimistic = 'nga_metrics_p30_t80.tif'
medium = 'nga_metrics_p30_t50.tif'

metrics_tif = os.path.join(get_project_path(), 'data/results', medium)

raster_metrics = rasterio.open(metrics_tif)

regions = sbc.get_shapes('Nigeria')
# getting shape of Anambra
shape = regions.get('NGAr102')['coordinates']
# lon and lat of Anambra
lat, lon = 6.221173, 6.971196

tam = 0.2
bbox = box(lon - tam, lat - tam, lon + tam, lat + tam)

geo = gpd.GeoDataFrame({'geometry': bbox}, index=[0])

coords = getFeatures(geo)
def main(argc, argv):

    in_files = get_dataset_paths('NGA')
    cmap = plt.cm.magma

    # Generate samples tiffs
    # it = RasterWindow(in_file, 10, 10)
    # y = (1000 - 9*100) / 8
    # image = np.ones((100, 100), dtype=rasterio.ubyte) * 127
    # with rasterio.open(
    #         'example_%s_%s.tif' % (0,1), 'w',
    #         driver='GTiff', width=1000, height=1000, count=1,
    #         dtype=image.dtype) as dst:
    #     for i, j in itertools.product(range(9), range(9)):
    #         dst.write(image, window=Window(j * (100+y), i * (100+y), 100, 100), indexes=1)

    # mean = [500, 500]
    # cov = [[10000, 0], [0, 10000]]
    # x, y = np.random.multivariate_normal(mean, cov, 100000).T
    # x, y = x.astype(np.uint32), y.astype(np.uint32)

    # image = np.zeros((1000, 1000), dtype=rasterio.ubyte)
    # image[x, y] = 255
    # with rasterio.open(
    #     'example_%s_%s.tif' % (0,0), 'w',
    #     driver='GTiff', width=1000, height=1000, count=1,
    #     dtype=image.dtype) as dst:

    #     dst.write(image, indexes=1)

    # Read by window, define horizontal and vertical split.
    # e.g. RasterTable(in_file, 8, 4) splits underlying data into matrix of 8 columns and 4 rows.
    # table = RasterTable(in_file, 8, 4)
    # for window in table:
    #     fig, ax = plt.subplots()
    #     ax.imshow(window.data, cmap=cmap, norm=LogNorm())
    #     plt.show()

    # Read by window size, 5000 X 3000 px
    # Note: The sizes around edge will differ from given size. You can get these values from (width, height)
    table = RasterTableSize(in_files['humdata'], 2000, 1000)
    for window in table:
        fig, ax = plt.subplots()
        ax.imshow(window.data, cmap=cmap, norm=LogNorm())
        plt.show()

    table = RasterTableAligned(in_files['humdata'], in_files['grid3'], 10, 10)
    for w_hum, w_grid3 in table:
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 10))
        ax1.imshow(w_hum.data, cmap=cmap, norm=LogNorm())
        w_grid3.data = w_grid3.data * 255
        ax2.imshow(w_grid3.data, cmap='gray')
        plt.show()

    table = RasterTable(os.path.join(get_project_path(), 'data', 'out',
                                     'nga_metrics.tif'),
                        1,
                        1,
                        index=1)
    for window in table:
        fig, ax = plt.subplots()
        ax.imshow(window.data, cmap=cmap, norm=LogNorm())
        plt.show()

    return 0