|
46 | 46 | "metadata": {},
|
47 | 47 | "outputs": [],
|
48 | 48 | "source": [
|
49 |
| - "%pip install stacchip==0.1.31" |
| 49 | + "%pip install stacchip==0.1.33" |
50 | 50 | ]
|
51 | 51 | },
|
52 | 52 | {
|
|
68 | 68 | "import numpy as np\n",
|
69 | 69 | "import pandas as pd\n",
|
70 | 70 | "import pystac_client\n",
|
71 |
| - "import rasterio\n", |
72 | 71 | "import shapely\n",
|
73 | 72 | "import torch\n",
|
74 | 73 | "import yaml\n",
|
75 | 74 | "from box import Box\n",
|
76 |
| - "from pyproj import Transformer\n", |
77 | 75 | "from stacchip.chipper import Chipper\n",
|
78 | 76 | "from stacchip.indexer import NoStatsChipIndexer\n",
|
79 | 77 | "from stacchip.processors.prechip import normalize_timestamp\n",
|
|
136 | 134 | "random.shuffle(items_list)"
|
137 | 135 | ]
|
138 | 136 | },
|
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 |
| - }, |
171 | 137 | {
|
172 | 138 | "cell_type": "code",
|
173 | 139 | "execution_count": null,
|
174 | 140 | "id": "ccb089e6-36d7-41a4-8297-f5517aa42065",
|
175 | 141 | "metadata": {},
|
176 | 142 | "outputs": [],
|
177 | 143 | "source": [
|
178 |
| - "chips = [] # List to hold chip metadata\n", |
179 | 144 | "chip_images = [] # List to hold chip pixels\n",
|
180 |
| - "chip_centroids = [] # List to hold chip centroids\n", |
181 | 145 | "chip_bounds = [] # List to hold chip bounds"
|
182 | 146 | ]
|
183 | 147 | },
|
|
194 | 158 | " # Index the chips in the item\n",
|
195 | 159 | " indexer = NoStatsChipIndexer(item)\n",
|
196 | 160 | "\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", |
201 | 161 | " # Instantiate the chipper\n",
|
202 | 162 | " chipper = Chipper(\n",
|
203 | 163 | " indexer, asset_blacklist=[\"thumbnail\", \"tilejson\", \"rendered_preview\"]\n",
|
|
206 | 166 | " # Get 5 randomly sampled chips from the total\n",
|
207 | 167 | " # number of chips within this item's entire image\n",
|
208 | 168 | " 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))" |
213 | 172 | ]
|
214 | 173 | },
|
215 | 174 | {
|
|
464 | 423 | "source": [
|
465 | 424 | "embeddings = []\n",
|
466 | 425 | "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", |
470 | 427 | " date = datetime.datetime.strptime(f\"{YEAR}-06-01\", \"%Y-%m-%d\")\n",
|
471 | 428 | " gsd = 0.6\n",
|
472 | 429 | "\n",
|
| 430 | + " lon, lat = chip_bounds[0].centroid.coords[0]\n", |
| 431 | + "\n", |
473 | 432 | " datacube = prep_datacube(\n",
|
474 | 433 | " np.array(tile), lat, lon, pd.to_datetime(f\"{YEAR}-06-01\"), gsd, model.device\n",
|
475 | 434 | " )\n",
|
|
482 | 441 | " \"embeddings\": [np.ascontiguousarray(embeddings_.squeeze())],\n",
|
483 | 442 | " \"image\": [np.ascontiguousarray(np.array(tile.transpose(1, 2, 0)).flatten())],\n",
|
484 | 443 | " }\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", |
488 | 444 | "\n",
|
489 | 445 | " # 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", |
491 | 447 | "\n",
|
492 | 448 | " outpath = f\"{outdir_embeddings}/{i}.gpq\"\n",
|
493 | 449 | " gdf.to_parquet(path=outpath, compression=\"ZSTD\", schema_version=\"1.0.0\")\n",
|
|
0 commit comments