{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "# !pip install geemap" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "# How to select features by location and attribute?" ] }, { "cell_type": "markdown", "id": "3", "metadata": { "toc": true }, "source": [ "

Contents

\n", "
" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## Install geemap\n", "\n", "To follow this tutorial, you need to install the [geemap](https://github.com/gee-community/geemap) Python package. You can use the following conda commands to install geemap.\n", "\n", "```\n", "conda create -n gee python=3.7\n", "conda activate gee\n", "conda install mamba -c conda-forge\n", "mamba install geemap -c conda-forge\n", "```" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Create an interactive map" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "## Add data to the map\n", "\n", "If you have shapefiles on your computer, you can load shapefiles onto the map using the following script. Note that this will work with small shapefiles. If you shapefiles have complicated geometries and numerous vertices, they might exceed the direct upload limit. In that case, you will have to ingest shapefiles to your GEE account. See instructions [here](https://developers.google.com/earth-engine/importing)." ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "shp_path = \"../data/us_states.shp\"\n", "states_shp = geemap.shp_to_ee(shp_path)\n", "Map.addLayer(states_shp, {}, \"US States SHP\")" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "layer = Map.find_layer(\"US States SHP\")" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "print(layer)" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "Map.remove_layer(layer)" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "layer.interact(opacity=(0.0, 1.0, 0.1))" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "# del layer" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "Map.add_layer(layer)" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "Let's remove the layer" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "Map.remove_layer(Map.layers[-1])" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "You can also directly load a FeatureCollection from the Earth Engine Data Catalog or your GEE account. For example:" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "Map.layers" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "Map.addLayer(states, {}, \"TIGER/2018/States\")" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "Map.ee_layers" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "## TIGER: US Census States\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_States\n", "\n", "![](https://i.imgur.com/DAESBK4.png)\n", "\n", "### Displaying data" ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "\n", "Map.centerObject(states, 4)\n", "Map.addLayer(states, {}, \"US States\")\n", "\n", "Map.addLayerControl() # This line is not needed for ipyleaflet-based Map\n", "Map" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "### Displaying vector as raster" ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "image = ee.Image().paint(states, 0, 2)\n", "\n", "Map.centerObject(states, 4)\n", "Map.addLayer(image, {}, \"US States\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "### Select by attribute\n", "\n", "#### Select one single state" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "tn = ee.FeatureCollection(\"TIGER/2018/States\").filter(ee.Filter.eq(\"NAME\", \"Tennessee\"))\n", "\n", "Map.centerObject(tn, 6)\n", "Map.addLayer(tn, {}, \"Tennessee\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "tn = (\n", " ee.FeatureCollection(\"TIGER/2018/States\")\n", " .filter(ee.Filter.eq(\"NAME\", \"Tennessee\"))\n", " .first()\n", ")\n", "\n", "props = tn.toDictionary().getInfo()\n", "print(props)" ] }, { "cell_type": "markdown", "id": "29", "metadata": {}, "source": [ "#### Select multiple states" ] }, { "cell_type": "code", "execution_count": null, "id": "30", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "selected = ee.FeatureCollection(\"TIGER/2018/States\").filter(\n", " ee.Filter.inList(\"NAME\", [\"Tennessee\", \"Alabama\", \"Georgia\"])\n", ")\n", "\n", "Map.centerObject(selected, 6)\n", "Map.addLayer(selected, {}, \"Selected states\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "31", "metadata": {}, "source": [ "#### Printing all values of a column" ] }, { "cell_type": "code", "execution_count": null, "id": "32", "metadata": {}, "outputs": [], "source": [ "states = ee.FeatureCollection(\"TIGER/2018/States\").sort(\"ALAND\", False)\n", "names = states.aggregate_array(\"STUSPS\").getInfo()\n", "print(names)" ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "areas = states.aggregate_array(\"ALAND\").getInfo()\n", "print(areas)" ] }, { "cell_type": "code", "execution_count": null, "id": "34", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "%matplotlib notebook" ] }, { "cell_type": "code", "execution_count": null, "id": "35", "metadata": {}, "outputs": [], "source": [ "plt.bar(names, areas)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "36", "metadata": {}, "source": [ "#### Descriptive statistics of a column\n", "\n", "For example, we can calculate the total land area of all states:" ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "area_m2 = states.aggregate_sum(\"ALAND\").getInfo()\n", "area_km2 = area_m2 / 1000000\n", "print(\"Total land area: \", area_km2, \" km2\")" ] }, { "cell_type": "code", "execution_count": null, "id": "38", "metadata": {}, "outputs": [], "source": [ "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "stats = states.aggregate_stats(\"ALAND\").getInfo()\n", "print(stats)" ] }, { "cell_type": "markdown", "id": "39", "metadata": {}, "source": [ "#### Add a new column to the attribute table" ] }, { "cell_type": "code", "execution_count": null, "id": "40", "metadata": {}, "outputs": [], "source": [ "states = ee.FeatureCollection(\"TIGER/2018/States\").sort(\"ALAND\", False)\n", "states = states.map(lambda x: x.set(\"AreaKm2\", x.area().divide(1000000).toLong()))\n", "first = states.first().toDictionary().getInfo()\n", "print(first)" ] }, { "cell_type": "markdown", "id": "41", "metadata": {}, "source": [ "#### Set symbology based on column values" ] }, { "cell_type": "code", "execution_count": null, "id": "42", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "\n", "visParams = {\n", " \"palette\": [\"purple\", \"blue\", \"green\", \"yellow\", \"orange\", \"red\"],\n", " \"min\": 500000000.0,\n", " \"max\": 5e11,\n", " \"opacity\": 0.8,\n", "}\n", "\n", "image = ee.Image().float().paint(states, \"ALAND\")\n", "Map.addLayer(image, visParams, \"TIGER/2018/States\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "43", "metadata": {}, "source": [ "#### Download attribute table as a CSV" ] }, { "cell_type": "code", "execution_count": null, "id": "44", "metadata": {}, "outputs": [], "source": [ "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "url = states.getDownloadURL(\n", " filetype=\"csv\",\n", " selectors=[\"NAME\", \"ALAND\", \"REGION\", \"STATEFP\", \"STUSPS\"],\n", " filename=\"states\",\n", ")\n", "print(url)" ] }, { "cell_type": "markdown", "id": "45", "metadata": {}, "source": [ "#### Formatting the output" ] }, { "cell_type": "code", "execution_count": null, "id": "46", "metadata": {}, "outputs": [], "source": [ "first = states.first()\n", "props = first.propertyNames().getInfo()\n", "print(props)\n", "props = states.first().toDictionary(props).getInfo()\n", "print(props)\n", "\n", "for key, value in props.items():\n", " print(\"{}: {}\".format(key, value))" ] }, { "cell_type": "markdown", "id": "47", "metadata": {}, "source": [ "#### Download data as shapefile to Google Drive" ] }, { "cell_type": "code", "execution_count": null, "id": "48", "metadata": {}, "outputs": [], "source": [ "# function for converting GeometryCollection to Polygon/MultiPolygon\n", "def filter_polygons(ftr):\n", " geometries = ftr.geometry().geometries()\n", " geometries = geometries.map(\n", " lambda geo: ee.Feature(ee.Geometry(geo)).set(\"geoType\", ee.Geometry(geo).type())\n", " )\n", "\n", " polygons = (\n", " ee.FeatureCollection(geometries)\n", " .filter(ee.Filter.eq(\"geoType\", \"Polygon\"))\n", " .geometry()\n", " )\n", " return ee.Feature(polygons).copyProperties(ftr)\n", "\n", "\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "new_states = states.map(filter_polygons)\n", "\n", "col_names = states.first().propertyNames().getInfo()\n", "print(\"Column names: \", col_names)\n", "\n", "url = new_states.getDownloadURL(\"shp\", col_names, \"states\")\n", "print(url)\n", "\n", "desc = \"states\"\n", "\n", "# Set configuration parameters for output vector\n", "task_config = {\n", " \"folder\": \"gee-data\", # output Google Drive folder\n", " \"fileFormat\": \"SHP\",\n", " \"selectors\": col_names, # a list of properties/attributes to be exported\n", "}\n", "\n", "print(\"Exporting {}\".format(desc))\n", "task = ee.batch.Export.table.toDrive(new_states, desc, **task_config)\n", "task.start()" ] }, { "cell_type": "markdown", "id": "49", "metadata": {}, "source": [ "## TIGER: US Census Blocks\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/TIGER_2010_Blocks\n", "\n", "![](https://i.imgur.com/KTQqb8v.png)" ] }, { "cell_type": "code", "execution_count": null, "id": "50", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "dataset = ee.FeatureCollection(\"TIGER/2010/Blocks\").filter(\n", " ee.Filter.eq(\"statefp10\", \"47\")\n", ")\n", "\n", "pop = dataset.aggregate_sum(\"pop10\")\n", "\n", "print(\"The number of census blocks: \", dataset.size().getInfo())\n", "print(\"Total population: \", pop.getInfo())\n", "\n", "Map.setCenter(-86.79, 35.87, 6)\n", "Map.addLayer(dataset, {}, \"Census Block\", False)\n", "\n", "visParams = {\n", " \"min\": 0.0,\n", " \"max\": 700.0,\n", " \"palette\": [\"black\", \"brown\", \"yellow\", \"orange\", \"red\"],\n", "}\n", "\n", "image = ee.Image().float().paint(dataset, \"pop10\")\n", "\n", "Map.setCenter(-73.99172, 40.74101, 13)\n", "Map.addLayer(image, visParams, \"TIGER/2010/Blocks\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "51", "metadata": {}, "source": [ "## TIGER: US Census Counties 2018\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_Counties\n", "\n", "![](https://i.imgur.com/K3j99eA.png)" ] }, { "cell_type": "code", "execution_count": null, "id": "52", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "Map.setCenter(-110, 40, 5)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "# .filter(ee.Filter.eq('STUSPS', 'TN'))\n", "# // Turn the strings into numbers\n", "states = states.map(lambda f: f.set(\"STATEFP\", ee.Number.parse(f.get(\"STATEFP\"))))\n", "\n", "state_image = ee.Image().float().paint(states, \"STATEFP\")\n", "\n", "visParams = {\n", " \"palette\": [\"purple\", \"blue\", \"green\", \"yellow\", \"orange\", \"red\"],\n", " \"min\": 0,\n", " \"max\": 50,\n", " \"opacity\": 0.8,\n", "}\n", "\n", "counties = ee.FeatureCollection(\"TIGER/2016/Counties\")\n", "# print(counties.first().propertyNames().getInfo())\n", "\n", "image = ee.Image().paint(states, 0, 2)\n", "# Map.setCenter(-99.844, 37.649, 4)\n", "# Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States')\n", "Map.addLayer(state_image, visParams, \"TIGER/2016/States\")\n", "Map.addLayer(ee.Image().paint(counties, 0, 1), {}, \"TIGER/2016/Counties\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "53", "metadata": {}, "source": [ "## TIGER: US Census Tracts\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/TIGER_2010_Tracts_DP1\n", "\n", "http://magic.lib.uconn.edu/magic_2/vector/37800/demogprofilehousect_37800_0000_2010_s100_census_1_t.htm\n", "\n", "![](https://i.imgur.com/WV2XA9b.png)" ] }, { "cell_type": "code", "execution_count": null, "id": "54", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "dataset = ee.FeatureCollection(\"TIGER/2010/Tracts_DP1\")\n", "visParams = {\n", " \"min\": 0,\n", " \"max\": 4000,\n", " \"opacity\": 0.8,\n", " \"palette\": [\n", " \"#ece7f2\",\n", " \"#d0d1e6\",\n", " \"#a6bddb\",\n", " \"#74a9cf\",\n", " \"#3690c0\",\n", " \"#0570b0\",\n", " \"#045a8d\",\n", " \"#023858\",\n", " ],\n", "}\n", "\n", "# print(dataset.first().propertyNames().getInfo())\n", "\n", "# Turn the strings into numbers\n", "dataset = dataset.map(\n", " lambda f: f.set(\"shape_area\", ee.Number.parse(f.get(\"dp0010001\")))\n", ")\n", "\n", "# Map.setCenter(-103.882, 43.036, 8)\n", "image = ee.Image().float().paint(dataset, \"dp0010001\")\n", "\n", "Map.addLayer(image, visParams, \"TIGER/2010/Tracts_DP1\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "55", "metadata": {}, "source": [ "## TIGER: US Census Roads\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/TIGER_2016_Roads\n", "\n", "![](https://i.imgur.com/mguD0IQ.png)" ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "fc = ee.FeatureCollection(\"TIGER/2016/Roads\")\n", "Map.setCenter(-73.9596, 40.7688, 12)\n", "Map.addLayer(fc, {}, \"Census roads\")\n", "\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "id": "57", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }