|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import boto3 |
| 4 | +import geopandas as gpd |
| 5 | +import numpy |
| 6 | +import pandas as pd |
| 7 | +import rasterio |
| 8 | +from rasterio.windows import Window, bounds, transform |
| 9 | +from shapely import box |
| 10 | + |
| 11 | +wd = Path("~/Desktop/california").expanduser() |
| 12 | + |
| 13 | +# Download worldcover layers over california |
| 14 | +grid = gpd.read_file( |
| 15 | + "s3://clay-california-worldcover-rgbnir-vvvh-chips/esa_wordlcover_grid_california.fgb" |
| 16 | +) |
| 17 | +s3 = boto3.client("s3") |
| 18 | + |
| 19 | +for id, tile in grid.iterrows(): |
| 20 | + print( |
| 21 | + "esa-worldcover-s2", |
| 22 | + tile.s2_rgbnir_2021.split("s3://esa-worldcover-s2/")[1], |
| 23 | + f"{wd}/rgbnir/{tile.s2_rgbnir_2021.split('/')[-1]}", |
| 24 | + ) |
| 25 | + s3.download_file( |
| 26 | + "esa-worldcover-s2", |
| 27 | + tile.s2_rgbnir_2021.split("s3://esa-worldcover-s2/")[1], |
| 28 | + f"{wd}/rgbnir/{tile.s2_rgbnir_2021.split('/')[-1]}", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +for id, tile in grid.iterrows(): |
| 33 | + print( |
| 34 | + "esa-worldcover-s1", |
| 35 | + tile.s1_vvvhratio_2021.split("s3://esa-worldcover-s1/")[1], |
| 36 | + f"{wd}/vvvhratio/{tile.s1_vvvhratio_2021.split('/')[-1]}", |
| 37 | + ) |
| 38 | + s3.download_file( |
| 39 | + "esa-worldcover-s1", |
| 40 | + tile.s1_vvvhratio_2021.split("s3://esa-worldcover-s1/")[1], |
| 41 | + f"{wd}/vvvhratio/{tile.s1_vvvhratio_2021.split('/')[-1]}", |
| 42 | + ) |
| 43 | + |
| 44 | +# Merge all into very large files |
| 45 | +""" |
| 46 | +gdal_merge.py -co BIGTIFF=YES -co BLOCKXSIZE=256 -co BLOCKYSIZE=256 -co TILED=YES -co COMPRESS=DEFLATE -o ${wd}/california_rgbnir_worldcover.tif ${wd}/rgbnir/*.tif |
| 47 | +gdal_merge.py -co BIGTIFF=YES -co BLOCKXSIZE=256 -co BLOCKYSIZE=256 -co TILED=YES -co COMPRESS=DEFLATE -o ${wd}/california_vvvhratio_worldcover.tif ${wd}/vvvhratio/*.tif |
| 48 | +""" |
| 49 | + |
| 50 | +# Create chips from merged files |
| 51 | +RGB = wd / "california_rgbnir_worldcover.tif" |
| 52 | +VVVH = wd / "california_vvvhratio_worldcover.tif" |
| 53 | +TILE_SIZE = 256 |
| 54 | +NO_DATA = 0 |
| 55 | +count = 0 |
| 56 | +boxes = [] |
| 57 | +cols = [] |
| 58 | +rows = [] |
| 59 | +with rasterio.open(RGB) as rgbnir: |
| 60 | + meta = rgbnir.meta.copy() |
| 61 | + meta["count"] = 6 |
| 62 | + meta["width"] = TILE_SIZE |
| 63 | + meta["height"] = TILE_SIZE |
| 64 | + meta["compress"] = "deflate" |
| 65 | + with rasterio.open(VVVH) as vvvh: |
| 66 | + for i in range(0, rgbnir.width, TILE_SIZE): |
| 67 | + for j in range(0, rgbnir.height, TILE_SIZE): |
| 68 | + dst_path = wd / f"chips/worldcover_california_chip_{i}_{j}.tif" |
| 69 | + # if dst_path.exists(): |
| 70 | + # continue |
| 71 | + win = Window(i, j, TILE_SIZE, TILE_SIZE) |
| 72 | + meta["transform"] = transform(win, rgbnir.transform) |
| 73 | + rgbnir_chip = rgbnir.read([1, 2, 3, 4], window=win) |
| 74 | + if 0 in rgbnir_chip.shape: |
| 75 | + continue |
| 76 | + if rgbnir_chip.shape[1] != TILE_SIZE: |
| 77 | + continue |
| 78 | + if rgbnir_chip.shape[2] != TILE_SIZE: |
| 79 | + continue |
| 80 | + # Filter at 10% nodata |
| 81 | + if numpy.sum(rgbnir_chip[2] == NO_DATA) > (TILE_SIZE * TILE_SIZE) / 10: |
| 82 | + continue |
| 83 | + vvvh_chip = vvvh.read([1, 2], window=win) |
| 84 | + count += 1 |
| 85 | + data = numpy.vstack([rgbnir_chip, vvvh_chip]) |
| 86 | + with rasterio.open(dst_path, "w", **meta) as dst: |
| 87 | + dst.write(data) |
| 88 | + # Track bounding boxes |
| 89 | + boxes.append(box(*bounds(win, rgbnir.transform))) |
| 90 | + cols.append(i) |
| 91 | + rows.append(j) |
| 92 | + print(f"Done with column {i} / {rgbnir.width}") |
| 93 | + |
| 94 | + |
| 95 | +chips = gpd.GeoDataFrame( |
| 96 | + pd.DataFrame( |
| 97 | + { |
| 98 | + "col": cols, |
| 99 | + "row": rows, |
| 100 | + } |
| 101 | + ), |
| 102 | + crs="EPSG:4326", |
| 103 | + geometry=boxes, |
| 104 | +) |
| 105 | + |
| 106 | +chips.to_file( |
| 107 | + "s3://clay-california-worldcover-rgbnir-vvvh-chips/california-worldcover-chips.fgb" |
| 108 | +) |
| 109 | + |
| 110 | + |
| 111 | +# Upload chips to s3 |
| 112 | +""" |
| 113 | +s5cmd sync ${wd}/chips/ s3://clay-california-worldcover-rgbnir-vvvh-chips/chips/ |
| 114 | +""" |
0 commit comments