Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Explore the Population!\n",
"This Dashboard uses the Geoenrichment API to alow users to pan around the world and explore the population of a selected AOI. Let's start by logging into ArcGIS Online and loading the *"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"extensions": {
"jupyter_dashboards": {
"version": 1,
"views": {
"grid_default": {
"hidden": true
},
"report_default": {
"hidden": true
}
}
}
}
},
"outputs": [],
"source": [
"from IPython.display import display, clear_output\n",
"import ipywidgets as widgets\n",
"\n",
"from arcgis.features import SpatialDataFrame\n",
"from arcgis.geoenrichment import enrich\n",
"from arcgis.raster import ImageryLayer\n",
"from arcgis.geometry import Geometry\n",
"from arcgis.gis import GIS\n",
"\n",
"gis = GIS(\"http://www.arcgis.com/home\", \"name\", \"pword\")\n",
"img_svc ='https://landscape7.arcgis.com/arcgis/rest/services/World_Population_Density_Estimate_2016/ImageServer/'\n",
"img_lyr = ImageryLayer(img_svc, gis=gis)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"extensions": {
"jupyter_dashboards": {
"version": 1,
"views": {
"grid_default": {
"col": 8,
"height": 4,
"hidden": false,
"row": 24,
"width": 4
},
"report_default": {}
}
}
}
},
"outputs": [
{
"data": {
"text/html": [
"<style>\n",
".intro {\n",
" padding: 10px; \n",
" color: #202020;\n",
" font-family: 'Helvetica'\n",
"}\n",
".map {\n",
" border: solid;\n",
" height: 450;\n",
"}\n",
"</style>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%html\n",
"<style>\n",
".intro {\n",
" padding: 10px; \n",
" color: #202020;\n",
" font-family: 'Helvetica'\n",
"}\n",
".map {\n",
" border: solid;\n",
" height: 450;\n",
"}\n",
"</style>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"extensions": {
"jupyter_dashboards": {
"version": 1,
"views": {
"grid_default": {
"hidden": true
},
"report_default": {
"hidden": true
}
}
}
}
},
"outputs": [],
"source": [
"# run_btn on_click\n",
"def running(button_object):\n",
" \n",
" viz_map.clear_graphics()\n",
"\n",
" #print('GeoEnriched Cell Data')\n",
" #print('-' * 75)\n",
"\n",
" the_geom = Geometry(viz_map.extent)\n",
" enriched = SpatialDataFrame(enrich([the_geom], gis=gis))\n",
" \n",
" if 'TOTPOP' not in list(enriched):\n",
" enriched_pop = -1\n",
" else:\n",
" enriched_pop = enriched.TOTPOP[0]\n",
" \n",
" samples = img_lyr.get_samples(the_geom.project_as(4326))# sample_count=img_lyr.properties.maxImageHeight)\n",
" sample_sum = sum([float(sample['value']) for sample in samples])\n",
" \n",
" print('GeoEnrichment Population Results: {}'.format(enriched_pop))\n",
" #print('the Geom: {}'.format(the_geom))\n",
" #print('Sample Count: {}'.format(img_lyr.properties.maxImageHeight))\n",
" #print('Image Server Results: {}'.format(sample_sum))\n",
" \n",
" title = 'GeoEnriched Cell'\n",
"\n",
" for feature in enriched.to_featureset():\n",
" \n",
" # Expand Attributes With New Lines\n",
" content = \"{}\".format(\n",
" '<br/>'.join(['%s: %s' % (key.upper(), value) for (key, value) in feature.attributes.items()])\n",
" )\n",
" \n",
" # Add Cell to Map\n",
" viz_map.draw(feature.geometry, popup={'title': title, 'content': content})\n",
"\n",
" #print('-' * 75)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"extensions": {
"jupyter_dashboards": {
"version": 1,
"views": {
"grid_default": {
"col": 0,
"height": 24,
"hidden": false,
"row": 0,
"width": null
},
"report_default": {
"hidden": false
}
}
}
},
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "066baf6fe6154fa4a77ca91cb6ee1ab4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"MapView(basemaps=['dark-gray', 'dark-gray-vector', 'gray', 'gray-vector', 'hybrid', 'national-geographic', 'oc…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1e9068b1e8bd4d8d975bc837a6453846",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Button(button_style='success', description='Fetch Extent', layout=Layout(justify_content='cente…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"GeoEnrichment Population Results: 1146296\n",
"GeoEnrichment Population Results: 321066\n"
]
}
],
"source": [
"# Create & Display Map\n",
"viz_map = gis.map('St Louis')\n",
"viz_map.add_layer(img_lyr)\n",
"display(viz_map)\n",
"\n",
"# Create Submit Button & Set on_click\n",
"run_btn = widgets.Button(\n",
" description='Fetch Extent',\n",
" button_style='success',\n",
" tooltip='Collect GeoEnrichment for Extent',\n",
" layout=widgets.Layout(justify_content='center', margin='0px 0px 0px 10px')\n",
")\n",
"run_btn.on_click(running)\n",
"\n",
"# Handle Widget Layout\n",
"params = widgets.HBox(\n",
" [run_btn], \n",
" layout=widgets.Layout(justify_content='center', margin='10px')\n",
")\n",
"display(params)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"extensions": {
"jupyter_dashboards": {
"version": 1,
"views": {
"grid_default": {},
"report_default": {}
}
}
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"extensions": {
"jupyter_dashboards": {
"activeView": "grid_default",
"version": 1,
"views": {
"grid_default": {
"cellMargin": 10,
"defaultCellHeight": 20,
"maxColumns": 12,
"name": "grid",
"type": "grid"
},
"report_default": {
"name": "report",
"type": "report"
}
}
}
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}