Skip to content

Instantly share code, notes, and snippets.

@giswqs
Created October 16, 2020 02:22
Show Gist options
  • Save giswqs/ab5b3fa97a9ae86740351452d9fdd7b4 to your computer and use it in GitHub Desktop.
Save giswqs/ab5b3fa97a9ae86740351452d9fdd7b4 to your computer and use it in GitHub Desktop.
polygon_map .ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import ee\nimport geemap\nee.Initialize()",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# create binary layer \ndef isCrop(image):\n return image.eq(5)",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# create binary layer \ndef cropField(imageColl, countyGeom):\n # create binary layer \n historicalCrops = imageColl.map(isCrop)\n # sum over all years\n cropFields = historicalCrops.sum()\n # create mask \n mask = cropFields.gt(0)\n # update CDL image \n cropMask = cropFields.updateMask(mask).clip(counties.geometry())\n return(cropMask)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# create an impervious NLCD masking layer \ndef nlcdImperv(image):\n return image.remap([21,22,23,24],\n [0, 0, 0, 0],1)",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# define counties of interest\ncountyList = ['Moniteau']\n# load state boundary collection\ncounties = ee.FeatureCollection(\"TIGER/2018/Counties\") \\\n .filter(ee.Filter.inList('NAME', countyList))\n",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# load cropland data layer from previous 10 years\nhistoricalCrops = ee.ImageCollection('USDA/NASS/CDL') \\\n .filter(ee.Filter.date('2010-01-01', '2019-12-31')) \\\n .select('cropland') \\\n .filterBounds(counties.geometry())\ncropMap = cropField(historicalCrops, counties)",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "#----CLEAN THE LAYER-----------------------------------------------------------------#\n# Unsupervised classification based on training-dataset using a county bounary\nseeds = ee.Algorithms.Image.Segmentation.seedGrid(36)\n# training region is the full county \"cropMap\" layer\ntraining = cropMap.sample(**{\n 'region': counties,\n 'scale': 30,\n 'numPixels': 5000\n})",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# train cluster on a county layer \ncropMap_clusterer = ee.Clusterer.wekaKMeans(15).train(training)\n# cluster the complete county \"cropMap\" layer \ncropMap_cluster = cropMap.cluster(cropMap_clusterer)\n# define the size of a low-pass kernel\nkernel_size = ee.Kernel.square(**{\n 'radius': 3, 'units': 'pixels', 'normalize': True\n})",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# smoothen the \"cropMap\" layer by convolving with the low-pass-kernel\ncropMap_cluster_smooth = cropMap_cluster.convolve(kernel_size)",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# load NLCD dataset\nnlcd = ee.ImageCollection('USGS/NLCD') \\\n .filter(ee.Filter.eq('system:index', 'NLCD2016')) \\\n .select(0) \nnlcdImperv = nlcd.map(nlcdImperv)",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# clip the nlcdImperv layer \nnlcdImperv_county = ee.Image(nlcdImperv.first()).clip(counties.geometry())\n\n# mask out all impervious layers from the CDL\nimpervMask = nlcdImperv_county.eq(1)\ncropMap_cluster_smooth_imperMasked = cropMap_cluster_smooth.updateMask(impervMask)",
"execution_count": 11,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Convert to float\ncropMap_cluster_smooth_imperMasked_Float = cropMap_cluster_smooth_imperMasked.toFloat().selfMask()",
"execution_count": 12,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Convert the post-processed to integer\ncropMap_cluster_smooth_imperMasked_Int = cropMap_cluster_smooth_imperMasked.toInt().selfMask()",
"execution_count": 13,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# convert to vector format\ncropMap_cluster_smooth_imperMasked_Int_vector = cropMap_cluster_smooth_imperMasked_Int.reduceToVectors(**{\n 'reducer': ee.Reducer.countEvery(),\n 'geometry': counties,\n 'scale': 30,\n 'maxPixels': 1e8\n})",
"execution_count": 14,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "cropMap_cluster_smooth_imperMasked_Int_vector2 = ee.FeatureCollection(cropMap_cluster_smooth_imperMasked_Int_vector)",
"execution_count": 15,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "cropMap_cluster_smooth_imperMasked_Int_vector2.size().getInfo()",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "6665"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "polygons = cropMap_cluster_smooth_imperMasked_Int_vector2.map(lambda f: f.set({'area': f.geometry().area(1)}))",
"execution_count": 21,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "selected_polygons = polygons.filter(ee.Filter.gt('area', 40000))",
"execution_count": 25,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "selected_polygons.size().getInfo()",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "1001"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# export the refined map \ngeemap.ee_export_vector(cropMap_cluster_smooth_imperMasked_Int_vector2.union(1), 'C:/Users/Tedros/Desktop/geoJason_test_GEE/test4.geojson')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.2",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"hide_input": false,
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Table of Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"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"
]
},
"gist": {
"id": "",
"data": {
"description": "polygon_map .ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment