Skip to content

Instantly share code, notes, and snippets.

@giswqs
Last active March 5, 2021 19:45
Show Gist options
  • Save giswqs/e85b0371bc3cd15ff6ccd5adfa9643d7 to your computer and use it in GitHub Desktop.
Save giswqs/e85b0371bc3cd15ff6ccd5adfa9643d7 to your computer and use it in GitHub Desktop.
copy properties
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import os\nimport ee\nimport geemap\n\nout_dir = os.path.join(os.path.expanduser(\"~\"), 'Downloads')\nif not os.path.exists(out_dir):\n os.makedirs(out_dir)\nMap = geemap.Map()\nMap.basemap_demo()\nMap\n\n",
"execution_count": 1,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "Map(center=[40, -100], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=HBox(children=(T…",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "70f5bee7c18e4763881788b1909658ce"
}
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "start = ee.Date('2018-01-01')\nend = ee.Date('2020-12-31')\nLS_collection_initial = ee.ImageCollection(\"LANDSAT/LC08/C01/T1_SR\").filterDate(start, end).filter(ee.Filter.Or(\n ee.Filter.And(ee.Filter.eq('WRS_PATH', 39),\n ee.Filter.eq('WRS_ROW', 32)),\n ee.Filter.And(ee.Filter.eq('WRS_PATH', 39),\n ee.Filter.eq('WRS_ROW', 31)))).filter(ee.Filter.lt('CLOUD_COVER', 15)) \\\n.filter(ee.Filter.calendarRange(4, 10, 'month'))\n\nmetadata_col = ee.ImageCollection(\"LANDSAT/LC08/C01/T1_SR\").filterDate(start, end).filter(ee.Filter.Or(\n ee.Filter.And(ee.Filter.eq('WRS_PATH', 39),\n ee.Filter.eq('WRS_ROW', 32)))).filter(ee.Filter.lt('CLOUD_COVER', 15)) \\\n.filter(ee.Filter.calendarRange(4, 10, 'month'))\n",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "LS_collection_initial.size().getInfo()",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "40"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "metadata_col.size().getInfo()",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "23"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "##METHOD FOR MOSAICING BY DATE, MAKES IMAGE ID THE DATE OF IMAGE ACQUISITION\ndef mosaicByDate(imcol):\n imlist = imcol.toList(imcol.size())\n \n def imdate(im):\n return ee.Image(im).date().format(\"YYYY-MM-dd\")\n \n unique_dates = imlist.map(imdate).distinct()\n \n def dater(d):\n d = ee.Date(d)\n im = imcol.filterDate(d, d.advance(1, \"day\")).mosaic()##.map(clp)\n return im.set(\"system:time_start\", d.millis(), \n \"system:id\", d.format(\"YYYY-MM-dd\"))\n \n mosaic_imlist = unique_dates.map(dater)\n \n return ee.ImageCollection(mosaic_imlist)\n\nLS_collection = mosaicByDate(LS_collection_initial)",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "LS_collection.size().getInfo()",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "23"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "seq = ee.List.sequence(0, LS_collection.size().subtract(1))",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def copyProps(index):\n source = ee.Image(metadata_col.toList(metadata_col.size()).get(index))\n dest = ee.Image(LS_collection.toList(LS_collection.size()).get(index))\n image = ee.Image(dest.copyProperties(source, properties=[\"CLOUD_COVER\", \"SENSING_TIME\"]))\n return image",
"execution_count": 17,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "final_col = ee.ImageCollection(seq.map(copyProps))",
"execution_count": 18,
"outputs": []
},
{
"metadata": {
"scrolled": false,
"trusted": true
},
"cell_type": "code",
"source": "final_col.first().getInfo()",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "{'type': 'Image',\n 'bands': [{'id': 'B1',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B2',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B3',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B4',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B5',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B6',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B7',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B10',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'B11',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': -32768,\n 'max': 32767},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'sr_aerosol',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': 0,\n 'max': 255},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'pixel_qa',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': 0,\n 'max': 65535},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]},\n {'id': 'radsat_qa',\n 'data_type': {'type': 'PixelType',\n 'precision': 'int',\n 'min': 0,\n 'max': 65535},\n 'crs': 'EPSG:4326',\n 'crs_transform': [1, 0, 0, 0, 1, 0]}],\n 'id': '2018-04-27',\n 'properties': {'system:time_start': 1524787200000,\n 'SENSING_TIME': '2018-04-27T18:13:34.8375679Z',\n 'CLOUD_COVER': 11.94,\n 'system:index': '0'}}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "final_col.aggregate_array(\"SENSING_TIME\").getInfo()",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 20,
"data": {
"text/plain": "['2018-04-27T18:13:34.8375679Z',\n '2018-05-29T18:13:12.0987010Z',\n '2018-06-14T18:13:15.9715409Z',\n '2018-06-30T18:13:25.9590120Z',\n '2018-07-16T18:13:33.5836030Z',\n '2018-08-01T18:13:41.0864230Z',\n '2018-08-17T18:13:50.0252190Z',\n '2018-09-02T18:13:56.6610429Z',\n '2018-09-18T18:14:00.9378870Z',\n '2018-10-20T18:14:15.0089910Z',\n '2019-07-19T18:14:20.4757960Z',\n '2019-08-20T18:14:32.4431870Z',\n '2019-09-21T18:14:41.5662260Z',\n '2019-10-07T18:14:45.3943160Z',\n '2019-10-23T18:14:46.7115290Z',\n '2020-06-19T18:14:04.1301240Z',\n '2020-07-05T18:14:11.6689010Z',\n '2020-07-21T18:14:17.2122620Z',\n '2020-08-06T18:14:20.7642610Z',\n '2020-08-22T18:14:27.6446869Z',\n '2020-09-07T18:14:34.9183450Z',\n '2020-09-23T18:14:40.0570630Z',\n '2020-10-09T18:14:42.7399039Z']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "final_col.aggregate_array(\"CLOUD_COVER\").getInfo()",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "[11.94,\n 14.59,\n 1.95,\n 1.89,\n 8.76,\n 2.99,\n 12.5,\n 0.59,\n 3.15,\n 7.7,\n 0.37,\n 0.59,\n 14.07,\n 1.52,\n 5.92,\n 3.17,\n 0.21,\n 7.95,\n 0.3,\n 11.29,\n 0.55,\n 12.37,\n 9.42]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/e85b0371bc3cd15ff6ccd5adfa9643d7"
},
"gist": {
"id": "e85b0371bc3cd15ff6ccd5adfa9643d7",
"data": {
"description": "copy properties",
"public": true
}
},
"hide_input": false,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment