Exemplo n.º 1
0
def create_mosaic(imgs_list:list,reproject=False,target_epsg:str='EPSG:4326',):
    '''
    With this function, you can create mosaic image from images list. You can give image path or xarray object read data before use this function.
    If you define reproject=True, system automatically convert all images into epsg:4326 and mosaic them.
    If you don't define reproject behaviour, system will controll your epsg code according to first image of your list.
    If the next images do not match the base image, they convert to hte base image epsg. Be careful when you work with UTM system

    imgs_list= ['target_path3/S2A_MSIL2A_20181030T081041_N0209_R078_T37SDA_20181030T091317/B02_subset.tif',
    'target_path3/S2A_MSIL2A_20181030T081041_N0209_R078_T37SDA_20181030T091317/B03_subset.tif',
    'target_path3/S2A_MSIL2A_20181030T081041_N0209_R078_T37SDA_20181030T091317/B04_subset.tif',]
    '''
    if type(imgs_list[0])==str:
            imgs_list=[open_image(img) for img in imgs_list]
    if reproject:
        reprojected_list=[]
        for img in imgs_list:
            xds_lonlat = img.rio.reproject("EPSG:4326")
            reprojected_list.append(xds_lonlat)
            # 0 olan yani nodata lar birlestirirken ne yapiyor kontrol etmek gerekiyor
        merged = merge_arrays(reprojected_list)
        return merged

    base_img=imgs_list[0]
    for i in range(len(imgs_list)-1):
        if base_img.rio.crs!=imgs_list[i+1].rio.crs:
            reproject_img=imgs_list[i+1]
            reproject_img=reproject_img.rio.reproject_match(base_img)
            imgs_list[i+1]=reproject_img
    
    merged = merge_arrays(imgs_list)
    return merged
Exemplo n.º 2
0
def test_merge_arrays__res():
    dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")
    with open_rasterio(dem_test, masked=True) as rds:
        rds.attrs = {
            "_FillValue": rds.rio.nodata,
            "grid_mapping": "spatial_ref",
            "crs": rds.attrs["crs"],
        }
        arrays = [
            rds.isel(x=slice(100), y=slice(100)),
            rds.isel(x=slice(100, 200), y=slice(100, 200)),
            rds.isel(x=slice(100), y=slice(100, 200)),
            rds.isel(x=slice(100, 200), y=slice(100)),
        ]
        merged = merge_arrays(arrays, res=(300, 300))

    assert_almost_equal(
        merged.rio.bounds(),
        (-7274009.649486291, 5003608.61015275, -7227509.649486291,
         5050108.61015275),
    )
    assert_almost_equal(
        tuple(merged.rio.transform()),
        (300.0, 0.0, -7274009.649486291, 0.0, -300.0, 5050108.61015275, 0.0,
         0.0, 1.0),
    )
    assert_almost_equal(merged.attrs.pop("transform"),
                        tuple(merged.rio.transform())[:6])
    assert merged.rio.shape == (155, 155)
    assert merged.coords["band"].values == [1]
    assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
    assert merged.rio.crs == rds.rio.crs
    assert merged.attrs == rds.attrs
    assert_almost_equal(nansum(merged), 13556564)
Exemplo n.º 3
0
def test_merge_arrays__res():
    dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")
    with open_rasterio(dem_test, masked=True) as rds:
        rds.attrs = {
            "_FillValue": rds.rio.nodata,
            "crs": rds.attrs["crs"],
        }
        arrays = [
            rds.isel(x=slice(100), y=slice(100)),
            rds.isel(x=slice(100, 200), y=slice(100, 200)),
            rds.isel(x=slice(100), y=slice(100, 200)),
            rds.isel(x=slice(100, 200), y=slice(100)),
        ]
        merged = merge_arrays(arrays, res=(300, 300))

    assert_almost_equal(
        merged.rio.bounds(),
        (-7274009.6494863, 5003308.6101528, -7227209.6494863, 5050108.6101528),
    )
    assert_almost_equal(
        tuple(merged.rio.transform()),
        (300.0, 0.0, -7274009.649486291, 0.0, -300.0, 5050108.61015275, 0.0, 0.0, 1.0),
    )
    assert merged.rio._cached_transform() == merged.rio.transform()
    assert merged.rio.shape == (156, 156)
    assert merged.coords["band"].values == [1]
    assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
    assert merged.rio.crs == rds.rio.crs
    assert_almost_equal(merged.attrs.pop("_FillValue"), rds.attrs.pop("_FillValue"))
    compare_attrs = dict(rds.attrs)
    compare_attrs.pop("crs")
    assert merged.attrs == compare_attrs
    assert merged.encoding["grid_mapping"] == "spatial_ref"
    assert_almost_equal(nansum(merged), 13760565)
