Exemplo n.º 1
0
def gen_img_samples(src, chunk_size, *band_order):
    """

    Args:
        src: input image (rasterio object)
        chunk_size: image tile size
        *band_order: ignore

    Returns: generator object

    """
    subdiv = 2.0
    step = int(chunk_size / subdiv)
    for row in range(0, src.height, step):
        for column in range(0, src.width, step):
            window = Window.from_slices(slice(row, row + chunk_size),
                                        slice(column, column + chunk_size))
            if band_order:
                window_array = reshape_as_image(
                    src.read(band_order[0], window=window))
            else:
                window_array = reshape_as_image(src.read(window=window))
            if window_array.shape[0] < chunk_size or window_array.shape[
                    1] < chunk_size:
                window_array = _pad_diff(window_array, window_array.shape[0],
                                         window_array.shape[1], chunk_size)
            window_array = _pad(window_array, chunk_size)

            yield window_array, row, column
Exemplo n.º 2
0
def dtc_pred_stack(model,stack2pred_img,outfile=None):

    """
    Function to classfy raster stack image 
    """
    if type(stack2pred_img)==str:
        with rio.open(stack2pred_img) as src:
            img = src.read()
            profile = src.profile
            reshaped_img = reshape_as_image(img)
            raster_pred = model.predict(reshaped_img.reshape(-1,len(img)))
            class_prediction = raster_pred.reshape(reshaped_img[:, :, 0].shape)
            class_prediction = class_prediction + 1
            profile.update({'nodata': 0,'dtype': rio.uint8,'count':1})

        with rio.open(outfile, 'w', **profile) as dst:
            dst.write(class_prediction.astype(rio.uint8), 1)
            
    elif type(stack2pred_img)==np.ndarray:
        stack2pred_img[np.isnan(stack2pred_img)]=0
        reshaped_img = reshape_as_image(stack2pred_img)
        raster_pred = model.predict(reshaped_img.reshape(-1,len(stack2pred_img)))
        class_prediction = raster_pred.reshape(reshaped_img[:, :, 0].shape)
        class_prediction = class_prediction + 1
        return class_prediction.astype(rio.uint8)
 

        

        
        
    def divide_into_pieces(self, filename, image_path, data_path):
        os.makedirs(f'{data_path}/images', exist_ok=True)
        os.makedirs(f'{data_path}/geojson_polygons', exist_ok=True)

        full_mask = imageio.imread(f'{data_path}/full_mask.png')

        with rasterio.open(image_path) as src, open(
                f'{data_path}/image_pieces.csv', 'w') as csvFile:
            writer = csv.writer(csvFile)
            writer.writerow([
                'original_image', 'piece_image', 'piece_geojson', 'start_x',
                'start_y', 'width', 'height'
            ])

            for j in tqdm(range(0, src.height // self.height)):
                for i in range(0, src.width // self.width):
                    raster_window = src.read(
                        window=Window(i * self.width, j *
                                      self.height, self.width, self.height))
                    image_array = reshape_as_image(raster_window)[:, :, :3]

                    is_mask = full_mask[j * self.height:j * self.height +
                                        self.height,
                                        i * self.width:i * self.width +
                                        self.width].sum() > 0

                    if image_non_zero(image_array) and is_mask:
                        image_format = 'tiff'
                        piece_name = f'{filename}_{j}_{i}.{image_format}'

                        poly = Polygon([
                            src.xy(j * self.height, i * self.width),
                            src.xy(j * self.height, (i + 1) * self.width),
                            src.xy((j + 1) * self.height,
                                   (i + 1) * self.width),
                            src.xy((j + 1) * self.height, i * self.width),
                            src.xy(j * self.height, i * self.width)
                        ])
                        gs = GeoSeries([poly])
                        gs.crs = src.crs
                        piece_geojson_name = f'{filename}_{j}_{i}.geojson'
                        gs.to_file(
                            f'{data_path}/geojson_polygons/{piece_geojson_name}',
                            driver='GeoJSON')
                        image_array = reshape_as_image(raster_window)

                        meta = src.meta
                        meta['height'] = image_array.shape[0]
                        meta['width'] = image_array.shape[1]
                        with rasterio.open(f'{data_path}/images/{piece_name}',
                                           'w', **meta) as dst:
                            for ix in range(image_array.shape[2]):
                                dst.write(image_array[:, :, ix], ix + 1)

                        writer.writerow([
                            filename, piece_name, piece_geojson_name,
                            i * self.width, j * self.height, self.width,
                            self.height
                        ])
def divide_into_pieces(image_path, save_path, width, height):
    if not os.path.exists(save_path):
        os.makedirs(save_path, exist_ok=True)
        print('Data directory created.')

    os.makedirs(f'{save_path}/images', exist_ok=True)
    os.makedirs(f'{save_path}/geojson_polygons', exist_ok=True)
    with rasterio.open(image_path) as src, open(
            f'{save_path}/image_pieces.csv', 'w') as csvFile:
        writer = csv.writer(csvFile)
        writer.writerow([
            'original_image', 'piece_image', 'piece_geojson', 'start_x',
            'start_y', 'width', 'height'
        ])

        for j in tqdm(range(0, src.height // height)):
            for i in range(0, src.width // width):
                raster_window = src.read(window=Window(i * width, j *
                                                       height, width, height))
                image_array = reshape_as_image(raster_window)[:, :, :3]

                if np.count_nonzero(image_array) > image_array.size * 0.9:
                    filename_w_ext = os.path.basename(image_path)
                    filename, _ = os.path.splitext(filename_w_ext)
                    image_format = 'tiff'
                    piece_name = f'{filename}_{j}_{i}.{image_format}'

                    poly = Polygon([
                        src.xy(j * height, i * width),
                        src.xy(j * height, (i + 1) * width),
                        src.xy((j + 1) * height, (i + 1) * width),
                        src.xy((j + 1) * height, i * width),
                        src.xy(j * height, i * width)
                    ])
                    gs = GeoSeries([poly])
                    gs.crs = src.crs
                    piece_geojson_name = f'{filename}_{j}_{i}.geojson'
                    gs.to_file(
                        f'{save_path}/geojson_polygons/{piece_geojson_name}',
                        driver='GeoJSON')
                    image_array = reshape_as_image(raster_window)

                    meta = src.meta
                    meta['height'] = image_array.shape[0]
                    meta['width'] = image_array.shape[1]
                    with rasterio.open(f'{save_path}/images/{piece_name}', 'w',
                                       **meta) as dst:
                        for ix in range(image_array.shape[2]):
                            dst.write(image_array[:, :, ix], ix + 1)

                        dst.close()

                    writer.writerow([
                        filename_w_ext, piece_name, piece_geojson_name,
                        i * width, j * height, width, height
                    ])

    csvFile.close()
def predict_raster(img_current,
                   img_previous,
                   channels,
                   network,
                   model_weights_path,
                   input_size=56,
                   neighbours=3):
    logging.info(f'network:{network}')
    logging.info(f'model_weights_path: {model_weights_path}')
    logging.info(f'channels: {channels}')
    logging.info(f'neighbours: {neighbours}')
    model, device = load_model(network, model_weights_path, channels,
                               neighbours)

    with rasterio.open(img_current) as source_current, \
         rasterio.open(img_previous) as source_previous:

        meta = source_current.meta
        meta['count'] = 1
        clearcut_mask = np.zeros((source_current.height, source_current.width))
        for i in tqdm(range(source_current.width // input_size)):
            for j in range(source_current.height // input_size):
                bottom_row = j * input_size
                upper_row = (j + 1) * input_size
                left_column = i * input_size
                right_column = (i + 1) * input_size

                corners = [
                    source_current.xy(bottom_row, left_column),
                    source_current.xy(bottom_row, right_column),
                    source_current.xy(upper_row, right_column),
                    source_current.xy(upper_row, left_column),
                    source_current.xy(bottom_row, left_column)
                ]

                window = Window(bottom_row, left_column, input_size,
                                input_size)
                image_current = reshape_as_image(
                    source_current.read(window=window))
                image_previous = reshape_as_image(
                    source_previous.read(window=window))

                difference_image = diff(image_current, image_previous)
                image_tensor = transforms.ToTensor()(difference_image.astype(
                    np.uint8)).to(device, dtype=torch.float)

                predicted = predict(image_tensor, model, channels, neighbours,
                                    input_size, device)
                predicted = mask_postprocess(predicted)
                clearcut_mask[left_column:right_column,
                              bottom_row:upper_row] += predicted

    meta['dtype'] = 'float32'
    return clearcut_mask.astype(np.float32), meta
def _parse_bytes_gdal_numpyfunc(img_bytes_np, tgt_bytes_np):
    with MemoryFile(img_bytes_np) as memfile:
        with memfile.open() as src:
            img_arr = src.read()

    with MemoryFile(tgt_bytes_np) as memfile:
        with memfile.open() as src:
            target_arr = src.read()

    return (reshape_as_image(img_arr).astype(np.float32),
            reshape_as_image(target_arr).astype(np.float32))
def show_rgb_with_mask(rgb, mask, alpha=0.5, figsize=(20, 20), title=""):
    """
    Show the rgb image overlayed with the mask image with the given alpha value applied
    to the mask image
    
    Args:
    - rgb (str/Path or np.array):
        - if str or Path, it's a path to the rgb file
        - if np.array, it's a 1, 3 or 4D numpy.array with (h,w,d,a) dimensions
    - mask (str/Path or np.array): 
        - if str or Path, it's a path to the mask file to overlay on top of the rgb
        - if np.array, uint8 type numpy array of the same size of `rgb_arr`
        - the image array must be 2 or 3 dimensional. If 3dim, one of the axis will be 
        assumed to be 1 and flattened out
    - alpha (float, [0,1])
    - figsize (tuple)
    
    Returns:
    - f, ax containing the rgb_arr overlayed by the mask image
    """
    f, ax = plt.subplots(figsize=figsize)

    # Set axis title
    ax_title = "RGB w/ road buffer mask"
    if len(title) > 0:
        ax_title += ": " + title
    ax.set_title(ax_title)

    # Read the images if the paths are the input args
    if not isinstance(rgb, (str, Path)):
        raise TypeError('First input should be a string or path object')
    if not isinstance(mask, (str, Path)):
        raise TypeError('Second input should be a string or path object')
    with rio.open(str(rgb), 'r') as ds:
        rgb = reshape_as_image(ds.read())
    with rio.open(str(mask), 'r') as ds:
        mask = reshape_as_image(ds.read())

    if not isinstance(rgb, np.ndarray):
        raise TypeError('rgb must be an np array at this point')
    if not isinstance(mask, np.ndarray):
        raise TypeError('mask must be an np array at this point')

    # Show rgb and mask overlay
    ax.imshow(rgb)

    # remove an empty dimension if mask np.array is 3dimensional
    # np.squeeze() is inplace
    ax.imshow(np.squeeze(mask), alpha=alpha)

    return f, ax
Exemplo n.º 8
0
    def data_as_image(self) -> numpy.ndarray:
        """Return the data array reshaped into an image processing/visualization software friendly order.

        (bands, rows, columns) -> (rows, columns, bands).

        """
        return reshape_as_image(self.data)
Exemplo n.º 9
0
def clip_advanced(in_raster, in_polygon, cliptype, clipped_raster=""):
    """
    Args:
        in_raster: raster to be clipped
        in_polygon: as 
        cliptype : either 'bbox', 'shp', 'geojson'
        clipped_raster: raster to be saved if not it will .....
    """

    with rio.open(in_raster) as src:
        out_meta = src.meta

        # get clip shape from polygon shape
        if cliptype == "shp":

            with fiona.open(in_polygon, "r") as polygon:
                shapes = [feature["geometry"] for feature in polygon]

        # get clip shape from bounding box
        elif cliptype == "bbox":

            # convert it to a polygon bbox
            bbox_pol = box(in_polygon[0], in_polygon[1], in_polygon[2],
                           in_polygon[3])

            # use geopandas to transform the bounding box into a json polygon
            geo = gpd.GeoDataFrame({'geometry': bbox_pol},
                                   index=[0],
                                   crs=src.crs)

            # convert it to a json polygon
            shapes = [json.loads(geo.to_json())['features'][0]['geometry']]

        # or getting directly a geojson string (as above)
        elif cliptype == "geojson":
            shapes = in_polygon

        else:
            None
            # print error

        # clipping of raster
        out_image, out_transform = mask.mask(src, shapes, crop=True)

    # if clipped raster, we save the data to a tif (otherwise just return out_image)
    if clipped_raster:
        out_meta.update({
            "driver": "GTiff",
            "height": out_image.shape[1],
            "width": out_image.shape[2],
            "transform": out_transform
        })

        with rio.open(clipped_raster, "w", **out_meta) as dst:
            dst.write(out_image)

    else:
        None

    return reshape_as_image(out_image)
Exemplo n.º 10
0
def read(in_raster, pixelextent=None):
    """Read a raster.

    Args:
        filename: File to read
        pixelextent: (xoff, yoff, nx, ny)

    Returns:
        numpy array of shape (bands, ny, nx)
    """

    # open in_raster image
    with rio.open(in_raster) as src:

        # if pixel extent is provided, get raster data only for pixels
        if pixelextent:

            # if a rio.Windows.Window is provided
            if type(pixelextent) == rio.windows.Window:
                array = src.read(window=pixelextent)

            # if a list provided
            else:
                array = src.read(window=rio.windows.Window(*pixelextent))
        else:
            array = src.read()

    # reshape to (rows, columns, bands) from (bands, rows, columns)
    return reshape_as_image(array)
Exemplo n.º 11
0
def compute_pca_score(file,
                      band_order,
                      bands,
                      nr_components,
                      subset_name=None):

    expvar_data = {}

    #read all bands (except B9 and B1 = 60m) and metadata
    with rio.open(file) as src:
        img = src.read(band_order)
        profile = src.profile.copy()

        arr = reshape_as_image(img).reshape(-1, len(img))
        PC = PCA(n_components=nr_components, random_state=42).fit(arr)

        eigenvectors_df = pd.DataFrame(
            PC.components_,
            columns=bands,
            index=[f'PC{str(i)}' for i in range(1, nr_components + 1)])
        eigenvectors_df = round(eigenvectors_df, 3)
        expvar_data['variance'] = PC.explained_variance_ratio_
        if subset_name != None:
            eigenvectors_df['subset'] = subset_name

    pc_nr = [f'PC{str(i)}' for i in range(1, nr_components + 1)]
    expvar_df = pd.DataFrame(expvar_data, index=pc_nr).round(4)
    score = pd.concat([eigenvectors_df, expvar_df], axis=1)
    return (score)
Exemplo n.º 12
0
def stack_to_disk(rfiles, new_rname, new_rmeta, outdir, mask=None):
    """
    Stack several rasters layer on a single raster and
    save it to disk
    *********

    params:
        rfiles -> list of raster path to be stacked
        new_rname -> name of the final stack
        new_rmeta -> metadata of the final raster
        outdir -> output directory
        mask -> 3D boolean masked array

    """
    assert (new_rmeta['driver'] == 'GTiff'),\
        "Please use GTiff driver to write to disk. \
    Passed {} instead."                       .format(new_rmeta['driver'])

    name = new_rname + '.tif'
    new_rpath = os.path.join(outdir, name)
    with rasterio.open(new_rpath, 'w', **new_rmeta) as dst:
        for _id, fl in enumerate(rfiles, start=1):
            with rasterio.open(fl) as src1:
                np_arr = reshape_as_image(src1.read())
                if mask is not None:
                    np_arr = mask_and_fill(np_arr, mask)
                    dst.write_band(_id, np_arr[:, :,
                                               0].astype(new_rmeta['dtype']))
    return new_rpath
Exemplo n.º 13
0
def extract_images(urls, to_folder, test_data=False):
    fpath_tiff = urls[0]
    fpath_geojson = urls[1]
    
    print("Reading in: {} tif image".format(fpath_tiff.split("/")[-2]))
    with rasterio.open(fpath_tiff) as tiff:
            df_roof_geometries = gpd.read_file(fpath_geojson)

            tiff_crs = tiff.crs.data
            df_roof_geometries['projected_geometry'] = (
                df_roof_geometries['geometry'].to_crs(tiff_crs)
            )

            roof_geometries = ""
            if not test_data:
                roof_geometries = (
                    df_roof_geometries[['id', 'roof_material', 'projected_geometry']].values
                )
            else:
                roof_geometries = (
                    df_roof_geometries[['id', 'projected_geometry']].values
                )
            numof_data = len(roof_geometries)
            for i, roof in enumerate(roof_geometries):
                print("Generating {} -> {} / {}".format(fpath_tiff.split("/")[-2], i+1, numof_data))
                
                if not test_data:
                    roof_id, roof_material, projected_geometry = roof
                    target_path = os.path.join(to_folder, roof_material, str(roof_id)+'-'+roof_material+'.png')
                else:
                    roof_id, projected_geometry = roof
                    target_path = os.path.join(to_folder, str(roof_id)+'.png')

                img = mask(tiff, [projected_geometry], crop=True)[0]
                cv2.imwrite(target_path, reshape_as_image(img))
Exemplo n.º 14
0
def divide_into_pieces(image_path, save_path, pieces_file, width, height):
    if not os.path.exists(save_path):
        os.makedirs(save_path, exist_ok=True)
        print('Data directory created.')

    with rasterio.open(image_path) as src, open(pieces_file, 'w') as csvFile:
        writer = csv.writer(csvFile)
        writer.writerow([
            'original_image', 'piece_image', 'piece_geojson', 'start_x',
            'start_y', 'width', 'height'
        ])

        for j in tqdm(range(0, src.height // height)):
            for i in range(0, src.width // width):
                raster_window = src.read(window=Window(i * width, j *
                                                       height, width, height))
                image_array = reshape_as_image(raster_window)[:, :, [3, 0, 1]]

                if np.count_nonzero(image_array) > image_array.size * 0.9:
                    filename_w_ext = os.path.basename(image_path)
                    filename, _ = os.path.splitext(filename_w_ext)
                    image_format = 'png'
                    piece_name = f'{filename}_nrg_{j}_{i}.{image_format}'

                    imageio.imwrite(f'{save_path}/{piece_name}', image_array)

                    writer.writerow([
                        filename_w_ext, piece_name, 'piece_geojson_name',
                        i * width, j * height, width, height
                    ])

    csvFile.close()
Exemplo n.º 15
0
def predict_arr(patch, classifier):
    """
    Use a classifier to predict on an array


    params:
    ----------

    patch : nd numpy array

    classifier: optimized classifier


    return:
        2D numpy array of int with
        the same size as the window
    """
    patch = reshape_as_image(patch)
    np.nan_to_num(patch, copy=False, nan=0.0, posinf=0, neginf=0)
    # Generate a prediction array
    # new rows: rows * cols
    # new cols: bands
    class_prediction = classifier.predict(patch.reshape(-1, patch.shape[2]))
    # Reshape our classification map back into a 2D matrix so we can visualize it
    class_prediction = class_prediction.reshape(patch[:, :, 0].shape)

    return class_prediction
    def split_cloud(self, cloud_path, save_cloud_path, image_pieces_path):
        pieces_info = pd.read_csv(image_pieces_path,
                                  dtype={
                                      'start_x': np.int64,
                                      'start_y': np.int64,
                                      'width': np.int64,
                                      'height': np.int64
                                  })

        with rasterio.open(cloud_path) as cld:
            clouds = reshape_as_image(cld.read())
            clouds = cv2.resize(clouds, (self.image_width, self.image_height),
                                interpolation=cv2.INTER_CUBIC)
            # to [-1; 1]
            clouds = np.clip(clouds, 0, 100)
            clouds = (clouds / 100 * 2 - 1)
            clouds = img_as_ubyte(clouds)
            for i in range(pieces_info.shape[0]):
                piece = pieces_info.loc[i]
                piece_cloud = clouds[piece['start_y']:piece['start_y'] +
                                     piece['height'],
                                     piece['start_x']:piece['start_x'] +
                                     piece['width']]
                filename_cloud = '{}/{}.png'.format(
                    save_cloud_path,
                    re.split(r'[/.]', piece['piece_image'])[-2])
                imageio.imwrite(filename_cloud, piece_cloud)
Exemplo n.º 17
0
def visualize_image(img,
                    title: str = '',
                    filename: str = None,
                    rgb: Tuple[int, int, int] = (0, 1, 2),
                    figsplt: FigSplt = None,
                    stretch: bool = True,
                    show: bool = False,
                    as_image: bool = False):
    """Visualize the satellite image data."""
    assert len(rgb) == 3, 'Exactly three bands should be passed as rgb'
    assert img.dtype in (
        np.uint8, np.float32,
        np.float64), 'RGB images can be only certain types in matplotlib'

    if as_image:
        img = reshape_as_image(img)

    if stretch:
        img = hist_stretch_image(img)
    else:
        img = hist_stretch_image(img, clip_extremes=0)

    fig, splt = get_fig_splt(figsplt)

    splt.set_title(title)
    splt.imshow(img)

    if filename:
        fig.savefig(filename)

    if show:
        plt.show()

    return splt
Exemplo n.º 18
0
    def classify(self):
        def calculate_chunks(width, height, tiles):
            pixels = width * height
            max_pixels = pixels / tiles
            chunk_size = int(math.floor(math.sqrt(max_pixels)))
            ncols = int(math.ceil(width / chunk_size))
            nrows = int(math.ceil(height / chunk_size))
            chunk_windows = []

            for col in range(ncols):
                col_offset = col * chunk_size
                w = min(chunk_size, width - col_offset)
                for row in range(nrows):
                    row_offset = row * chunk_size
                    h = min(chunk_size, height - row_offset)
                    chunk_windows.append(
                        ((row, col), Window(col_offset, row_offset, w, h)))
            return chunk_windows

        with rasterio.open(self.rlayer.dataProvider().dataSourceUri()) as src:
            width = src.width
            height = src.height
            bands = src.count
            meta = src.meta
            dtype = src.dtypes

            self.signals.status.emit(strftime("%Y-%m-%d %H:%M:%S", gmtime()),
                                     "Predicting image values ... ")

            chunk_blocks = calculate_chunks(width, height, self.tiles)
            meta.update({"count": 1, "dtype": dtype[0]})

            with rasterio.open(self.outlayer, "w", **meta) as dst:
                counter = 1
                for idx, window in chunk_blocks:
                    self.signals.status.emit(
                        strftime("%Y-%m-%d %H:%M:%S",
                                 gmtime()), "Processing Block: " +
                        str(counter) + " of " + str(len(chunk_blocks)))
                    img = src.read(window=window)
                    dtype = rasterio.dtypes.get_minimum_dtype(img)
                    reshaped_img = reshape_as_image(img)
                    rows, cols, bands_n = reshaped_img.shape

                    class_prediction = self.classifier.predict(
                        reshaped_img.reshape(-1, bands))
                    classification = np.zeros((rows, cols, 1)).astype(dtype)
                    classification[:, :, 0] = class_prediction.reshape(
                        reshaped_img[:, :, 1].shape).astype(dtype)
                    final = reshape_as_raster(classification)
                    dst.write(final, window=window)
                    counter += 1

        seconds_elapsed = time() - self.starttime
        self.signals.status.emit(
            strftime("%Y-%m-%d %H:%M:%S", gmtime()),
            "Execution completed in " +
            str(np.around(seconds_elapsed, decimals=2)) + " seconds",
        )
        return self.outlayer
Exemplo n.º 19
0
def reader_nm():
    method = request.method
    INPUT = os.path.join(BASE_PATH, 'u.0p25.grib')
    if method == 'GET':
        if not request.args or 'dataName' not in request.args:
            abort(400)
        else:
            INPUT = os.path.join(BASE_PATH, request.args['dataName']
                                 or 'u.0p25.grib')
    elif method == 'POST':
        if not request.json or 'dataName' not in request.json:
            abort(400)
        else:
            INPUT = os.path.join(BASE_PATH, request.args['dataName']
                                 or 'u.0p25.grib')

    grib_header = {}

    bands = import_data(INPUT)

    grib_header["umin"] = bands[0, :, :].min()
    grib_header["umax"] = bands[0, :, :].max()
    grib_header["vmin"] = bands[1, :, :].min()
    grib_header["vmax"] = bands[1, :, :].max()

    bands = prepare_array(bands)

    image = reshape_as_image(bands)

    write_image(os.path.join(BASE_PATH, 'i.png'), image)

    return jsonify({'code': 200, 'msg': 'success', 'data': grib_header})
Exemplo n.º 20
0
def read_geotiff(geotiff_path):
    """Read geotiff, return reshaped image and metadata."""
    with rasterio.open(geotiff_path, 'r') as src:
        img = src.read()
        img_meta = src.meta

    return reshape_as_image(img), img_meta
Exemplo n.º 21
0
def load_data(stand_path, chip_size=None, offsets=None):
    """Loads NAIP, LANDSAT, and stand delineation data"""
    dirname, cell_id, state_name, year, agency = parse_stand_path(stand_path)
    naip_path = get_naip_path(dirname, cell_id, state_name, year)
    landsat_path = get_landsat_path(dirname, cell_id, state_name, year)

    with rasterio.open(naip_path) as src:
        profile = src.profile
        height, width = src.shape
        if chip_size is not None:
            if offsets is not None:
                row_off, col_off = offsets
            else:
                row_off = np.random.randint(0, height - chip_size)
                col_off = np.random.randint(0, width - chip_size)
            window = windows.Window(col_off, row_off, chip_size, chip_size)
        else:
            window = None

        naip = reshape_as_image(src.read(window=window))
        if window is not None:
            trf = src.window_transform(window)
            bbox = src.window_bounds(window)
        else:
            trf = src.transform
            bbox = src.bounds

    with rasterio.open(landsat_path) as src:
        if chip_size is not None:
            window = windows.from_bounds(*bbox,
                                         transform=src.transform,
                                         height=chip_size,
                                         width=chip_size)
        else:
            window = windows.from_bounds(*bbox,
                                         transform=src.transform,
                                         height=height,
                                         width=width)
        landsat = ((reshape_as_image(
            np.stack([src.read(band + 1, window=window)
                      for band in range(4)])) / 3000).clip(0, 1) * 255).astype(
                          np.uint8)

    stands = gpd.read_file(stand_path)
    stands = gpd.clip(stands, box(*bbox))

    return naip, landsat, profile, trf, stands
Exemplo n.º 22
0
def matplot_image(ax, image):
    # based on rasterio.plot, but accepts in-memory images
    rgb = extract_rgb(image)
    rgb = reshape_as_image(rgb)
    # https://stackoverflow.com/questions/24739769/matplotlib-imshow-plots-different-if-using-colormap-or-rgb-array
    lo, hi = np.min(rgb), np.max(rgb)
    rgb = ((rgb - lo) / (hi - lo)) ** (1 / 2.2)
    ax.imshow(rgb)
Exemplo n.º 23
0
def show_img(fpath, n):
    with rasterio.open(fpath) as src:
        arr = src.read([3, 2, 1], masked=True)
        print(type(arr))
        print(arr.shape)
        if arr.dtype == 'uint16':
            arr = (255 * (arr / np.max(arr))).astype(np.uint8)
        pyplot.figure(n)
        return pyplot.imshow(reshape_as_image(arr))
Exemplo n.º 24
0
def array_to_img(arr, tileformat='png', mask=None, color_map=None):
    """Convert an array to an base64 encoded img

    Attributes
    ----------
    arr : numpy ndarray
        Image array to encode.
    tileformat : str (default: png)
        Image format to return (Accepted: "jpg" or "png")
    Mask: numpy ndarray
        Mask
    color_map: numpy array
        ColorMap array (see: utils.get_colormap)

    Returns
    -------
    out : str
        base64 encoded PNG or JPEG image.
    """

    if tileformat not in ['png', 'jpg']:
        raise InvalidFormat('Invalid {} extension'.format(tileformat))

    if arr.dtype != np.uint8:
        logger.error('Data casted to UINT8')
        arr = arr.astype(np.uint8)

    if len(arr.shape) >= 3:
        arr = reshape_as_image(arr)
        arr = arr.squeeze()

    if len(arr.shape) != 2 and color_map:
        raise InvalidFormat('Cannot apply colormap on a multiband image')

    if len(arr.shape) == 2:
        mode = 'L'
    else:
        mode = 'RGB'

    img = Image.fromarray(arr, mode=mode)
    if color_map:
        img.putpalette(color_map)

    sio = BytesIO()
    if tileformat == 'jpg':
        tileformat = 'jpeg'
        params = {'subsampling': 0, 'quality': 100}
    else:
        if mask is not None:
            mask_img = Image.fromarray(mask.astype(np.uint8))
            img.putalpha(mask_img)
        params = {'compress_level': 0}

    img.save(sio, tileformat, **params)
    sio.seek(0)

    return base64.b64encode(sio.getvalue()).decode()
Exemplo n.º 25
0
    def read_image_around_latlon(self, img: str, lat: float, lon: float,
                                 size: float):
        t = rasterio.open(self.path / img)

        window = self.window_around_latlon(t, lat, lon, size)

        # TODO downsample image if the zoom level is too low / the image too large
        tw = reshape_as_image(t.read(window=window, boundless=True))
        return tw, window, t
Exemplo n.º 26
0
def clip_image(img_fp):
    with rasterio.open(img_fp) as src:

        img = src.read()[:, 400:1500, 1500:3000]

        # Take our full image and reshape into long 2d array (nrow * ncol, nband) for classification
        reshaped_img = reshape_as_image(img)

    return img, reshaped_img
Exemplo n.º 27
0
    def update(self, step, T, E, acceptance, improvement):
        """Print progress."""
        if acceptance is None:
            acceptance = 0
        if improvement is None:
            improvement = 0
        if step > 0:
            elapsed = time.time() - self.start
            remain = (self.steps - step) * (elapsed / step)
            # print('Time {}  ({} Remaing)'.format(time_string(elapsed), time_string(remain)))
        else:
            elapsed = 0
            remain = 0

        curr = self.cmd(self.state)
        curr_score = float(E)
        best = self.cmd(self.best_state)
        best_score = self.best_energy
        report = progress_report(
            curr,
            best,
            curr_score,
            best_score,
            step,
            self.steps,
            acceptance * 100,
            improvement * 100,
            time_string(elapsed),
            time_string(remain),
        )
        print(report)

        if fig:
            imgs[1].set_data(
                reshape_as_image(self.apply_color(self.src.copy(), self.state))
            )
            imgs[2].set_data(
                reshape_as_image(self.apply_color(self.src.copy(), self.best_state))
            )
            if txt:
                txt.set_text(report)
            fig.canvas.draw()
Exemplo n.º 28
0
 def __normalize_rgb(self, tile):
     tile = tile[[3, 2, 1], :, :].astype(np.float64)
     max_val = 4000
     min_val = 0
     # Enforce maximum and minimum values
     tile[tile[:, :, :] > max_val] = max_val
     tile[tile[:, :, :] < min_val] = min_val
     for b in range(tile.shape[0]):
         tile[b, :, :] = tile[b, :, :] * 1 / (max_val - min_val)
     tile_reshaped = reshape_as_image(tile)
     return tile_reshaped
Exemplo n.º 29
0
def gen_img_samples(rst_pth, tile_size, dist_samples, *band_order):
    with rasterio.open(rst_pth) as src:
        for row in range(0, src.height, dist_samples):
            for column in range(0, src.width, dist_samples):
                window = Window.from_slices(slice(row, row + tile_size),
                                            slice(column, column + tile_size))
                if band_order:
                    window_array = reshape_as_image(
                        src.read(band_order[0], window=window))
                else:
                    window_array = reshape_as_image(src.read(window=window))

                if window_array.shape[0] < tile_size or window_array.shape[
                        1] < tile_size:
                    padding = pad_diff(window_array.shape[0],
                                       window_array.shape[1], tile_size,
                                       tile_size)
                    window_array = pad(window_array, padding, fill=np.nan)

                yield window_array
def load_image_rasterio(img_path, parse_dltile_filename=True, decode=True):
    """Process a single image file, for any GDAL-compatible image type
    
    Parameters:
    img_path: the path to the GDAL-compatible raster image
    parse_dltile_filename: if True then the image filename will be assumed to be a DLTile key 
        with ':' replaced by '#'. This DLTile key will be returned as tile_key. If False then 
        tile_key will consist of '|'.join([filename, str(image_geotransform), str(img_crs)])
    decode: if True then the image data will be read and returned as an array. If False then the 
        raw file bytes will be returned.

    
    Returns: 
    5-tuple of (image_data, height, width, bands, tile_key)
    Where image_data and tile_key depend on chosen parameters as above.
    """
    # we use rasterio to parse the actual image data into an array
    # so that we can handle multi bands, different filetypes, dtypes
    # But we still use tf api to handle the actual file reading as it is
    # so much faster.
    img_arr = None
    with tf.io.gfile.GFile(img_path, 'rb') as f:
        image_data = f.read()
        with MemoryFile(image_data) as memfile:
            with memfile.open() as src:
                if decode:
                    img_arr = src.read()
                gt_str = str(src.get_transform())
                crs_str = str(src.read_crs())
                height = src.height
                width = src.width
                bands = src.count
    # as opposed to:
    #with rasterio.open(img_path) as src:
    #    img_arr = src.read() # reads all bands to 3d array bands,rows,cols
    #    gt_str = str(src.get_transform())
    #    crs_str = str(src.read_crs())

    if parse_dltile_filename:
        tile_key = '.'.join(os.path.basename(img_path).split(os.extsep)[:-1]).replace('#',':')
    else:
        if not (gt_str is None or crs_str is None):
            tile_key = '|'.join((os.path.basename(img_path), 
                                 gt_str, crs_str))
        else:
            tile_key = os.path.basename(img_path)
    if decode:
        img_arr = reshape_as_image(img_arr) #  converts dim order to rows,cols,bands
        # just in case we later extend to read a window/part of the dataset, put this trap 
        # in to make sure we don't trip over returning the wrong shape
        assert (height,width,bands)==img_arr.shape
        return img_arr, height, width, bands, tile_key
    else:
        return image_data, height, width, bands, tile_key
Exemplo n.º 31
0
def test_plotting_extent():
    from rasterio.plot import reshape_as_image
    expected = (101985.0, 339315.0, 2611485.0, 2826915.0)
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        assert plotting_extent(src) == expected
        assert plotting_extent(
            reshape_as_image(src.read()), transform=src.affine) == expected
        assert plotting_extent(
            src.read(1), transform=src.transform) == expected
        # array requires a transform
        with pytest.raises(ValueError):
            plotting_extent(src.read(1))
Exemplo n.º 32
0
def test_reshape():
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        im_data = plot.reshape_as_image(src.read())
        assert im_data.shape == (718, 791, 3)
Exemplo n.º 33
0
def test_roundtrip_reshape():
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        data = src.read()
        im_data = plot.reshape_as_image(data)
        assert np.array_equal(data, rasterio.plot.reshape_as_raster(im_data))