|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "id": "ec48b40a", |
6 | 6 | "metadata": {}, |
7 | | - "source": "# Extract layers for step 1\n\nEvery story in the Impact Sphere includes a Step 1 layer — a national or regional overview that provides geographic context for the story. This layer always covers the full country or region, even when the story itself focuses on a specific city or area within it (e.g., a story about Nairobi still uses a Kenya-wide Step 1 layer; a story about West Africa uses a regional mosaic).\n\nThis notebook extracts Step 1 layers from one of four global datasets:\n- **WorldCover** — ESA global land cover map (10 m)\n- **WSF Building Area** — DLR World Settlement Footprint 3D building area\n- **Global Surface Water (GSW)** — JRC surface water occurrence\n- **WorldCereal** — ESA global cropland classification\n\nMore sources may be added in the future.\n\n## Workflows\n\nEach data source section provides two workflows:\n- **Single country** — clip or download the dataset for one country, then style and process\n- **Multi-country region** — clip or download per country (each clipped to its GADM boundary), merge into a regional mosaic, then style and process" |
| 7 | + "source": [ |
| 8 | + "# Extract layers for step 1\n", |
| 9 | + "\n", |
| 10 | + "Every story in the Impact Sphere includes a Step 1 layer — a national or regional overview that provides geographic context for the story. This layer always covers the full country or region, even when the story itself focuses on a specific city or area within it (e.g., a story about Nairobi still uses a Kenya-wide Step 1 layer; a story about West Africa uses a regional mosaic).\n", |
| 11 | + "\n", |
| 12 | + "This notebook extracts Step 1 layers from one of four global datasets:\n", |
| 13 | + "- **WorldCover** — ESA global land cover map (10 m)\n", |
| 14 | + "- **WSF Building Area** — DLR World Settlement Footprint 3D building area\n", |
| 15 | + "- **Global Surface Water (GSW)** — JRC surface water occurrence\n", |
| 16 | + "- **WorldCereal** — ESA global cropland classification\n", |
| 17 | + "\n", |
| 18 | + "More sources may be added in the future.\n", |
| 19 | + "\n", |
| 20 | + "## Workflows\n", |
| 21 | + "\n", |
| 22 | + "Each data source section provides two workflows:\n", |
| 23 | + "- **Single country** — clip or download the dataset for one country, then style and process\n", |
| 24 | + "- **Multi-country region** — clip or download per country (each clipped to its GADM boundary), merge into a regional mosaic, then style and process" |
| 25 | + ] |
8 | 26 | }, |
9 | 27 | { |
10 | 28 | "cell_type": "markdown", |
|
22 | 40 | "id": "a25ef8df", |
23 | 41 | "metadata": {}, |
24 | 42 | "outputs": [], |
25 | | - "source": "import os\nimport sys\nfrom glob import glob\nfrom pathlib import Path\n\nimport rasterio as rio\nfrom rasterio.merge import merge\n\nsys.path.append(\"../src\")\n\nfrom data_processing.download_step1 import download_worldcover_for_country, download_gsw_for_country\nfrom data_processing.raster_processor import RasterProcessor\nfrom data_processing.utils import clip_raster_to_country_and_create_cog" |
| 43 | + "source": [ |
| 44 | + "import os\n", |
| 45 | + "import sys\n", |
| 46 | + "from glob import glob\n", |
| 47 | + "from pathlib import Path\n", |
| 48 | + "\n", |
| 49 | + "import rasterio as rio\n", |
| 50 | + "from rasterio.enums import Resampling\n", |
| 51 | + "from rasterio.merge import merge\n", |
| 52 | + "\n", |
| 53 | + "sys.path.append(\"../src\")\n", |
| 54 | + "\n", |
| 55 | + "from data_processing.download_step1 import download_worldcover_for_country, download_gsw_for_country\n", |
| 56 | + "from data_processing.raster_processor import RasterProcessor\n", |
| 57 | + "from data_processing.utils import clip_raster_to_country_and_create_cog, resample_raster" |
| 58 | + ] |
26 | 59 | }, |
27 | 60 | { |
28 | 61 | "cell_type": "markdown", |
|
525 | 558 | "id": "5d89ee8c", |
526 | 559 | "metadata": {}, |
527 | 560 | "outputs": [], |
528 | | - "source": [ |
529 | | - "# WORKING CELL — set country, run, then revert.\n", |
530 | | - "country = \"your_country\"\n", |
531 | | - "\n", |
532 | | - "# Clip raster to country and create COG\n", |
533 | | - "raster_file = \"../data/processed/WorldCereal/2021_tc-annual_temporarycrops_classification_0.004deg.tif\"\n", |
534 | | - "output_dir = \"../data/processed/WorldCereal/Clipped/\"\n", |
535 | | - "\n", |
536 | | - "cog_file = clip_raster_to_country_and_create_cog(raster_file, country, output_dir=output_dir)\n", |
537 | | - "print(f\"Created COG: {cog_file}\")" |
538 | | - ] |
| 561 | + "source": "# WORKING CELL — set country, run, then revert.\ncountry = \"your_country\"\n\n# Clip raster to country and create COG\nraster_file = \"../data/processed/WorldCereal/2021_tc-annual_temporarycrops_classification_0.004deg.tif\"\noutput_dir = \"../data/processed/WorldCereal/Clipped/\"\n\ncog_file = clip_raster_to_country_and_create_cog(raster_file, country, output_dir=output_dir)\nprint(f\"Created COG: {cog_file}\")" |
539 | 562 | }, |
540 | 563 | { |
541 | 564 | "cell_type": "code", |
|
545 | 568 | "outputs": [], |
546 | 569 | "source": [ |
547 | 570 | "# Style and process raster\n", |
| 571 | + "# For large countries (e.g. India), resample first to avoid Mapbox 500KB tile limit at low zoom.\n", |
| 572 | + "# Set resample_factor > 1 to downsample; 1 = no resampling.\n", |
| 573 | + "resample_factor = 1\n", |
| 574 | + "\n", |
548 | 575 | "print(f\"Processing WorldCereal for {country}...\")\n", |
549 | 576 | "BASE_PATH = Path(\"../data/processed/WorldCereal/\")\n", |
550 | 577 | "input_file = BASE_PATH / f\"Clipped/2021_tc-annual_temporarycrops_classification_0.004deg_{country.replace(' ', '_')}.tif\"\n", |
551 | 578 | "style_file = BASE_PATH / \"temporarycrops.qml\"\n", |
552 | 579 | "output_file = BASE_PATH / f\"Rasters/WorldCereal_{country.replace(' ', '_')}.tif\"\n", |
553 | 580 | "layer_name = f\"{country.replace(' ', '_')}_WorldCereal\"\n", |
554 | 581 | "\n", |
| 582 | + "if resample_factor > 1:\n", |
| 583 | + " resampled_file = input_file.parent / f\"{input_file.stem}_resampled{input_file.suffix}\"\n", |
| 584 | + " resample_raster(input_file, resampled_file, scale_factor=resample_factor, resampling_method=Resampling.nearest)\n", |
| 585 | + " input_file = resampled_file\n", |
| 586 | + " print(f\"Resampled by {resample_factor}x → {resampled_file}\")\n", |
| 587 | + "\n", |
555 | 588 | "RasterProcessor(\n", |
556 | 589 | " input_file,\n", |
557 | 590 | " output_file=output_file,\n", |
|
673 | 706 | ], |
674 | 707 | "metadata": { |
675 | 708 | "kernelspec": { |
676 | | - "display_name": "esa_gda_env", |
| 709 | + "display_name": "esa-gda", |
677 | 710 | "language": "python", |
678 | 711 | "name": "python3" |
679 | 712 | }, |
|
687 | 720 | "name": "python", |
688 | 721 | "nbconvert_exporter": "python", |
689 | 722 | "pygments_lexer": "ipython3", |
690 | | - "version": "3.9.23" |
| 723 | + "version": "3.13.11" |
691 | 724 | } |
692 | 725 | }, |
693 | 726 | "nbformat": 4, |
|
0 commit comments