Exemplo n.º 4
0
def test_merge_arrays(squeeze):
    dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")
    with open_rasterio(dem_test) as rds:
        rds.attrs = {
            "_FillValue": rds.rio.nodata,
            "crs": rds.attrs["crs"],
        }
        arrays = [
            rds.isel(x=slice(100), y=slice(100)),
            rds.isel(x=slice(100, 200), y=slice(100, 200)),
            rds.isel(x=slice(100), y=slice(100, 200)),
            rds.isel(x=slice(100, 200), y=slice(100)),
        ]
        if squeeze:
            arrays = [array.squeeze() for array in arrays]
        merged = merge_arrays(arrays)

    if RASTERIO_GE_125:
        assert_almost_equal(
            merged.rio.bounds(),
            (-7274009.6494863, 5003777.3385, -7227678.3778335,
             5050108.6101528),
        )
        assert merged.rio.shape == (200, 200)
        assert_almost_equal(merged.sum(), 22865733)

    else:
        assert_almost_equal(
            merged.rio.bounds(),
            (
                -7274009.649486291,
                5003545.682141737,
                -7227446.721475236,
                5050108.61015275,
            ),
        )
        assert merged.rio.shape == (201, 201)
        assert_almost_equal(merged.sum(), 11368261)

    assert_almost_equal(
        tuple(merged.rio.transform()),
        (
            231.6563582639536,
            0.0,
            -7274009.649486291,
            0.0,
            -231.65635826374404,
            5050108.61015275,
            0.0,
            0.0,
            1.0,
        ),
    )
    assert merged.rio._cached_transform() == merged.rio.transform()
    assert merged.coords["band"].values == [1]
    assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
    assert merged.rio.crs == rds.rio.crs
    assert merged.attrs == rds.attrs
    assert merged.encoding["grid_mapping"] == "spatial_ref"
Exemplo n.º 5
0
def merge_tiffs(all_image_files):
    # all_image_files = glob.glob(os.path.join(dir_path, "*.TIF"))
    elements = []
    
    for im in all_image_files:
        elements.append(rioxarray.open_rasterio(im))
    
    return merge.merge_arrays(elements, nodata=0.0)
Exemplo n.º 6
0
def create_mosaic(list_of_files, reporject=None):
    dem = []
    for fp in list_of_files:
        print(fp)
        dem.append(
            rioxarray.open_rasterio(fp, masked=True, resampling="average")
        )
    # Merge files
    dem_mosaic = merge.merge_arrays(dem)
    if reporject is not None:
        dem_mosaic_wgs84 = xr_reproject(dem_mosaic)
        return dem_mosaic_wgs84
    return dem_mosaic
Exemplo n.º 7
0
def merge_data():
    lc_fd = r'/share/home/liujunzhi/liujunzhi/Albert/landcover/landcoverData'
    lc_fnList = os.listdir(lc_fd)
    raster_list = []
    for fn in lc_fnList:
        fp = os.path.join(lc_fd, fn)
        da = rioxr.open_rasterio(fp)
        raster_list.append(da)
    print("Data loaded!")
    merged = merge_arrays(raster_list)
    merged_albers = merged.rio.reproject('ESRI:102025')
    # print("投影转换完成!")
    output_fp = r'/share/home/liujunzhi/liujunzhi/Albert/landcover/TempData/mergedLandcoverData_extent.tif'
    merged_albers.rio.to_raster(output_fp, compress='LZW')
Exemplo n.º 8
0
def test_merge__different_crs(dataset):
    dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")
    with open_rasterio(dem_test) as rds:
        crs = rds.rio.crs
        if dataset:
            rds = rds.to_dataset()
        arrays = [
            rds.isel(x=slice(100), y=slice(100)).rio.reproject("EPSG:3857"),
            rds.isel(x=slice(100, 200), y=slice(100, 200)),
            rds.isel(x=slice(100), y=slice(100, 200)),
            rds.isel(x=slice(100, 200), y=slice(100)),
        ]
        if dataset:
            merged = merge_datasets(arrays, crs=crs)
        else:
            merged = merge_arrays(arrays, crs=crs)
    assert_almost_equal(
        merged.rio.bounds(),
        (-7300984.0238134, 5003618.5908794, -7223500.6583578, 5050108.6101528),
    )
    assert_almost_equal(
        tuple(merged.rio.transform()),
        (
            553.4526103969893,
            0.0,
            -7300984.023813409,
            0.0,
            -553.4526103969796,
            5050108.610152751,
            0.0,
            0.0,
            1.0,
        ),
    )
    assert merged.rio.shape == (84, 140)
    assert merged.coords["band"].values == [1]
    assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
    assert merged.rio.crs == rds.rio.crs
    if dataset:
        assert merged.attrs == {"grid_mapping": "spatial_ref"}
        assert_almost_equal(merged[merged.rio.vars[0]].sum(), -131013894)
    else:
        assert merged.attrs == {
            "_FillValue": -28672,
            "add_offset": 0.0,
            "grid_mapping": "spatial_ref",
            "scale_factor": 1.0,
        }
        assert_almost_equal(merged.sum(), -131013894)
