Skip to content

Commit 4069a02

Browse files
authored
Fix typos, unused imports, unused variables, indention on notebook scripts (#2519)
1 parent 092ca9d commit 4069a02

2 files changed

Lines changed: 40 additions & 44 deletions

File tree

notebooks/generate_offline_imagery1.ipynb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
{
6060
"cell_type": "code",
6161
"source": [
62-
"COUNTRY_NAME=input('Name of country to export, exactly as it appears in FAO GAUL 2015: ')\n",
62+
"COUNTRY_NAME = input('Name of country to export, exactly as it appears in FAO GAUL 2015: ')\n",
6363
"\n",
64-
"DEST_BUCKET=input('Bucket ID where GeoTIFFs should be stored: ')\n",
64+
"DEST_BUCKET = input('Bucket ID where GeoTIFFs should be stored: ')\n",
6565
"\n",
66-
"CLOUD_PROJECT=input('Cloud project for Earth Engine execution: ')"
66+
"CLOUD_PROJECT = input('Cloud project for Earth Engine execution: ')"
6767
],
6868
"metadata": {
6969
"id": "iA2QLfo1_jzU"
@@ -126,7 +126,7 @@
126126
"# Visualization params:\n",
127127
"S2_VIS_PARAMS = {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000, 'gamma': 1.2}\n",
128128
"HILLSHADE_VIS_PARAMS = {'min': 0, 'max': 160}\n",
129-
"# Ooacity ranges for low and high illumination regions.\n",
129+
"# Opacity ranges for low and high illumination regions.\n",
130130
"LO_MIN = 0\n",
131131
"LO_MAX = 170\n",
132132
"HI_MIN = 210\n",
@@ -411,7 +411,7 @@
411411
" dark_pixels = img.select('B8').lt(NIR_DRK_THRESH*SR_BAND_SCALE).multiply(not_water).rename('dark_pixels')\n",
412412
"\n",
413413
" # Determine the direction to project cloud shadow from clouds (assumes UTM projection).\n",
414-
" shadow_azimuth = ee.Number(90).subtract(ee.Number(img.get('MEAN_SOLAR_AZIMUTH_ANGLE')));\n",
414+
" shadow_azimuth = ee.Number(90).subtract(ee.Number(img.get('MEAN_SOLAR_AZIMUTH_ANGLE')))\n",
415415
"\n",
416416
" # Project shadows from clouds for the distance specified by the CLD_PRJ_DIST input.\n",
417417
" cld_proj = (img.select('clouds').directionalDistanceTransform(shadow_azimuth, CLD_PRJ_DIST*10)\n",
@@ -470,7 +470,7 @@
470470
" lo = ee.Image(1).subtract(lo_scaled).selfMask().visualize(**LO_VIS)\n",
471471
"\n",
472472
" hi_clamped = hillshade.clamp(HI_MIN, HI_MAX)\n",
473-
" hi_scaled = hi_clamped.unitScale(HI_OPAC_MIN , HI_OPAC_MAX)\n",
473+
" hi_scaled = hi_clamped.unitScale(HI_OPAC_MIN, HI_OPAC_MAX)\n",
474474
" hi = hi_scaled.selfMask().visualize(**HI_VIS)\n",
475475
"\n",
476476
" return img.blend(lo.blend(hi))\n",
@@ -485,7 +485,7 @@
485485
"map.add_ee_image(image.clip(aoi), {}, 'Image export preview', show=True, opacity=1, min_zoom=9)\n",
486486
"map.fit_bounds([[aoi_bounds.south, aoi_bounds.west], [aoi_bounds.north, aoi_bounds.east]])\n",
487487
"\n",
488-
"print(\"Visualizing preview - this may take awhile. You may proceed with other steps without waiting.\")\n",
488+
"print(\"Visualizing preview - this may take a while. You may proceed with other steps without waiting.\")\n",
489489
"print()\n",
490490
"display(map)"
491491
],
@@ -518,13 +518,13 @@
518518
"source": [
519519
"from unidecode import unidecode\n",
520520
"\n",
521-
"def build_export_task(image, extent, max_zoom, file_name_prefix, dimensions):\n",
521+
"def build_export_task(image, extent, file_name_prefix, dimensions):\n",
522522
" x, y, z = extent\n",
523523
" country = unidecode(COUNTRY_NAME).replace(\"'\", \"\").replace(\" \", \"-\")\n",
524524
" return {\n",
525525
" 'fileFormat': 'GeoTIFF',\n",
526526
" 'image': image,\n",
527-
" # Note: Region is specified in the source image's CRS, not the target one.\n",
527+
" # Note: Region is specified in the source image's CRS, not the target one.\n",
528528
" 'region': tile_to_ee_bbox(extent),\n",
529529
" 'description': f\"Export-{country}-{z}-{x}-{y}\",\n",
530530
" 'bucket': DEST_BUCKET,\n",
@@ -540,7 +540,6 @@
540540
"tasks = [build_export_task(\n",
541541
" image=image,\n",
542542
" extent=extent,\n",
543-
" max_zoom=HI_RES_MAX_ZOOM,\n",
544543
" file_name_prefix=f\"{DEST_PATH}/{extent.z}/{extent.x}/{extent.y}\",\n",
545544
" dimensions=hi_res_dim)\n",
546545
" for extent in aoi_tileset_extents]\n",
@@ -576,7 +575,7 @@
576575
"\n",
577576
"# https://google-auth-oauthlib.readthedocs.io/en/latest/reference/google_auth_oauthlib.flow.html\n",
578577
"# Export missing tilesets.\n",
579-
"print(\"Starting tasks. See running tasks at https://code.earthengine.google.com/tasks\")\n",
578+
"print(f\"Starting tasks. See running tasks at https://code.earthengine.google.com/tasks?project={CLOUD_PROJECT}\")\n",
580579
"for i, t in enumerate(tasks):\n",
581580
" name = t['fileNamePrefix'] + '.tif'\n",
582581
" blob = storage.Blob(name, bucket)\n",

notebooks/generate_offline_imagery2.ipynb

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
"\n",
3434
"This notebook converts normal GeoTIFFs into cloud optimized-GeoTIFFs (COGs) compatible with Google Maps Platform APIs and the Ground data collection platform Android app.\n",
3535
"\n",
36-
"We've affectionally dubbed these Maps-optimized COGs as \"MOGs\" for convenience and brevity. See [README.md](https://github.com/google/ground-android/blob/master/ground/src/main/java/com/google/android/ground/ui/map/gms/mog/README.md) in Android implementation for details.\n",
36+
"We've affectionately dubbed these Maps-optimized COGs as \"MOGs\" for convenience and brevity. See [README.md](https://github.com/google/ground-android/blob/master/ground/src/main/java/com/google/android/ground/ui/map/gms/mog/README.md) in Android implementation for details.\n",
3737
"\n",
3838
"Be sure [Generate Ground offline imagery](https://colab.research.google.com/github/google/ground-platform/blob/master/notebooks/generate_offline_imagery1.ipynb) has been run and Earth Engine tasks have completed before running this notebook.\n",
3939
"\n",
40-
"By default, only new input images will be converted. GeoTIFF which already have a correspondign MOG will be skipped unless `OVERWRITE_EXISTING` is set `True`."
40+
"By default, only new input images will be converted. GeoTIFFs which already have a corresponding MOG will be skipped unless `OVERWRITE_EXISTING` is set `True`."
4141
],
4242
"metadata": {
4343
"id": "MN0yiT1u74PH"
@@ -64,9 +64,9 @@
6464
{
6565
"cell_type": "code",
6666
"source": [
67-
"SRC_BUCKET_NAME=input('ID of bucket containing raw GeoTIFFs : ')\n",
67+
"SRC_BUCKET_NAME=input('ID of bucket containing raw GeoTIFFs: ')\n",
6868
"\n",
69-
"DEST_BUCKET_NAME=input('ID of bucket project where processed imagery is to be written: ')"
69+
"DEST_BUCKET_NAME=input('ID of bucket where processed imagery is to be written: ')"
7070
],
7171
"metadata": {
7272
"id": "wqIeqxMEI5-3"
@@ -79,7 +79,7 @@
7979
"source": [
8080
"### Custom paths\n",
8181
"\n",
82-
"The can be adjusted as needed."
82+
"These can be adjusted as needed."
8383
],
8484
"metadata": {
8585
"id": "lTpntWE-JxHs"
@@ -88,8 +88,8 @@
8888
{
8989
"cell_type": "code",
9090
"source": [
91-
"SRC_PATH=\"raw-imagery/s2/2022\"\n",
92-
"DEST_PATH=\"offline-imagery/default\""
91+
"SRC_PATH = \"raw-imagery/s2/2022\"\n",
92+
"DEST_PATH = \"offline-imagery/default\""
9393
],
9494
"metadata": {
9595
"id": "O4yVrj-6JrBM"
@@ -113,24 +113,23 @@
113113
"source": [
114114
"from math import pi\n",
115115
"\n",
116-
"HI_RES_MOG_MIN_ZOOM=8\n",
117-
"HI_RES_MOG_MAX_ZOOM=14\n",
118-
"TILE_SIZE=256\n",
119-
"NODATA_VALUE=0\n",
120-
"OVERVIEW_FILENAME=\"overview.tif\"\n",
121-
"OVERWRITE_EXISTING=False\n",
122-
"\n",
123-
"SRC_PREFIX=f\"{SRC_PATH}/{HI_RES_MOG_MIN_ZOOM}\"\n",
124-
"OVERVIEW_TIFF_PATH=f\"{SRC_PREFIX}/{OVERVIEW_FILENAME}\"\n",
125-
"DEST_PREFIX=f\"{DEST_PATH}/{HI_RES_MOG_MIN_ZOOM}\"\n",
126-
"OVERVIEW_COG_PATH=f\"{DEST_PREFIX}/{OVERVIEW_FILENAME}\"\n",
127-
"\n",
128-
"# Constants\n",
129-
"TMP_FILENAME=\"temp.tif\"\n",
130-
"GCS_BROWSER_BASE_URL=\"https://console.cloud.google.com/storage/browser\"\n",
131-
"GCS_DETAILS_BASE_URL=\"https://console.cloud.google.com/storage/browser/_details\"\n",
116+
"HI_RES_MOG_MIN_ZOOM = 8\n",
117+
"HI_RES_MOG_MAX_ZOOM = 14\n",
118+
"TILE_SIZE = 256\n",
119+
"NODATA_VALUE = 0\n",
120+
"OVERVIEW_FILENAME = \"overview.tif\"\n",
121+
"OVERWRITE_EXISTING = False\n",
122+
"\n",
123+
"SRC_PREFIX = f\"{SRC_PATH}/{HI_RES_MOG_MIN_ZOOM}\"\n",
124+
"OVERVIEW_TIFF_PATH = f\"{SRC_PREFIX}/{OVERVIEW_FILENAME}\"\n",
125+
"DEST_PREFIX = f\"{DEST_PATH}/{HI_RES_MOG_MIN_ZOOM}\"\n",
126+
"OVERVIEW_COG_PATH = f\"{DEST_PREFIX}/{OVERVIEW_FILENAME}\"\n",
127+
"\n",
128+
"TMP_FILENAME = \"temp.tif\"\n",
129+
"GCS_BROWSER_BASE_URL = \"https://console.cloud.google.com/storage/browser\"\n",
130+
"GCS_DETAILS_BASE_URL = \"https://console.cloud.google.com/storage/browser/_details\"\n",
132131
"EPSG_3857_MAX = pi * 6378137\n",
133-
"EPSG_3857_WORLD_BOUNDS=(-EPSG_3857_MAX, -EPSG_3857_MAX, EPSG_3857_MAX, EPSG_3857_MAX)"
132+
"EPSG_3857_WORLD_BOUNDS = (-EPSG_3857_MAX, -EPSG_3857_MAX, EPSG_3857_MAX, EPSG_3857_MAX)"
134133
],
135134
"metadata": {
136135
"id": "FFNaHNCUJpBE"
@@ -205,13 +204,11 @@
205204
"import rasterio\n",
206205
"from rasterio.transform import from_origin\n",
207206
"from rasterio.enums import Resampling\n",
208-
"from rasterio.windows import from_bounds\n",
209207
"from osgeo import gdal # https://gdal.org/api/python/osgeo.gdal.html\n",
210208
"import os\n",
211209
"from rasterio.plot import show\n",
212210
"import matplotlib.pyplot as plt\n",
213211
"import numpy as np\n",
214-
"import re\n",
215212
"import tempfile\n",
216213
"\n",
217214
"storage_client = storage.Client()\n",
@@ -246,7 +243,7 @@
246243
"cell_type": "code",
247244
"source": [
248245
"def show_size(path):\n",
249-
" file_stats=os.stat(path)\n",
246+
" file_stats = os.stat(path)\n",
250247
" print(f'{path}: {file_stats.st_size / (1024 * 1024):.2f} MB')\n",
251248
"\n",
252249
"def show_thumbnail(src_path):\n",
@@ -261,10 +258,10 @@
261258
" gdal.UseExceptions()\n",
262259
"\n",
263260
" # Open the input raster\n",
264-
" src=gdal.Open(src_path)\n",
261+
" src = gdal.Open(src_path)\n",
265262
"\n",
266-
" # Open the output raster\n",
267-
" ds=gdal.Translate(\n",
263+
" # Create the output raster\n",
264+
" ds = gdal.Translate(\n",
268265
" destName=TMP_FILENAME,\n",
269266
" srcDS=src,\n",
270267
" format=\"COG\",\n",
@@ -280,7 +277,7 @@
280277
"\n",
281278
" # Close the output raster to flush buffer.\n",
282279
" ds = None\n",
283-
" tmp_ds = None\n",
280+
" src = None\n",
284281
"\n",
285282
" file_size = os.stat(TMP_FILENAME).st_size / (1024 * 1024)\n",
286283
" print(f\"Uploading {file_size:.1f} MB to {dst_path}\")\n",
@@ -313,8 +310,8 @@
313310
"print(f\"Output: https://console.cloud.google.com/storage/browser/{DEST_BUCKET_NAME}/{DEST_PREFIX}\")\n",
314311
"\n",
315312
"for subpath in src_paths:\n",
316-
" src_path=f\"/vsigs_streaming/{SRC_BUCKET_NAME}/{subpath}\"\n",
317-
" dst_path=DEST_PREFIX + subpath[len(SRC_PREFIX):]\n",
313+
" src_path = f\"/vsigs_streaming/{SRC_BUCKET_NAME}/{subpath}\"\n",
314+
" dst_path = DEST_PREFIX + subpath[len(SRC_PREFIX):]\n",
318315
" blob = storage.Blob(dst_path, dest_bucket)\n",
319316
" if (not OVERWRITE_EXISTING and blob.exists(storage_client)):\n",
320317
" print(f\"{dst_path} exists, skipping.\")\n",

0 commit comments

Comments
 (0)