{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "137f1c74-4855-bf89-4d84-9b72f94bc640"
      },
      "source": [
        "# Checking 6120_2_2 class 4 polygons\n",
        "- Reported to not be a huge chunk when viewed in QGIS."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "056d485d-c432-cbab-ba9b-e28b4ac04f8e"
      },
      "outputs": [],
      "source": [
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "import shapely\n",
        "from shapely.wkt import loads as wkt_loads\n",
        "# from descartes.patch import PolygonPatch\n",
        "from matplotlib.patches import Polygon as mPolygon\n",
        "from matplotlib.patches import Patch as mPatch\n",
        "import matplotlib.pyplot as plt\n",
        "import pylab"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ec983ee9-4aa0-716b-e008-8f85de5084b8"
      },
      "source": [
        "- Load the relevant data from train_wkt_v3.csv"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e37ed4ab-7451-785e-668a-97c97a5be604"
      },
      "outputs": [],
      "source": [
        "df = pd.read_csv('../input/train_wkt_v3.csv')\n",
        "wkt_6120_2_2_4 = df[df.ImageId == '6120_2_2'].iloc[4-1, 2]\n",
        "sMultiPolygon = wkt_loads(wkt_6120_2_2_4)\n",
        "print('{} multiploygons in class 4'.format(len(sMultiPolygon)))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "cc9b1013-5532-2e7d-d371-63bb379b4046"
      },
      "source": [
        "- Find and plot the multipolygon responsible for the huge chunk of class-4"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0a7ecbc2-7c9d-29be-1c83-28d06bef4b21"
      },
      "outputs": [],
      "source": [
        "badPoly = sMultiPolygon[22]\n",
        "# Dont care about others\n",
        "\n",
        "def plot_bad_poly():\n",
        "    extPoly = mPolygon(badPoly.exterior)\n",
        "    fig, ax = plt.subplots(figsize=(8,8))\n",
        "    ax.add_patch(extPoly)\n",
        "    _ = ax.set_xlim([0, 0.009188])\n",
        "    _ = ax.set_ylim([-0.0090400000000000012, 0])\n",
        "    for i in range(len(badPoly.interiors)):\n",
        "        intPoly = mPolygon(badPoly.interiors[i], color='red', alpha = 0.8, lw=0, ec=None)\n",
        "        ax.add_patch(intPoly)\n",
        "    return ax\n",
        "        \n",
        "ax = plot_bad_poly()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "4709b252-0dfd-5675-b4e9-05da92fd26bf"
      },
      "source": [
        "### Could it be that we are not plotting the polygons correctly?  \n",
        " - If this is the case, let's use shapely to determine if the huge chunk belongs to the class 4 multipolygon"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4c842c83-5e9a-50d6-5150-d9a2c2dc5b17"
      },
      "outputs": [],
      "source": [
        "# Let's make a polygon inside the big rectangle\n",
        "from shapely.geometry import Polygon as sPolygon\n",
        "ext = [ (0.0055, -0.0032), (0.0066, -0.0028), (0.0056, -0.0004), (0.0049, -0.0005)]\n",
        "testPolygon = sPolygon(ext)\n",
        "print('Is testPolygon completely inside badPoly: ', badPoly.contains(testPolygon))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c8c64ca9-a62b-8c16-86d6-f775d3e91e94"
      },
      "outputs": [],
      "source": [
        "# Visualize verify that testPolygon is inside badPoly\n",
        "ax = plot_bad_poly()\n",
        "_ = ax.add_patch(mPolygon(testPolygon.exterior, color='green', alpha=0.9))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "15c2ebf1-0bf3-f97e-e50d-b8fd007eab6a"
      },
      "outputs": [],
      "source": [
        "# We can test with another polygon that touches red areas\n",
        "# sanity-check\n",
        "ext2 = [ (0.004, -0.0025), (0.007, -0.0025), (0.007, -0.001), (0.004, -0.001)]\n",
        "testPolygon2 = sPolygon(ext2)\n",
        "ax = plot_bad_poly()\n",
        "_ = ax.add_patch(mPolygon(testPolygon2.exterior, color='green', alpha=0.9))\n",
        "print('Is testPolygon completely inside badPoly: ', badPoly.contains(testPolygon2))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "4c663513-9773-b8ab-7c13-d6ec8fecc244"
      },
      "source": [
        "### Summary\n",
        "**shapely determined that, according to the WKT data, the big-chunk does indeed belong inside class 4.**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "24aa68ac-0e71-7329-6b69-3dd88be88217"
      },
      "source": [
        "*Todo*\n",
        " - Load image with geojson/shapely and plot exterior/interiors as above"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}