Exemplo n.º 9
0
def test_merge_arrays():
    dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")
    with open_rasterio(dem_test) as rds:
        rds.attrs = {
            "_FillValue": rds.rio.nodata,
            "grid_mapping": "spatial_ref",
            "crs": rds.attrs["crs"],
        }
        arrays = [
            rds.isel(x=slice(100), y=slice(100)),
            rds.isel(x=slice(100, 200), y=slice(100, 200)),
            rds.isel(x=slice(100), y=slice(100, 200)),
            rds.isel(x=slice(100, 200), y=slice(100)),
        ]
        merged = merge_arrays(arrays)

    assert_almost_equal(
        merged.rio.bounds(),
        (-7274009.649486291, 5003545.682141737, -7227446.721475236,
         5050108.61015275),
    )
    assert_almost_equal(
        tuple(merged.rio.transform()),
        (
            231.6563582639536,
            0.0,
            -7274009.649486291,
            0.0,
            -231.65635826374404,
            5050108.61015275,
            0.0,
            0.0,
            1.0,
        ),
    )
    assert_almost_equal(merged.attrs.pop("transform"),
                        tuple(merged.rio.transform())[:6])
    assert merged.rio.shape == (201, 201)
    assert merged.coords["band"].values == [1]
    assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"]
    assert merged.rio.crs == rds.rio.crs
    assert merged.attrs == rds.attrs
    assert_almost_equal(merged.sum(), 11368261)
Exemplo n.º 10
0
                ds_masked = ds.where(mask)

                hsv = _hsv(ds_masked.B11, ds_masked.B8A, ds_masked.B04)

                if alg == 'EVI':
                    evi = _evi(ds_masked.B02, ds_masked.B04, ds_masked.B8A)
                    combined = [evi, hsv.H]
                elif alg == 'NDVI':
                    ndvi = _ndvi(ds_masked.B04, ds_masked.B8A)
                    combined = [ndvi, hsv.H]

                gvi_ = _gvi(combined, **{'limits': limits})

                tiles.append(gvi_)

            sng_date_utm_strip = merge_arrays(tiles, method='max', nodata=np.NAN)

            sng_date_reproj = sng_date_utm_strip.rio.reproject('EPSG:4326', )

            dates_da.append(sng_date_reproj)

    # time_agg = xr.combine_nested(dates_da, concat_dim=['time'])
    time_agg = xr.combine_nested(dates_da, concat_dim=[['time', 'x', 'y']])

    GVDM = xr.apply_ufunc(_decades, time_agg,
                          input_core_dims=[['time']],
                          exclude_dims={'time', },
                          dask='parallelized',
                          dask_gufunc_kwargs={'allow_rechunk': True},
                          vectorize=True,)
    GVDM.name = 'GVI'
Exemplo n.º 11
0
plt.rcParams["axes.titleweight"] = "semibold"

# read the digital terrain model
# OS Terrain 50
rasters = [
    "data/Terrain50/sn91_OST50GRID_20210507/SN91.asc",
    "data/Terrain50/sn92_OST50GRID_20210507/SN92.asc",
    "data/Terrain50/so01_OST50GRID_20210507/SO01.asc",
    "data/Terrain50/so02_OST50GRID_20210507/SO02.asc"
]
arrays = []

for ras in rasters:
    arrays.append(rxr.open_rasterio(ras, masked=True))

dtm = merge_arrays(arrays)

# view the DTM
dtm

# plot the DTM
dtm.squeeze().plot.imshow(
    cmap="terrain",
    cbar_kwargs={"label": "Elevation (m)"},
    vmax=dtm.max(),
    vmin=dtm.min(),
    figsize=(9, 9)
)

plt.title("50 m Digital Terrain Model of the Brecon Beacons")
plt.text(