Skip to content

Commit b461a19

Browse files
committed
Updated code to use stacchip 0.1.33, simplifying bbox calculations
1 parent 0873539 commit b461a19

File tree

1 file changed

+8
-52
lines changed

1 file changed

+8
-52
lines changed

nbs/v1-inference-simsearch-naip-stacchip.ipynb

+8-52
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"metadata": {},
4747
"outputs": [],
4848
"source": [
49-
"%pip install stacchip==0.1.31"
49+
"%pip install stacchip==0.1.33"
5050
]
5151
},
5252
{
@@ -68,12 +68,10 @@
6868
"import numpy as np\n",
6969
"import pandas as pd\n",
7070
"import pystac_client\n",
71-
"import rasterio\n",
7271
"import shapely\n",
7372
"import torch\n",
7473
"import yaml\n",
7574
"from box import Box\n",
76-
"from pyproj import Transformer\n",
7775
"from stacchip.chipper import Chipper\n",
7876
"from stacchip.indexer import NoStatsChipIndexer\n",
7977
"from stacchip.processors.prechip import normalize_timestamp\n",
@@ -136,48 +134,14 @@
136134
"random.shuffle(items_list)"
137135
]
138136
},
139-
{
140-
"cell_type": "code",
141-
"execution_count": null,
142-
"id": "ef3daea0-4e97-426a-ba9e-9f05ef0a37e5",
143-
"metadata": {},
144-
"outputs": [],
145-
"source": [
146-
"def get_bounds_centroid(url: str):\n",
147-
" \"\"\"\n",
148-
" Retrieve the bounds and centroid of an image from its URL.\n",
149-
"\n",
150-
" Parameters:\n",
151-
" url (str): The URL of the image.\n",
152-
"\n",
153-
" Returns:\n",
154-
" tuple: Bounds coordinates and centroid coordinates.\n",
155-
" \"\"\"\n",
156-
" with rasterio.open(url) as rst:\n",
157-
" bounds = rst.bounds\n",
158-
" transformer = Transformer.from_crs(rst.crs, 4326)\n",
159-
"\n",
160-
" centroid_x = (bounds.left + bounds.right) / 2\n",
161-
" centroid_y = (bounds.top + bounds.bottom) / 2\n",
162-
"\n",
163-
" centroid_x, centroid_y = transformer.transform(centroid_x, centroid_y)\n",
164-
"\n",
165-
" bounds_b, bounds_l = transformer.transform(bounds.left, bounds.bottom)\n",
166-
" bounds_t, bounds_r = transformer.transform(bounds.right, bounds.top)\n",
167-
"\n",
168-
" return [bounds_b, bounds_l, bounds_t, bounds_r], [centroid_x, centroid_y]"
169-
]
170-
},
171137
{
172138
"cell_type": "code",
173139
"execution_count": null,
174140
"id": "ccb089e6-36d7-41a4-8297-f5517aa42065",
175141
"metadata": {},
176142
"outputs": [],
177143
"source": [
178-
"chips = [] # List to hold chip metadata\n",
179144
"chip_images = [] # List to hold chip pixels\n",
180-
"chip_centroids = [] # List to hold chip centroids\n",
181145
"chip_bounds = [] # List to hold chip bounds"
182146
]
183147
},
@@ -194,10 +158,6 @@
194158
" # Index the chips in the item\n",
195159
" indexer = NoStatsChipIndexer(item)\n",
196160
"\n",
197-
" # Obtain the item bounds and centroid\n",
198-
" bounds, centroid = get_bounds_centroid(item.assets[\"image\"].href)\n",
199-
" print(f\"Bounds coordinates: {bounds}, centroid coordinates: {centroid}\")\n",
200-
"\n",
201161
" # Instantiate the chipper\n",
202162
" chipper = Chipper(\n",
203163
" indexer, asset_blacklist=[\"thumbnail\", \"tilejson\", \"rendered_preview\"]\n",
@@ -206,10 +166,9 @@
206166
" # Get 5 randomly sampled chips from the total\n",
207167
" # number of chips within this item's entire image\n",
208168
" for chip_id in random.sample(range(0, len(chipper)), 25):\n",
209-
" chips.append(chipper[chip_id])\n",
210-
" chip_images.append(chipper[chip_id][\"image\"])\n",
211-
" chip_bounds.append(bounds)\n",
212-
" chip_centroids.append(centroid)"
169+
" x_index, y_index, chip = chipper[chip_id]\n",
170+
" chip_images.append(chip[\"image\"])\n",
171+
" chip_bounds.append(indexer.get_chip_bbox(x_index, y_index))"
213172
]
214173
},
215174
{
@@ -464,12 +423,12 @@
464423
"source": [
465424
"embeddings = []\n",
466425
"i = 0\n",
467-
"for tile, centroid, box in zip(chip_images, chip_centroids, chip_bounds):\n",
468-
" lon, lat = centroid[0], centroid[1]\n",
469-
"\n",
426+
"for tile, box in zip(chip_images, chip_bounds):\n",
470427
" date = datetime.datetime.strptime(f\"{YEAR}-06-01\", \"%Y-%m-%d\")\n",
471428
" gsd = 0.6\n",
472429
"\n",
430+
" lon, lat = chip_bounds[0].centroid.coords[0]\n",
431+
"\n",
473432
" datacube = prep_datacube(\n",
474433
" np.array(tile), lat, lon, pd.to_datetime(f\"{YEAR}-06-01\"), gsd, model.device\n",
475434
" )\n",
@@ -482,12 +441,9 @@
482441
" \"embeddings\": [np.ascontiguousarray(embeddings_.squeeze())],\n",
483442
" \"image\": [np.ascontiguousarray(np.array(tile.transpose(1, 2, 0)).flatten())],\n",
484443
" }\n",
485-
" # Define the bounding box as a Polygon (xmin, ymin, xmax, ymax)\n",
486-
" box_emb = shapely.geometry.box(box[0], box[1], box[2], box[3])\n",
487-
" print(box_emb)\n",
488444
"\n",
489445
" # Create the GeoDataFrame\n",
490-
" gdf = gpd.GeoDataFrame(data, geometry=[box_emb], crs=\"EPSG:4326\")\n",
446+
" gdf = gpd.GeoDataFrame(data, geometry=[box], crs=\"EPSG:4326\")\n",
491447
"\n",
492448
" outpath = f\"{outdir_embeddings}/{i}.gpq\"\n",
493449
" gdf.to_parquet(path=outpath, compression=\"ZSTD\", schema_version=\"1.0.0\")\n",

0 commit comments

Comments
 (0)