Beispiel #1
0
def train_raw(raster_file, train_geo):
    training_vectors = gpd.read_file(train_geo)
    class_dict = crop_classes(train_geo)
    print(class_dict)
    # set up our training data lists
    # this larger cell reads data from a raster file for each training vector
    x_raw = []
    y_raw = []
    with rasterio.open(raster_file, 'r') as src:
        for (label, geom) in zip(training_vectors.name,
                                 training_vectors.geometry):
            # read the raster data matching the geometry bounds
            window = bounds_window(geom.bounds, src.transform)
            # store our window information
            window_affine = src.window_transform(window)
            fsrc = src.read(window=window)
            # rasterize the geometry into the larger shape and affine
            mask = rasterize([(geom, 1)],
                             out_shape=fsrc.shape[1:],
                             transform=window_affine,
                             fill=0,
                             dtype='uint8',
                             all_touched=True).astype(bool)
            # for each label pixel (places where the mask is true)...
            label_pixels = np.argwhere(mask)
            for (row, col) in label_pixels:
                # add a pixel of data to X
                data = fsrc[:, row, col]
                one_x = np.nan_to_num(data, nan=1e-3)
                x_raw.append(one_x)
                y_raw.append(class_dict[label])

    return x_raw, y_raw
def get_zonal_stat(geojson: Feature, raster: str) -> Tuple[float, float]:
    """Return zonal statistics."""
    geom = shape(geojson.geometry.dict())
    with rasterio.open(raster) as src:
        # read the raster data matching the geometry bounds
        window = bounds_window(geom.bounds, src.transform)
        # store our window information & read
        window_affine = src.window_transform(window)
        data = src.read(window=window)

        # calculate the coverage of pixels for weighting
        pctcover = rasterize_pctcover(geom, atrans=window_affine, shape=data.shape[1:])

        return (
            np.average(data[0], weights=pctcover),
            np.nanmedian(data),
        )
def test_bounds_window():
    with rasterio.open(raster) as src:
        assert bounds_window(src.bounds, src.transform) == \
            ((0, src.shape[0]), (0, src.shape[1]))
Beispiel #4
0
def test_bounds_window():
    with rasterio.open(raster) as src:
        assert bounds_window(src.bounds, src.transform) == \
            ((0, src.shape[0]), (0, src.shape[1]))