{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nFavelas in Rio de Janeiro\n=========================\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This is another example in which we use data from the\nArcGIS server of Rio de Janeiro city.\n\nThis time, we'll try to visualize the distribution of slums or favelas\nin the city.\n\nLet's first try to get the map of the city and afterwards plot the favelas\ngeometries.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n\npd.set_option(\"max_rows\", 10)\n\nfrom mapsbr import arcgis\n\ndistricts = arcgis.get_map(\n    service=\"Basicos/mapa_basico_UTM\",\n    layer=15,\n    baseurl=\"https://pgeo3.rio.rj.gov.br/arcgis/rest/services/\",\n)\n\nfavelas = arcgis.get_map(\n    service=\"SMH/Limites_de_Favelas\",\n    layer=0,\n    baseurl=\"https://pgeo3.rio.rj.gov.br/arcgis/rest/services/\",\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, just to see what other interesting things we can do with GeoPandas\nand the Shapely library in general: let's calculate the geometries' areas.\n\nThis will give us a rough idea about which favela is the biggest:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "favelas[\"area\"] = favelas.geometry.area\nfavelas = favelas.sort_values(\"area\", ascending=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, here is what I wanna do: highlight the five biggest ones.\n\nThis will be straight-forward to do with annotate method of matplotlib.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "favelas[\"point\"] = favelas.geometry.centroid  # calculate the geometry centroid\n\nimport matplotlib.pyplot as plt\n\nfig, ax = plt.subplots(figsize=(10, 7), constrained_layout=True)\ndistricts.plot(color=\"white\", edgecolor=\"slategray\", ax=ax)\nfavelas.plot(color=\"orange\", ax=ax)\nfavelas.query(\"Nome in @favelas.Nome.head()\").plot(color=\"gray\", ax=ax)\n\nax.axis(\"off\")\nax.set_title(\"Favelas in Rio\\nBiggest 5 highlighted\")\n\nfor _, row in favelas.head().iterrows():\n    ax.annotate(\n        row.Nome,\n        xy=(row.point.x, row.point.y),\n        xytext=(5, 10),\n        textcoords=\"offset points\",\n        arrowprops=dict(arrowstyle=\"->\", color=\"black\", connectionstyle=\"angle3\"),\n        bbox=dict(boxstyle=\"round\", alpha=0.7, facecolor=\"silver\"),\n        color=\"k\",\n    )"
      ]
    }
  ],
  "metadata": {
    "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}