Skip to content

Commit

Permalink
Updated code to use stacchip 0.1.33, simplifying bbox calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowcap committed Jul 4, 2024
1 parent 0873539 commit b461a19
Showing 1 changed file with 8 additions and 52 deletions.
60 changes: 8 additions & 52 deletions nbs/v1-inference-simsearch-naip-stacchip.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install stacchip==0.1.31"
"%pip install stacchip==0.1.33"
]
},
{
Expand All @@ -68,12 +68,10 @@
"import numpy as np\n",
"import pandas as pd\n",
"import pystac_client\n",
"import rasterio\n",
"import shapely\n",
"import torch\n",
"import yaml\n",
"from box import Box\n",
"from pyproj import Transformer\n",
"from stacchip.chipper import Chipper\n",
"from stacchip.indexer import NoStatsChipIndexer\n",
"from stacchip.processors.prechip import normalize_timestamp\n",
Expand Down Expand Up @@ -136,48 +134,14 @@
"random.shuffle(items_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef3daea0-4e97-426a-ba9e-9f05ef0a37e5",
"metadata": {},
"outputs": [],
"source": [
"def get_bounds_centroid(url: str):\n",
" \"\"\"\n",
" Retrieve the bounds and centroid of an image from its URL.\n",
"\n",
" Parameters:\n",
" url (str): The URL of the image.\n",
"\n",
" Returns:\n",
" tuple: Bounds coordinates and centroid coordinates.\n",
" \"\"\"\n",
" with rasterio.open(url) as rst:\n",
" bounds = rst.bounds\n",
" transformer = Transformer.from_crs(rst.crs, 4326)\n",
"\n",
" centroid_x = (bounds.left + bounds.right) / 2\n",
" centroid_y = (bounds.top + bounds.bottom) / 2\n",
"\n",
" centroid_x, centroid_y = transformer.transform(centroid_x, centroid_y)\n",
"\n",
" bounds_b, bounds_l = transformer.transform(bounds.left, bounds.bottom)\n",
" bounds_t, bounds_r = transformer.transform(bounds.right, bounds.top)\n",
"\n",
" return [bounds_b, bounds_l, bounds_t, bounds_r], [centroid_x, centroid_y]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ccb089e6-36d7-41a4-8297-f5517aa42065",
"metadata": {},
"outputs": [],
"source": [
"chips = [] # List to hold chip metadata\n",
"chip_images = [] # List to hold chip pixels\n",
"chip_centroids = [] # List to hold chip centroids\n",
"chip_bounds = [] # List to hold chip bounds"
]
},
Expand All @@ -194,10 +158,6 @@
" # Index the chips in the item\n",
" indexer = NoStatsChipIndexer(item)\n",
"\n",
" # Obtain the item bounds and centroid\n",
" bounds, centroid = get_bounds_centroid(item.assets[\"image\"].href)\n",
" print(f\"Bounds coordinates: {bounds}, centroid coordinates: {centroid}\")\n",
"\n",
" # Instantiate the chipper\n",
" chipper = Chipper(\n",
" indexer, asset_blacklist=[\"thumbnail\", \"tilejson\", \"rendered_preview\"]\n",
Expand All @@ -206,10 +166,9 @@
" # Get 5 randomly sampled chips from the total\n",
" # number of chips within this item's entire image\n",
" for chip_id in random.sample(range(0, len(chipper)), 25):\n",
" chips.append(chipper[chip_id])\n",
" chip_images.append(chipper[chip_id][\"image\"])\n",
" chip_bounds.append(bounds)\n",
" chip_centroids.append(centroid)"
" x_index, y_index, chip = chipper[chip_id]\n",
" chip_images.append(chip[\"image\"])\n",
" chip_bounds.append(indexer.get_chip_bbox(x_index, y_index))"
]
},
{
Expand Down Expand Up @@ -464,12 +423,12 @@
"source": [
"embeddings = []\n",
"i = 0\n",
"for tile, centroid, box in zip(chip_images, chip_centroids, chip_bounds):\n",
" lon, lat = centroid[0], centroid[1]\n",
"\n",
"for tile, box in zip(chip_images, chip_bounds):\n",
" date = datetime.datetime.strptime(f\"{YEAR}-06-01\", \"%Y-%m-%d\")\n",
" gsd = 0.6\n",
"\n",
" lon, lat = chip_bounds[0].centroid.coords[0]\n",
"\n",
" datacube = prep_datacube(\n",
" np.array(tile), lat, lon, pd.to_datetime(f\"{YEAR}-06-01\"), gsd, model.device\n",
" )\n",
Expand All @@ -482,12 +441,9 @@
" \"embeddings\": [np.ascontiguousarray(embeddings_.squeeze())],\n",
" \"image\": [np.ascontiguousarray(np.array(tile.transpose(1, 2, 0)).flatten())],\n",
" }\n",
" # Define the bounding box as a Polygon (xmin, ymin, xmax, ymax)\n",
" box_emb = shapely.geometry.box(box[0], box[1], box[2], box[3])\n",
" print(box_emb)\n",
"\n",
" # Create the GeoDataFrame\n",
" gdf = gpd.GeoDataFrame(data, geometry=[box_emb], crs=\"EPSG:4326\")\n",
" gdf = gpd.GeoDataFrame(data, geometry=[box], crs=\"EPSG:4326\")\n",
"\n",
" outpath = f\"{outdir_embeddings}/{i}.gpq\"\n",
" gdf.to_parquet(path=outpath, compression=\"ZSTD\", schema_version=\"1.0.0\")\n",
Expand Down

0 comments on commit b461a19

Please sign in to comment.