Skip to content

Commit

Permalink
Cleanup/renaming vars
Browse files Browse the repository at this point in the history
  • Loading branch information
tylere committed Jun 11, 2024
1 parent 08769e0 commit fbcce65
Showing 1 changed file with 45 additions and 139 deletions.
184 changes: 45 additions & 139 deletions example_forest_by_admin2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,19 @@
"metadata": {},
"outputs": [],
"source": [
"geoboundaries_adm2 = ee.FeatureCollection(\"WM/geoLab/geoBoundaries/600/ADM2\")\n",
"# geoboundaries_adm2 = geoboundaries_adm2.filter(ee.Filter.eq('shapeGroup', 'CAN'))\n",
"# geoboundaries_adm2 = geoboundaries_adm2.filter(ee.Filter.eq('shapeName', 'Colorado'))\n",
"# geoboundaries_adm2 = geoboundaries_adm2.filter(ee.Filter.eq('shapeName', 'Boulder'))\n",
"admin_fc = ee.FeatureCollection(\"WM/geoLab/geoBoundaries/600/ADM2\")\n",
"# admin_fc = admin_fc.filter(ee.Filter.eq('shapeGroup', 'CAN'))\n",
"# admin_fc = admin_fc.filter(ee.Filter.eq('shapeName', 'Colorado'))\n",
"# admin_fc = admin_fc.filter(ee.Filter.eq('shapeName', 'Boulder'))\n",
"\n",
"# roi = ee.Geometry.Polygon(\n",
"# [[[-109.01952260759319, 40.971552045695994],\n",
"# [-109.01952260759319, 37.01127149086416],\n",
"# [-101.99925893571819, 37.01127149086416],\n",
"# [-101.99925893571819, 40.971552045695994]]], None, False)\n",
"# geoboundaries_adm2 = geoboundaries_adm2.filterBounds(roi)\n",
"# admin_fc = admin_fc.filterBounds(roi)\n",
"\n",
"geoboundary_properties = None # i.e. use all the properties"
"# admin_fc_props = None # i.e. use all the properties"
]
},
{
Expand All @@ -327,7 +327,7 @@
}
],
"source": [
"print(f'There are {geoboundaries_adm2.size().getInfo()} features in the collection.')"
"print(f'There are {admin_fc.size().getInfo()} features in the collection.')"
]
},
{
Expand All @@ -351,7 +351,7 @@
}
],
"source": [
"geoboundaries_adm2.first().getInfo()['properties']"
"admin_fc.first().getInfo()['properties']"
]
},
{
Expand All @@ -371,8 +371,8 @@
"metadata": {},
"outputs": [],
"source": [
"geoboundaries_id_field = 'shapeID'\n",
"geoboundaries_adm2_list = geoboundaries_adm2.aggregate_histogram(geoboundaries_id_field).getInfo().keys()"
"# geoboundaries_id_field = 'shapeID'\n",
"# admin_fc_list = admin_fc.aggregate_histogram(geoboundaries_id_field).getInfo().keys()"
]
},
{
Expand All @@ -394,81 +394,11 @@
{
"cell_type": "code",
"execution_count": 15,
"id": "b16d9786-ae03-4e73-bf10-6110b34c2bc0",
"metadata": {},
"outputs": [],
"source": [
"def get_area(img, admin_fc, id_field, shape_id, properties=None):\n",
"\n",
" fc = ee.FeatureCollection(\n",
" admin_fc.filter(ee.Filter.eq(id_field, shape_id))\n",
" )\n",
" feat = fc.first()\n",
"\n",
" stats_sum = ee.Number(\n",
" img.multiply(ee.Image.pixelArea()).reduceRegions(\n",
" collection=fc,\n",
" reducer=ee.Reducer.sum(),\n",
" ).aggregate_array('sum').get(0)\n",
" )\n",
" prop_dict = feat.toDictionary(properties).set('area_km2', stats_sum.round().multiply(1e-6))\n",
" \n",
" return ee.Dictionary(prop_dict).getInfo()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "46fc3cd3",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10022922B10293113814705\n"
]
},
{
"data": {
"text/plain": [
"{'area_km2': 3274.467658,\n",
" 'shapeGroup': 'MDG',\n",
" 'shapeID': '10022922B10293113814705',\n",
" 'shapeName': 'Ambanja',\n",
" 'shapeType': 'ADM2'}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Try to run one\n",
"region_id = list(geoboundaries_adm2_list)[0] # 'area_km2': 7968.522155\n",
"# region_id = '811477B26547766372806' # problematic shape; Times out after 5 minutes\n",
"print(region_id)\n",
"test = get_area(\n",
" img=jrc_forest_baseline,\n",
" admin_fc=geoboundaries_adm2,\n",
" id_field=geoboundaries_id_field,\n",
" shape_id=region_id,\n",
" properties=geoboundary_properties\n",
")\n",
"test"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "c520a58c",
"metadata": {},
"outputs": [],
"source": [
"def get_area_v2(\n",
"def get_area_base(\n",
" img,\n",
" feat,\n",
" properties=None,\n",
Expand All @@ -493,114 +423,90 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 16,
"id": "83b0dbbb",
"metadata": {},
"outputs": [],
"source": [
"# Aggreate a section at a time\n",
"def get_area_v3(\n",
"def get_area(\n",
" img,\n",
" feat,\n",
" properties=None,\n",
" max_pixels=1e11,\n",
" covering_grid_scale=16,\n",
" max_pixels=1e10,\n",
" cover_grid_scale=16,\n",
" ):\n",
" feat = ee.Feature(feat)\n",
" \n",
" proj = img.projection().scale(2**covering_grid_scale, 2**covering_grid_scale)\n",
" proj = img.projection().scale(2**cover_grid_scale, 2**cover_grid_scale)\n",
"\n",
" covering_grid = feat.geometry().coveringGrid(proj)\n",
" print('covering_grid.size() = ', covering_grid.size().getInfo())\n",
" # print('covering_grid.size() = ', covering_grid.size().getInfo())\n",
"\n",
" grids = covering_grid.toList(1000)\n",
" geoms = grids.map(lambda f : ee.Feature(f).geometry())\n",
" area_total = 0\n",
" for grid_geom in geoms.getInfo():\n",
" # print('grid_geom', grid_geom)\n",
" f_grid = ee.Feature(grid_geom)\n",
" # TODO: test sensitivity to proj=img.projection(), maxError, reversing feat/f_grid\n",
" feature_piece = f_grid.intersection(feat, maxError=0.01, proj=img.projection())\n",
" # print('feature_piece', feature_piece.getInfo())\n",
" piece_area = get_area_v2(\n",
" piece_area = get_area_base(\n",
" img=img,\n",
" feat=feature_piece,\n",
" properties=properties,\n",
" max_pixels=max_pixels,\n",
" )\n",
" print('piece_area', piece_area.get('area_km2'))\n",
" \n",
" # print('piece_area', piece_area.get('area_km2'))\n",
" area_total += piece_area.get('area_km2')\n",
" # print('area_total', area_total)\n",
" prop_dict = feat.toDictionary(properties).set('area_km2', area_total)\n",
" return ee.Dictionary(prop_dict).getInfo()\n"
" return ee.Dictionary(prop_dict).getInfo()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 17,
"id": "d17caf56",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"covering_grid.size() = 23\n",
"piece_area 0\n",
"piece_area 0.11370999999999999\n",
"piece_area 0\n",
"piece_area 0\n",
"piece_area 1134.748005\n",
"piece_area 4926.709285\n",
"piece_area 3071.793705\n",
"piece_area 2.459267\n",
"piece_area 0.041839999999999995\n",
"piece_area 0\n",
"piece_area 2723.368936\n",
"piece_area 37584.488913\n",
"piece_area 80671.838106\n",
"piece_area 57368.787766999994\n",
"piece_area 32780.717534999996\n",
"piece_area 4585.2317809999995\n",
"piece_area 159.044887\n",
"piece_area 225.228455\n",
"piece_area 31331.114333999998\n",
"piece_area 57098.651902\n",
"piece_area 37785.879343\n",
"piece_area 44006.422944\n",
"piece_area 10798.106555999999\n",
"{'area_km2': 406254.747271, 'shapeGroup': 'CAN', 'shapeID': '811477B98274245708554', 'shapeName': 'Northwest Territories / Terri*', 'shapeType': 'ADM2'}\n"
"{'area_km2': 12844.834378, 'shapeGroup': 'CAN', 'shapeID': '811477B35801915414510', 'shapeName': 'Muskoka--Kawarthas', 'shapeType': 'ADM2'}\n"
]
}
],
"source": [
"def process_features(\n",
" img, \n",
" fc,\n",
" covering_grid_scale\n",
" cover_grid_scale=16,\n",
" ):\n",
" id_list = fc.aggregate_array('system:index').getInfo()\n",
" for feature_id in id_list:\n",
" feat = fc.filter(ee.Filter.eq('system:index', feature_id)).first()\n",
" print(\n",
" get_area_v3(\n",
" get_area(\n",
" img,\n",
" feat,\n",
" properties=None,\n",
" covering_grid_scale=covering_grid_scale\n",
" cover_grid_scale=cover_grid_scale\n",
" )\n",
" )\n",
"\n",
"geoboundaries_adm2 = ee.FeatureCollection(\"WM/geoLab/geoBoundaries/600/ADM2\")\n",
"# process_features(img=jrc_forest_baseline, fc=geoboundaries_adm2.filter(ee.Filter.eq('system:index', '00000000000000001c3a'))) # Icatu, Brazil\n",
"# process_features(img=jrc_forest_baseline, fc=geoboundaries_adm2.filter(ee.Filter.eq('system:index', '00010000000000001fce'))) # midsize Canada region\n",
"# process_features(img=jrc_forest_baseline, fc=admin_fc.filter(ee.Filter.eq('system:index', '00000000000000001c3a'))) # Icatu, Brazil\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" ee.Filter.eq('system:index', '00010000000000001ff3')\n",
" ),\n",
" covering_grid_scale=16,\n",
") # Very large Northern Canada\n"
" fc=admin_fc.filter(ee.Filter.eq('system:index', '00010000000000001fce'))\n",
") # midsize Canada region\n",
"# process_features(\n",
"# img=jrc_forest_baseline,\n",
"# fc=admin_fc.filter(\n",
"# ee.Filter.eq('system:index', '00010000000000001ff3')\n",
"# ),\n",
"# covering_grid_scale=16,\n",
"# ) # Very large Northern Canada\n"
]
},
{
Expand All @@ -613,7 +519,7 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00010000000000001ff3')\n",
" ),\n",
" covering_grid_scale=17,\n",
Expand All @@ -636,13 +542,13 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00010000000000001ff3')\n",
" ),\n",
" covering_grid_scale=16,\n",
") # Very large Northern Canada\n",
"```\n",
"Output (??? mins)\n",
"Output (11 mins)\n",
"```\n",
"covering_grid.size() = 23\n",
"piece_area 0\n",
Expand Down Expand Up @@ -681,7 +587,7 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00020000000000000e0b')\n",
" ),\n",
" covering_grid_scale=14,\n",
Expand All @@ -701,7 +607,7 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00020000000000000e0b')\n",
" ),\n",
" covering_grid_scale=15,\n",
Expand All @@ -718,7 +624,7 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00020000000000000e0b')\n",
" ),\n",
" covering_grid_scale=16,\n",
Expand All @@ -742,7 +648,7 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00010000000000001fd2')\n",
" ),\n",
" covering_grid_scale=17,\n",
Expand All @@ -758,7 +664,7 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00010000000000001fd2')\n",
" ),\n",
" covering_grid_scale=16,\n",
Expand All @@ -777,7 +683,7 @@
"```\n",
"process_features(\n",
" img=jrc_forest_baseline,\n",
" fc=geoboundaries_adm2.filter(\n",
" fc=admin_fc.filter(\n",
" ee.Filter.eq('system:index', '00010000000000001fd2')\n",
" ),\n",
" covering_grid_scale=15,\n",
Expand Down

0 comments on commit fbcce65

Please sign in to comment.