├── README.md ├── geemap_colab_dl.ipynb ├── processimage.ipynb ├── rfregression.ipynb ├── unet_water.ipynb └── water-butoushui.ipynb /README.md: -------------------------------------------------------------------------------- 1 | #geemap-study 2 | geemap-study 3 | 4 | 5 | 6 | 1. Beginner of geemap - Extract water using existing data products and threshold method water-butoushui.ipynb 7 | 8 | 9 | 10 | 2. GEE is used in conjunction with the local random forest regression model rfregression.ipynb 11 | 12 | 13 | 3.geemap - Use existing products to randomly stratify sampling to generate 256*256 image samples processimage.ipynb 14 | 15 | 16 | 4. Deep learning inference in geemap-geemap + Colab 17 | 18 | 19 | 5. Surface water mapping using Pytorch - full version -unetwater 20 | -------------------------------------------------------------------------------- /processimage.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "利用已有产品随机分层抽样多进程生成256*256图片样本" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import ee\n", 17 | "import geemap\n", 18 | "import logging\n", 19 | "import multiprocessing\n", 20 | "import os\n", 21 | "import requests\n", 22 | "import shutil\n", 23 | "from retry import retry\n", 24 | "geemap.set_proxy(port=4780)\n", 25 | "Map = geemap.Map(center=(30, 115), zoom=8)" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "ESRI_water为label,S2为影像值,params中的值对分层采样也对生成图片等多个函数调用" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "region = ee.FeatureCollection(\"users/yamiletsharon250/wuhan\").geometry()\n", 42 | "ESRI_water = ee.ImageCollection(\"projects/sat-io/open-datasets/landcover/ESRI_Global-LULC_10m\").mean().eq(1).selfMask().clip(region)\n", 43 | "S2 = ee.ImageCollection('COPERNICUS/S2_SR').filterBounds(region).filterDate('2020', '2021').select( 'B8','B4', 'B3').median().visualize(min=0, max=4000).clip(region) \n", 44 | "params = {\n", 45 | " 'count': 100, # How many image chips to export\n", 46 | " 'buffer': 2560, # The buffer distance (m) around each point\n", 47 | " 'scale': 100, # The scale to do stratified sampling\n", 48 | " 'seed': 1, # A randomization seed to use for subsampling.\n", 49 | " 'dimensions': '256x256', # The dimension of each image chip\n", 50 | " 'format': \"png\", # The output image format, can be png, jpg, ZIPPED_GEO_TIFF, GEO_TIFF, NPY\n", 51 | " 'prefix': 'tile_', # The filename prefix\n", 52 | " 'processes': 25, # How many processes to used for parallel processing\n", 53 | " 'label_out_dir': '/label', # The label output directory. Default to the current working directly\n", 54 | " 'val_out_dir': '/val', # The val output directory. Default to the current working directly\n", 55 | "}\n", 56 | "def getSamples():\n", 57 | " img = ESRI_water\n", 58 | " points = img.stratifiedSample(\n", 59 | " numPoints=params['count'],\n", 60 | " region=region,\n", 61 | " scale=params['scale'],\n", 62 | " seed=params['seed'],\n", 63 | " geometries=True,\n", 64 | " )\n", 65 | " Map.data = points\n", 66 | " return points.aggregate_array('.geo').getInfo()\n", 67 | "\n", 68 | "viz = {min: 1,max: 1,'opacity':1,'palette':['blue']}\n", 69 | "Map.addLayer(S2, {}, \"landasat\")\n", 70 | "Map.addLayer(ESRI_water, viz, \"ESRI_water\")\n", 71 | "Map.addLayer(region, {}, \"ROI\",False)\n", 72 | "Map" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "因为jupyter不支持对进程调用,这步的目的是保存一个py文件方便调用" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "%%writefile test.py\n", 89 | "import ee\n", 90 | "import geemap\n", 91 | "import os\n", 92 | "import requests\n", 93 | "import shutil\n", 94 | "from retry import retry\n", 95 | "geemap.set_proxy(port=4780)\n", 96 | "ee.Initialize(opt_url='https://earthengine-highvolume.googleapis.com')\n", 97 | "region = ee.FeatureCollection(\"users/yamiletsharon250/wuhan\").geometry()\n", 98 | "ESRI_water = ee.ImageCollection(\"projects/sat-io/open-datasets/landcover/ESRI_Global-LULC_10m\").mean().eq(1).selfMask().clip(region)\n", 99 | "S2 = ee.ImageCollection('COPERNICUS/S2_SR').filterBounds(region).filterDate('2020', '2021').select( 'B8','B4', 'B3').median().visualize(min=0, max=4000).clip(region) \n", 100 | "params = {\n", 101 | " 'count': 100, # How many image chips to export\n", 102 | " 'buffer': 2560, # The buffer distance (m) around each point\n", 103 | " 'scale': 100, # The scale to do stratified sampling\n", 104 | " 'seed': 1, # A randomization seed to use for subsampling.\n", 105 | " 'dimensions': '256x256', # The dimension of each image chip\n", 106 | " 'format': \"png\", # The output image format, can be png, jpg, ZIPPED_GEO_TIFF, GEO_TIFF, NPY\n", 107 | " 'prefix': 'tile_', # The filename prefix\n", 108 | " 'processes': 25, # How many processes to used for parallel processing\n", 109 | " 'label_out_dir': '/label', # The output directory. Default to the current working directly\n", 110 | " 'val_out_dir': '/val',\n", 111 | "}\n", 112 | "@retry(tries=10, delay=1, backoff=2)\n", 113 | "def getLabelResult(index, point):\n", 114 | " point = ee.Geometry.Point(point['coordinates'])\n", 115 | " region = point.buffer(params['buffer']).bounds()\n", 116 | "\n", 117 | " if params['format'] in ['png', 'jpg']:\n", 118 | " url = ESRI_water.getThumbURL(\n", 119 | " {\n", 120 | " 'region': region,\n", 121 | " 'dimensions': params['dimensions'],\n", 122 | " 'format': params['format'],\n", 123 | " }\n", 124 | " )\n", 125 | " else:\n", 126 | " url = ESRI_water.getDownloadURL(\n", 127 | " {\n", 128 | " 'region': region,\n", 129 | " 'dimensions': params['dimensions'],\n", 130 | " 'format': params['format'],\n", 131 | " }\n", 132 | " )\n", 133 | "\n", 134 | " if params['format'] == \"GEO_TIFF\":\n", 135 | " ext = 'tif'\n", 136 | " else:\n", 137 | " ext = params['format']\n", 138 | "\n", 139 | " r = requests.get(url, stream=True)\n", 140 | " if r.status_code != 200:\n", 141 | " r.raise_for_status()\n", 142 | " out_dir = os.path.abspath(params['label_out_dir'])\n", 143 | " basename = str(index).zfill(len(str(params['count'])))\n", 144 | " filename = f\"{out_dir}/{params['prefix']}{basename}.{ext}\"\n", 145 | " with open(filename, 'wb') as out_file:\n", 146 | " shutil.copyfileobj(r.raw, out_file)\n", 147 | " print(\"Done: \", basename)\n", 148 | " \n", 149 | "@retry(tries=10, delay=1, backoff=2)\n", 150 | "def getValResult(index, point):\n", 151 | " point = ee.Geometry.Point(point['coordinates'])\n", 152 | " region = point.buffer(params['buffer']).bounds()\n", 153 | "\n", 154 | " if params['format'] in ['png', 'jpg']:\n", 155 | " url = S2.getThumbURL(\n", 156 | " {\n", 157 | " 'region': region,\n", 158 | " 'dimensions': params['dimensions'],\n", 159 | " 'format': params['format'],\n", 160 | " }\n", 161 | " )\n", 162 | " else:\n", 163 | " url = S2.getDownloadURL(\n", 164 | " {\n", 165 | " 'region': region,\n", 166 | " 'dimensions': params['dimensions'],\n", 167 | " 'format': params['format'],\n", 168 | " }\n", 169 | " )\n", 170 | "\n", 171 | " if params['format'] == \"GEO_TIFF\":\n", 172 | " ext = 'tif'\n", 173 | " else:\n", 174 | " ext = params['format']\n", 175 | "\n", 176 | " r = requests.get(url, stream=True)\n", 177 | " if r.status_code != 200:\n", 178 | " r.raise_for_status()\n", 179 | " out_dir = os.path.abspath(params['val_out_dir'])\n", 180 | " basename = str(index).zfill(len(str(params['count'])))\n", 181 | " filename = f\"{out_dir}/{params['prefix']}{basename}.{ext}\"\n", 182 | " with open(filename, 'wb') as out_file:\n", 183 | " shutil.copyfileobj(r.raw, out_file)\n", 184 | " print(\"Done: \", basename)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "多进程运行保存的test.py" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "import test\n", 201 | "logging.basicConfig()\n", 202 | "items = getSamples()\n", 203 | "pool = multiprocessing.Pool(params['processes'])\n", 204 | "pool.starmap(test.getLabelResult, enumerate(items))\n", 205 | "pool.starmap(test.getValResult, enumerate(items))\n", 206 | "pool.close()\n", 207 | "Map.addLayer(Map.data, {}, \"Sample points\")" 208 | ] 209 | } 210 | ], 211 | "metadata": { 212 | "kernelspec": { 213 | "display_name": "Python 3.8.13 ('geemap')", 214 | "language": "python", 215 | "name": "python3" 216 | }, 217 | "language_info": { 218 | "codemirror_mode": { 219 | "name": "ipython", 220 | "version": 3 221 | }, 222 | "file_extension": ".py", 223 | "mimetype": "text/x-python", 224 | "name": "python", 225 | "nbconvert_exporter": "python", 226 | "pygments_lexer": "ipython3", 227 | "version": "3.8.13" 228 | }, 229 | "orig_nbformat": 4, 230 | "vscode": { 231 | "interpreter": { 232 | "hash": "4a860719dd1f9b140713c0900992a4e3a702812e36d6d1efbb6e4981c621e303" 233 | } 234 | } 235 | }, 236 | "nbformat": 4, 237 | "nbformat_minor": 2 238 | } 239 | -------------------------------------------------------------------------------- /rfregression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "该笔记本记载了gee上通过样本点获取训练数据-训练回归模型-云端调用-回归显示的全流程" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import ee\n", 17 | "import geemap\n", 18 | "import pandas as pd\n", 19 | "from geemap import ml\n", 20 | "from sklearn import ensemble\n", 21 | "geemap.set_proxy(port=4780)\n", 22 | "Map = geemap.Map(center=(30, 120), zoom=4)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "合成的土地利用poi点和边界导入" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "application/vnd.jupyter.widget-view+json": { 40 | "model_id": "c6590134ccf54bfb8f077051b92a233f", 41 | "version_major": 2, 42 | "version_minor": 0 43 | }, 44 | "text/plain": [ 45 | "Map(center=[30, 120], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=HBox(children=(To…" 46 | ] 47 | }, 48 | "metadata": {}, 49 | "output_type": "display_data" 50 | } 51 | ], 52 | "source": [ 53 | "points = ee.FeatureCollection('projects/assimov/assets/wuhanpoi')\n", 54 | "ROI = ee.FeatureCollection(\"users/yamiletsharon250/wuhan\").geometry()\n", 55 | "Map.addLayer(ROI, {}, \"武汉\")\n", 56 | "Map.addLayer(points, {}, \"武汉点\")\n", 57 | "Map" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "常规的去云以及波段合成操作" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 3, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "def rmL8Cloud(image):\n", 74 | " cloudShadowBitMask = (1 << 3); \n", 75 | " cloudsBitMask = (1 << 5); \n", 76 | " qa = image.select('pixel_qa'); \n", 77 | " mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).And(qa.bitwiseAnd(cloudsBitMask).eq(0)); \n", 78 | " return image.updateMask(mask).toFloat().divide(10000).copyProperties(image).copyProperties(image, [\"system:time_start\",'system:time_end','system:footprint'])\n", 79 | "\n", 80 | "collection = ee.ImageCollection(\"LANDSAT/LC08/C01/T1_SR\") \\\n", 81 | " .map(rmL8Cloud)\\\n", 82 | " .filterDate('2021-01-01','2022-01-01') \\\n", 83 | " .filterBounds(ROI) \\\n", 84 | " .filter(ee.Filter.lte('CLOUD_COVER',5))\\\n", 85 | " \n", 86 | "image=collection.mean().clip(ROI)\n", 87 | "def NDWI(img):\n", 88 | " nir = img.select('B4')\n", 89 | " green = img.select('B2')\n", 90 | " ndwi = img.expression(\n", 91 | " '(B3-B5)/(B3+B5)',\n", 92 | " {\n", 93 | " 'B5':nir,\n", 94 | " 'B3':green\n", 95 | " })\n", 96 | " return ndwi\n", 97 | "\n", 98 | "def EWI(img):\n", 99 | " swir1 = img.select('B6')\n", 100 | " nir = img.select('B5')\n", 101 | " green = img.select('B3')\n", 102 | " ewi = img.expression(\n", 103 | " '(B3-B5-B6)/(B3+B5+B6)',\n", 104 | " {\n", 105 | " 'B6':swir1,\n", 106 | " 'B5':nir,\n", 107 | " 'B3':green\n", 108 | " })\n", 109 | " return ewi\n", 110 | "\n", 111 | "MNDWI = image.normalizedDifference(['B3', 'B6']).rename('MNDWI');#计算MNDWI\n", 112 | "ndbi = image.normalizedDifference(['B6', 'B5']).rename('NDBI');#计算NDBI\n", 113 | "ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');#计算NDVI\n", 114 | "ewi = EWI(image).rename('EWI');#计算NDVI\n", 115 | "ndwi = NDWI(image).rename('NDWI');#计算MNDWI\n", 116 | "lswi = image.normalizedDifference(['B5','B6']).rename('LSWI')\n", 117 | "nbr2 = image.normalizedDifference([\"B6\", \"B7\"]).rename(\"NBR2\")\n", 118 | "awei = image.expression(\n", 119 | " '4*(green-SWIR1)-(0.25*NIR+2.75*SWIR2)',{\n", 120 | " 'green':image.select('B3'),\n", 121 | " 'NIR':image.select('B5'),\n", 122 | " 'SWIR1':image.select('B6'),\n", 123 | " 'SWIR2':image.select('B7'),\n", 124 | " }).float().rename('AWEI')\n", 125 | "image=image \\\n", 126 | " .addBands(ndvi) \\\n", 127 | " .addBands(ndbi) \\\n", 128 | " .addBands(MNDWI) \\\n", 129 | " .addBands(ndwi) \\\n", 130 | " .addBands(ewi) \\\n", 131 | " .addBands(lswi) \\\n", 132 | " .addBands(awei) \\\n", 133 | " .addBands(nbr2)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "设置训练波段,导出训练数据至本地" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 6, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7','MNDWI','NDBI','NDVI','EWI','NDWI','AWEI','LSWI',\"NBR2\"]\n", 150 | "training = image.select(bands).sampleRegions(**{\n", 151 | " 'collection': points,\n", 152 | " 'properties': ['landcover'],\n", 153 | " 'scale': 30\n", 154 | "})\n", 155 | "geemap.ee_to_csv(training,\"rftrain.csv\")" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "可视化dataframe" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 5, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "data": { 172 | "text/html": [ 173 | "
\n", 174 | "\n", 187 | "\n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | "
system:indexlandcoverAWEINDWINDVIB2NBR2B3B4B5B6MNDWIB7EWINDBILSWI
000000000000000000087_010.400050-0.222047-0.5783050.07410.2291670.11040.11640.03110.00590.8985380.00370.497965-0.6810810.681081
100000000000000000087_110.402425-0.223677-0.5720050.07410.2391300.11070.11680.03180.00570.9020620.00350.493927-0.6960000.696000
200000000000000000087_210.402250-0.229963-0.5897090.07350.2127660.11070.11740.03030.00570.9020620.00370.509202-0.6833330.683333
300000000000000000087_310.406175-0.231491-0.5927170.07370.2584270.11130.11810.03020.00560.9041920.00330.513256-0.6871510.687151
400000000000000000087_410.405400-0.227723-0.5929680.07410.2666670.11120.11780.03010.00570.9024810.00330.512925-0.6815640.681564
...................................................
358700000000000000000588_05-0.991900-0.1525180.5395230.05890.2269290.08220.08010.26780.2187-0.4536390.1378-0.710920-0.1009250.100925
35880000000000000000058a_05-1.202575-0.1708560.1249470.14680.0526510.18690.20730.26650.2909-0.2176640.2618-0.4977830.043775-0.043775
35890000000000000000058a_15-1.201125-0.1741000.1125100.15370.0485300.19750.21850.27390.2960-0.1995950.2686-0.4852750.038779-0.038779
35900000000000000000058d_05-0.970475-0.2118850.1650010.11140.0846070.14810.17130.23900.2378-0.2324440.2007-0.526004-0.0025170.002517
35910000000000000000058d_15-1.158500-0.1798940.1600750.12400.0654280.15780.17840.24640.2695-0.2614090.2364-0.5315420.044776-0.044776
\n", 421 | "

3592 rows × 16 columns

\n", 422 | "
" 423 | ], 424 | "text/plain": [ 425 | " system:index landcover AWEI NDWI NDVI B2 \\\n", 426 | "0 00000000000000000087_0 1 0.400050 -0.222047 -0.578305 0.0741 \n", 427 | "1 00000000000000000087_1 1 0.402425 -0.223677 -0.572005 0.0741 \n", 428 | "2 00000000000000000087_2 1 0.402250 -0.229963 -0.589709 0.0735 \n", 429 | "3 00000000000000000087_3 1 0.406175 -0.231491 -0.592717 0.0737 \n", 430 | "4 00000000000000000087_4 1 0.405400 -0.227723 -0.592968 0.0741 \n", 431 | "... ... ... ... ... ... ... \n", 432 | "3587 00000000000000000588_0 5 -0.991900 -0.152518 0.539523 0.0589 \n", 433 | "3588 0000000000000000058a_0 5 -1.202575 -0.170856 0.124947 0.1468 \n", 434 | "3589 0000000000000000058a_1 5 -1.201125 -0.174100 0.112510 0.1537 \n", 435 | "3590 0000000000000000058d_0 5 -0.970475 -0.211885 0.165001 0.1114 \n", 436 | "3591 0000000000000000058d_1 5 -1.158500 -0.179894 0.160075 0.1240 \n", 437 | "\n", 438 | " NBR2 B3 B4 B5 B6 MNDWI B7 EWI \\\n", 439 | "0 0.229167 0.1104 0.1164 0.0311 0.0059 0.898538 0.0037 0.497965 \n", 440 | "1 0.239130 0.1107 0.1168 0.0318 0.0057 0.902062 0.0035 0.493927 \n", 441 | "2 0.212766 0.1107 0.1174 0.0303 0.0057 0.902062 0.0037 0.509202 \n", 442 | "3 0.258427 0.1113 0.1181 0.0302 0.0056 0.904192 0.0033 0.513256 \n", 443 | "4 0.266667 0.1112 0.1178 0.0301 0.0057 0.902481 0.0033 0.512925 \n", 444 | "... ... ... ... ... ... ... ... ... \n", 445 | "3587 0.226929 0.0822 0.0801 0.2678 0.2187 -0.453639 0.1378 -0.710920 \n", 446 | "3588 0.052651 0.1869 0.2073 0.2665 0.2909 -0.217664 0.2618 -0.497783 \n", 447 | "3589 0.048530 0.1975 0.2185 0.2739 0.2960 -0.199595 0.2686 -0.485275 \n", 448 | "3590 0.084607 0.1481 0.1713 0.2390 0.2378 -0.232444 0.2007 -0.526004 \n", 449 | "3591 0.065428 0.1578 0.1784 0.2464 0.2695 -0.261409 0.2364 -0.531542 \n", 450 | "\n", 451 | " NDBI LSWI \n", 452 | "0 -0.681081 0.681081 \n", 453 | "1 -0.696000 0.696000 \n", 454 | "2 -0.683333 0.683333 \n", 455 | "3 -0.687151 0.687151 \n", 456 | "4 -0.681564 0.681564 \n", 457 | "... ... ... \n", 458 | "3587 -0.100925 0.100925 \n", 459 | "3588 0.043775 -0.043775 \n", 460 | "3589 0.038779 -0.038779 \n", 461 | "3590 -0.002517 0.002517 \n", 462 | "3591 0.044776 -0.044776 \n", 463 | "\n", 464 | "[3592 rows x 16 columns]" 465 | ] 466 | }, 467 | "execution_count": 5, 468 | "metadata": {}, 469 | "output_type": "execute_result" 470 | } 471 | ], 472 | "source": [ 473 | "df = pd.read_csv(\"rftrain.csv\")\n", 474 | "df" 475 | ] 476 | }, 477 | { 478 | "cell_type": "markdown", 479 | "metadata": {}, 480 | "source": [ 481 | "随机森林回归训练n_trees为参数,还有其他参数可以调,我只是简单打个样" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 6, 487 | "metadata": {}, 488 | "outputs": [], 489 | "source": [ 490 | "feature_names = ['AWEI','NDWI','NDVI','B2','NBR2','B3','B4','B5','B6','MNDWI','B7','EWI','NDBI','LSWI']\n", 491 | "label = \"landcover\"\n", 492 | "X = df[feature_names]\n", 493 | "y = df[label]\n", 494 | "n_trees = 10\n", 495 | "rf = ensemble.RandomForestRegressor(n_trees).fit(X, y)\n", 496 | "rf" 497 | ] 498 | }, 499 | { 500 | "cell_type": "markdown", 501 | "metadata": {}, 502 | "source": [ 503 | "将rf模型转为trees" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 9, 509 | "metadata": {}, 510 | "outputs": [], 511 | "source": [ 512 | "trees = ml.rf_to_strings(rf, feature_names,output_mode='REGRESSION')" 513 | ] 514 | }, 515 | { 516 | "cell_type": "markdown", 517 | "metadata": {}, 518 | "source": [ 519 | "trees转classifier" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 11, 525 | "metadata": {}, 526 | "outputs": [], 527 | "source": [ 528 | "ee_classifier = ml.strings_to_classifier(trees)\n", 529 | "#ee_classifier.getInfo()" 530 | ] 531 | }, 532 | { 533 | "cell_type": "markdown", 534 | "metadata": {}, 535 | "source": [ 536 | "将模型保存到本地" 537 | ] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": 4, 542 | "metadata": {}, 543 | "outputs": [], 544 | "source": [ 545 | "ml.trees_to_csv(trees, \"trees.csv\")\n", 546 | "ee_classifierlocal =ml.csv_to_classifier(\"trees.csv\")\n", 547 | "#ee_classifierlocal.getInfo()" 548 | ] 549 | }, 550 | { 551 | "cell_type": "markdown", 552 | "metadata": {}, 553 | "source": [ 554 | "将模型保存到云端,可能得等会,因为保存要时间,上传有限制不能超过40m" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": 13, 560 | "metadata": {}, 561 | "outputs": [], 562 | "source": [ 563 | "ml.export_trees_to_fc(trees, 'users/yamiletsharon250/rfregression')" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": null, 569 | "metadata": {}, 570 | "outputs": [], 571 | "source": [ 572 | "rf_fc = ee.FeatureCollection('users/yamiletsharon250/rfregression')\n", 573 | "ee_classifiercloud = ml.fc_to_classifier(rf_fc)\n", 574 | "#ee_classifiercloud.getInfo()" 575 | ] 576 | }, 577 | { 578 | "cell_type": "markdown", 579 | "metadata": {}, 580 | "source": [ 581 | "回归模型验证,使用的是local模型,cloud模型在js端验证" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 7, 587 | "metadata": {}, 588 | "outputs": [], 589 | "source": [ 590 | "regression = image.select(bands).classify(ee_classifierlocal, 'predicted')" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 8, 596 | "metadata": {}, 597 | "outputs": [], 598 | "source": [ 599 | "regressionMin = (regression.reduceRegion(\n", 600 | " **{\n", 601 | "'reducer': ee.Reducer.min(),\n", 602 | "'scale': 30,\n", 603 | "'crs': 'EPSG:3857',\n", 604 | "'bestEffort': True,\n", 605 | "'tileScale': 16\n", 606 | "}\n", 607 | "))\n", 608 | "regressionMax = (regression.reduceRegion(\n", 609 | " **{\n", 610 | "'reducer': ee.Reducer.max(),\n", 611 | "'scale': 30,\n", 612 | "'crs': 'EPSG:3857',\n", 613 | "'bestEffort': True,\n", 614 | "'tileScale': 16\n", 615 | "}\n", 616 | "))" 617 | ] 618 | }, 619 | { 620 | "cell_type": "code", 621 | "execution_count": 18, 622 | "metadata": {}, 623 | "outputs": [ 624 | { 625 | "data": { 626 | "text/plain": [ 627 | "1" 628 | ] 629 | }, 630 | "execution_count": 18, 631 | "metadata": {}, 632 | "output_type": "execute_result" 633 | } 634 | ], 635 | "source": [ 636 | "\n", 637 | "# regressionMin.getNumber('predicted').getInfo()\n", 638 | "# regressionMax.getNumber('predicted').getInfo()" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 10, 644 | "metadata": {}, 645 | "outputs": [ 646 | { 647 | "data": { 648 | "application/vnd.jupyter.widget-view+json": { 649 | "model_id": "c6590134ccf54bfb8f077051b92a233f", 650 | "version_major": 2, 651 | "version_minor": 0 652 | }, 653 | "text/plain": [ 654 | "Map(bottom=2097.0, center=[21.53484700204879, 119.00390625000001], controls=(WidgetControl(options=['position'…" 655 | ] 656 | }, 657 | "metadata": {}, 658 | "output_type": "display_data" 659 | } 660 | ], 661 | "source": [ 662 | "viz = {min: 1,max: 5,'opacity':1,'palette':['blue','yellow','ffffff','red','green']}\n", 663 | "Map.addLayer(regression, viz, 'Regression')\n", 664 | "Map" 665 | ] 666 | } 667 | ], 668 | "metadata": { 669 | "kernelspec": { 670 | "display_name": "Python 3.9.12 ('gee')", 671 | "language": "python", 672 | "name": "python3" 673 | }, 674 | "language_info": { 675 | "codemirror_mode": { 676 | "name": "ipython", 677 | "version": 3 678 | }, 679 | "file_extension": ".py", 680 | "mimetype": "text/x-python", 681 | "name": "python", 682 | "nbconvert_exporter": "python", 683 | "pygments_lexer": "ipython3", 684 | "version": "3.9.12" 685 | }, 686 | "orig_nbformat": 4, 687 | "vscode": { 688 | "interpreter": { 689 | "hash": "5d51a8884af55115bb6d844298b2a4bf9c86da95dd33d0236e0d8eb5aff4e382" 690 | } 691 | } 692 | }, 693 | "nbformat": 4, 694 | "nbformat_minor": 2 695 | } 696 | -------------------------------------------------------------------------------- /water-butoushui.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "pycharm": { 8 | "is_executing": true 9 | } 10 | }, 11 | "outputs": [ 12 | { 13 | "data": { 14 | "application/vnd.jupyter.widget-view+json": { 15 | "model_id": "f3538c70531948aca30e947c82435415", 16 | "version_major": 2, 17 | "version_minor": 0 18 | }, 19 | "text/plain": [ 20 | "Map(center=[30, 120], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=HBox(children=(To…" 21 | ] 22 | }, 23 | "metadata": {}, 24 | "output_type": "display_data" 25 | } 26 | ], 27 | "source": [ 28 | "import ee\n", 29 | "import geemap\n", 30 | "#port根据自己科学工具的端口号修改\n", 31 | "geemap.set_proxy(port=4780)\n", 32 | "Map = geemap.Map(center=(30, 120), zoom=4)\n", 33 | "table = ee.FeatureCollection(\"users/yamiletsharon250/wuhan\")\n", 34 | "roi = table\n", 35 | "Map.addLayer(roi, {}, \"武汉\")\n", 36 | "Map" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "#获取影像与去云\n", 46 | "\n", 47 | "#l457 去云函数\n", 48 | "def rmL457Cloud(image):\n", 49 | " qa = image.select('pixel_qa')\n", 50 | " cloud = qa.bitwiseAnd(1 << 5) \\\n", 51 | " .And(qa.bitwiseAnd(1 << 7)) \\\n", 52 | " .Or(qa.bitwiseAnd(1 << 3))\n", 53 | " mask2 = image.mask().reduce(ee.Reducer.min())\n", 54 | " mask3 = image.select('B1').gt(2000)\n", 55 | " return image.updateMask(cloud.Not()).updateMask(mask2).updateMask(mask3.Not()) \\\n", 56 | " .copyProperties(image) \\\n", 57 | " .copyProperties(image, [\"system:time_start\",'system:time_end','system:footprint'])\n", 58 | "\n", 59 | "#l8 去云函数\n", 60 | "def rmL8Cloud(image):\n", 61 | " cloudShadowBitMask = (1 << 3)\n", 62 | " cloudsBitMask = (1 << 5)\n", 63 | " qa = image.select('pixel_qa')\n", 64 | " mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) \\\n", 65 | " .And(qa.bitwiseAnd(cloudsBitMask).eq(0))\n", 66 | " mask2 = image.select('B1').gt(2000)\n", 67 | " return image.updateMask(mask).updateMask(mask2.Not()) \\\n", 68 | " .copyProperties(image) \\\n", 69 | " .copyProperties(image, [\"system:time_start\",'system:time_end'])\n", 70 | " \n", 71 | "#定义影像执行云量筛选与去云\n", 72 | "l8_sr = ee.ImageCollection(\"LANDSAT/LC08/C01/T1_SR\").map(rmL8Cloud) \\\n", 73 | " .filter(ee.Filter.lte('CLOUD_COVER',10))\n", 74 | "\n", 75 | "l7_sr = ee.ImageCollection(\"LANDSAT/LE07/C01/T1_SR\").map(rmL457Cloud) \\\n", 76 | " .filter(ee.Filter.lte('CLOUD_COVER',10))\n", 77 | "\n", 78 | "l5_sr = ee.ImageCollection(\"LANDSAT/LT05/C01/T1_SR\").map(rmL457Cloud) \\\n", 79 | " .filter(ee.Filter.lte('CLOUD_COVER',10))" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "#计算水体方法_ARWDR,可以查阅论文,我忘了是哪篇了。。。\n", 89 | "#获取dem山体角,意义在于去除山体阴影\n", 90 | "def calcWater(image):\n", 91 | " MNDWI = image.select(\"MNDWI\")\n", 92 | " NDWI = image.select(\"MNDWI\")\n", 93 | " NDVI = image.select(\"NDVI\")\n", 94 | " EVI = image.select(\"EVI\")\n", 95 | " water = NDWI.gt(0) \\\n", 96 | " .And(MNDWI.gt(0.1) \\\n", 97 | " .And(EVI.lt(0.1)) \\\n", 98 | " .And(MNDWI.gt(NDVI)) \\\n", 99 | " .Or(MNDWI.gt(EVI)))\n", 100 | " dem = ee.Image(\"USGS/GMTED2010\").select(0)\n", 101 | " azimuth = ee.Number(image.get('SUN_AZIMUTH'))\n", 102 | " ele = ee.Number(image.get('SUN_ELEVATION'))\n", 103 | " shadow = ee.Algorithms.HillShadow(dem, azimuth, ele).eq(1)\n", 104 | " return image.addBands(water.rename(\"water\")).updateMask(shadow)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 4, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "#Landsat-5/7处理方法\n", 114 | "\n", 115 | " #Landsat SR数据需要缩放,比例是 0.0001#\n", 116 | "def Landsat57scaleImage(image):\n", 117 | " time_start = image.get(\"system:time_start\")\n", 118 | " SUN_AZIMUTH = image.get('SOLAR_AZIMUTH_ANGLE')\n", 119 | " SUN_ELEVATION = image.get('SOLAR_ZENITH_ANGLE')\n", 120 | " image = image.select([\"B1\",\"B2\",\"B3\",\"B4\",\"B5\",\"B7\"])\n", 121 | " image = image.divide(10000)\n", 122 | " image = image.set(\"system:time_start\", time_start) \\\n", 123 | " .set(\"SUN_AZIMUTH\",SUN_AZIMUTH ) \\\n", 124 | " .set(\"SUN_ELEVATION\", SUN_ELEVATION)\n", 125 | " return image\n", 126 | "\n", 127 | " # SR数据去云#\n", 128 | "def Landsat57srCloudMask(image):\n", 129 | " qa = image.select('pixel_qa')\n", 130 | " cloudShadowBitMask = (1 << 3)\n", 131 | " snowBitMask = (1 << 4)\n", 132 | " cloudsBitMask = (1 << 5)\n", 133 | " mask1 = qa.bitwiseAnd(cloudsBitMask).eq(0) \\\n", 134 | " .And(qa.bitwiseAnd(snowBitMask).eq(0)) \\\n", 135 | " .And(qa.bitwiseAnd(cloudShadowBitMask).eq(0))\n", 136 | " mask2 = image.mask().reduce(ee.Reducer.min())\n", 137 | " return image.updateMask(mask1.And(mask2))\n", 138 | "\n", 139 | "\n", 140 | " #NDVI\n", 141 | "def Landsat57NDVI(image):\n", 142 | " return image.addBands(image.normalizedDifference([\"B4\", \"B3\"]) \\\n", 143 | " .rename(\"NDVI\"))\n", 144 | " \n", 145 | "\n", 146 | "def Landsat57NDWI(image):\n", 147 | " return image.addBands(image.normalizedDifference([\"B2\", \"B4\"]) \\\n", 148 | " .rename(\"NDWI\"))\n", 149 | " #MNDWI\n", 150 | "\n", 151 | "def Landsat57MNDWI(image):\n", 152 | " return image.addBands(image.normalizedDifference([\"B2\", \"B5\"]) \\\n", 153 | " .rename(\"MNDWI\"))\n", 154 | "\n", 155 | " # EVI\n", 156 | "def Landsat57EVI(image):\n", 157 | " evi = image.expression(\"EVI = 2.5 * (NIR - R) / (NIR + 6*R -7.5*B + 1)\", {\n", 158 | " 'NIR': image.select(\"B4\"),\n", 159 | " 'R': image.select(\"B3\"),\n", 160 | " 'B': image.select(\"B1\")\n", 161 | " })\n", 162 | " return image.addBands(evi)\n", 163 | "\n", 164 | "\n", 165 | " #获取Landsat5的SR数据,并select water#\n", 166 | "def Landsat57getL5SRCollection(startDate, endDate, region):\n", 167 | " dataset = l5_sr.filterDate(startDate, endDate) \\\n", 168 | " .filterBounds(region) \\\n", 169 | " .map(Landsat57srCloudMask) \\\n", 170 | " .map(Landsat57scaleImage) \\\n", 171 | " .map(Landsat57NDVI) \\\n", 172 | " .map(Landsat57MNDWI) \\\n", 173 | " .map(Landsat57NDWI) \\\n", 174 | " .map(Landsat57EVI) \\\n", 175 | " .map(calcWater) \\\n", 176 | " .select(\"water\")\n", 177 | " return dataset\n", 178 | "\n", 179 | " #* 获取Landsat7的SR数据#\n", 180 | "def Landsat57getL7SRCollection(startDate, endDate, region):\n", 181 | " dataset = l7_sr.filterDate(startDate, endDate) \\\n", 182 | " .filterBounds(region) \\\n", 183 | " .map(Landsat57srCloudMask) \\\n", 184 | " .map(Landsat57scaleImage) \\\n", 185 | " .map(Landsat57NDVI) \\\n", 186 | " .map(Landsat57NDWI) \\\n", 187 | " .map(Landsat57MNDWI) \\\n", 188 | " .map(Landsat57EVI) \\\n", 189 | " .map(calcWater) \\\n", 190 | " .select(\"water\")\n", 191 | " return dataset" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 5, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "#Landsat 8处理方法\n", 201 | "\n", 202 | " # Landsat8 SR数据缩放#\n", 203 | "def Landsat8scaleImage(image):\n", 204 | " time_start = image.get(\"system:time_start\")\n", 205 | " SUN_AZIMUTH = image.get('SOLAR_AZIMUTH_ANGLE')\n", 206 | " SUN_ELEVATION = image.get('SOLAR_ZENITH_ANGLE')\n", 207 | " image = image.select([\"B2\",\"B3\",\"B4\",\"B5\",\"B6\",\"B7\"])\n", 208 | " image = image.divide(10000)\n", 209 | " image = image.set(\"system:time_start\", time_start) \\\n", 210 | " .set(\"SUN_AZIMUTH\",SUN_AZIMUTH ) \\\n", 211 | " .set(\"SUN_ELEVATION\", SUN_ELEVATION)\n", 212 | " return image\n", 213 | "\n", 214 | "\n", 215 | " #SR数据去云#\n", 216 | "def Landsat8srCloudMask(image):\n", 217 | " cloudShadowBitMask = (1 << 3)\n", 218 | " snowBitMask = (1 << 4)\n", 219 | " cloudsBitMask = (1 << 5)\n", 220 | " qa = image.select('pixel_qa')\n", 221 | " mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) \\\n", 222 | " .And(qa.bitwiseAnd(snowBitMask).eq(0)) \\\n", 223 | " .And(qa.bitwiseAnd(cloudsBitMask).eq(0))\n", 224 | " return image.updateMask(mask)\n", 225 | "\n", 226 | "\n", 227 | " #NDVI\n", 228 | "def Landsat8NDVI(image):\n", 229 | " return image.addBands(image.normalizedDifference([\"B5\", \"B4\"]) \\\n", 230 | " .rename(\"NDVI\"))\n", 231 | "def Landsat8NDWI(image):\n", 232 | " return image.addBands(image.normalizedDifference([\"B3\", \"B5\"]) \\\n", 233 | " .rename(\"NDWI\"))\n", 234 | "\n", 235 | " #MNDWI\n", 236 | "def Landsat8MNDWI(image):\n", 237 | " return image.addBands(image.normalizedDifference([\"B3\", \"B6\"]) \\\n", 238 | " .rename(\"MNDWI\"))\n", 239 | "\n", 240 | "\n", 241 | " # EVI\n", 242 | "def Landsat8EVI(image):\n", 243 | " evi = image.expression(\"EVI = 2.5 * (NIR - R) / (NIR + 6*R -7.5*B + 1)\", {\n", 244 | " 'NIR': image.select(\"B5\"),\n", 245 | " 'R': image.select(\"B4\"),\n", 246 | " 'B': image.select(\"B2\")\n", 247 | " })\n", 248 | " return image.addBands(evi)\n", 249 | "\n", 250 | "\n", 251 | " #获取Landsat8的SR数据#\n", 252 | "def Landsat8getL8SRCollection(startDate, endDate, region):\n", 253 | " dataset = l8_sr.filterDate(startDate, endDate) \\\n", 254 | " .filterBounds(roi) \\\n", 255 | " .map(Landsat8srCloudMask) \\\n", 256 | " .map(Landsat8scaleImage) \\\n", 257 | " .map(Landsat8NDVI) \\\n", 258 | " .map(Landsat8MNDWI) \\\n", 259 | " .map(Landsat8NDWI) \\\n", 260 | " .map(Landsat8EVI) \\\n", 261 | " .map(calcWater) \\\n", 262 | " .select(\"water\")\n", 263 | " return dataset" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 6, 269 | "metadata": {}, 270 | "outputs": [ 271 | { 272 | "name": "stdout", 273 | "output_type": "stream", 274 | "text": [ 275 | "2015\n" 276 | ] 277 | }, 278 | { 279 | "data": { 280 | "application/vnd.jupyter.widget-view+json": { 281 | "model_id": "f3538c70531948aca30e947c82435415", 282 | "version_major": 2, 283 | "version_minor": 0 284 | }, 285 | "text/plain": [ 286 | "Map(center=[30, 120], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=HBox(children=(To…" 287 | ] 288 | }, 289 | "metadata": {}, 290 | "output_type": "display_data" 291 | } 292 | ], 293 | "source": [ 294 | "#生成逐年的水体\n", 295 | "def processYearWaterImage(year, region):\n", 296 | " startDate = ee.Date.fromYMD(year, 1, 1)\n", 297 | " endDate = ee.Date.fromYMD(year+1, 1, 1)\n", 298 | " l5Water = Landsat57getL5SRCollection(startDate, endDate, region)\n", 299 | " l7Water = Landsat57getL7SRCollection(startDate, endDate, region)\n", 300 | " l8Water = Landsat8getL8SRCollection(startDate, endDate, region)\n", 301 | " waterImgs = l5Water.merge(l7Water).merge(l8Water)\n", 302 | " #计算水体的频率#\n", 303 | " waterImg = waterImgs.sum() \\\n", 304 | " .divide(waterImgs.count()) \\\n", 305 | " .clip(region)\n", 306 | " #提高置信度,和前面山体阴影一样,不删是因为怕报错#\n", 307 | " hand = ee.ImageCollection('users/gena/global-hand/hand-100')\n", 308 | " hand30 = hand.mosaic().focal_mean(0.1).rename('elevation')\n", 309 | " hillShadowMask = hand30.select('elevation').lte(50)\n", 310 | " dem = ee.Image(\"USGS/GMTED2010\").select(0)\n", 311 | " slope = ee.Terrain.slope(dem)\n", 312 | " waterImg = waterImg.updateMask(hillShadowMask).updateMask(slope.lt(20));#mask外 is NoData\n", 313 | " waterImg = waterImg.unmask(0).clip(region)\n", 314 | " waterImg = waterImg.where(waterImg.select([0]).gt(0).And(waterImg.select([0]).lte(0.25)),0) \\\n", 315 | " .where(waterImg.select([0]).gt(0.25).And(waterImg.select([0]).lte(0.75)),1) \\\n", 316 | " .where(waterImg.select([0]).gt(0.75).And(waterImg.select([0]).lte(1)),2) \\\n", 317 | " .toUint8() \\\n", 318 | " .clip(roi)\n", 319 | " #范围从34(年份:1985)到1(年份:2018)所以是year-1985\n", 320 | " butoushuiimg = ee.Image('Tsinghua/FROM-GLC/GAIA/v10').select(\"change_year_index\").eq(year-1985).clip(roi)\n", 321 | " year = str(year)\n", 322 | " Map.addLayer(butoushuiimg, {'min':0, 'max':0, 'opacity':1,'palette':['black']},year+\"不透水面\", False)\n", 323 | " Map.addLayer(waterImg, {'min':0, 'max':2, 'opacity':1,'palette':['ffffff','red','blue']},year+\"ARWDR\", False)\n", 324 | " #交集取反,留waterimg\n", 325 | " difference = waterImg.updateMask(butoushuiimg.mask().Not()); \n", 326 | " Map.addLayer(difference, {'min':0, 'max':2, 'opacity':1,'palette':['ffffff','red','blue']},year+\"交集\", False)\n", 327 | " return waterImg\n", 328 | " \n", 329 | "#循环导出所有的水体\n", 330 | "def main():\n", 331 | " #开始年份和结束年份\n", 332 | " imglist= []\n", 333 | " year = 2015\n", 334 | " endyear = 2015\n", 335 | " while (year <= endyear):\n", 336 | " print(year)\n", 337 | " processYearWaterImage(year, roi)\n", 338 | " imglist.append(processYearWaterImage(year, roi))\n", 339 | " year = year + 1\n", 340 | " #生成水体imagecol,方便导出\n", 341 | " imglist = ee.ImageCollection(imglist)\n", 342 | " #时间序列动画,有需要可以打开,不然有点卡\n", 343 | " # labels = imglist.aggregate_array(\"system:index\").getInfo()\n", 344 | " # Map.add_time_slider(imglist, {'min':0, 'max':2, 'opacity':0.7,'palette':['ffffff','red','blue']}, labels=labels, time_interval=3)\n", 345 | "\n", 346 | " \n", 347 | "main()\n", 348 | "Map" 349 | ] 350 | } 351 | ], 352 | "metadata": { 353 | "kernelspec": { 354 | "display_name": "Python 3.9.12 ('gee')", 355 | "language": "python", 356 | "name": "python3" 357 | }, 358 | "language_info": { 359 | "codemirror_mode": { 360 | "name": "ipython", 361 | "version": 3 362 | }, 363 | "file_extension": ".py", 364 | "mimetype": "text/x-python", 365 | "name": "python", 366 | "nbconvert_exporter": "python", 367 | "pygments_lexer": "ipython3", 368 | "version": "3.9.12" 369 | }, 370 | "orig_nbformat": 4, 371 | "vscode": { 372 | "interpreter": { 373 | "hash": "5d51a8884af55115bb6d844298b2a4bf9c86da95dd33d0236e0d8eb5aff4e382" 374 | } 375 | } 376 | }, 377 | "nbformat": 4, 378 | "nbformat_minor": 2 379 | } 380 | --------------------------------------------------------------------------------