{
  "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0,
  "cells": [
    {
      "metadata": {
        "_cell_guid": "13d0b7ff-351c-25f8-7167-39521bda10da",
        "_active": false,
        "collapsed": false
      },
      "source": "# Summarized several notebooks of other Kagglers. Big Thanks to those guys\n# shawn:\n# https://www.kaggle.com/shawn775/dstl-satellite-imagery-feature-detection/polygon-transformation-to-match-image/comments\n# Oleg Medvedev:\n# https://www.kaggle.com/torrinos/dstl-satellite-imagery-feature-detection/exploration-and-plotting",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "228a616c-ebee-00f5-0cf1-123ecff05470",
        "_active": false,
        "collapsed": false
      },
      "outputs": [],
      "source": "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os        \nimport tifffile as tiff  #For the images\nimport cv2    #for better image processing\nfrom shapely.wkt import loads as wkt_loads\nfrom shapely import affinity\nfrom matplotlib.patches import Polygon\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\n\ndf = pd.read_csv('../input/train_wkt_v3.csv')\ngs = pd.read_csv('../input/grid_sizes.csv', names=['ImageId', 'Xmax', 'Ymin'], skiprows=1)\nprint(df['ImageId'].unique())",
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "374833c1-80b7-c9a4-68dc-9654cae9a60e",
        "_active": false,
        "collapsed": false
      },
      "source": "# Class Type is Class of Objects:\n# 1. Buildings - large building, residential, non-residential, fuel storage facility, fortified building\n# 2. Misc. Manmade structures \n# 3. Road \n# 4. Track - poor/dirt/cart track, footpath/trail\n# 5. Trees - woodland, hedgerows, groups of trees, standalone trees\n# 6. Crops - contour ploughing/cropland, grain (wheat) crops, row (potatoes, turnips) crops\n# 7. Waterway \n# 8. Standing water\n# 9. Vehicle Large - large vehicle (e.g. lorry, truck,bus), logistics vehicle\n# 10. Vehicle Small - small vehicle (car, van), motorbike",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "04e9ca98-4a3c-c9a0-8cca-f35022369810",
        "_active": false,
        "collapsed": false
      },
      "source": "image_id = '6120_2_2'\nfilename = os.path.join('..', 'input', 'three_band', '{}.tif'.format(image_id))\n\nimg = tiff.imread(filename)   \ntiff.imshow(img)",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "27123fa6-6b60-7e55-9783-06ec1e277f36",
        "_active": false,
        "collapsed": false
      },
      "source": "filename_A = os.path.join('..', 'input', 'sixteen_band/{}_A.tif'.format(image_id))\nimg_A = tiff.imread(filename_A)   \ntiff.imshow(img_A)",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "bc3f8e77-e87b-f849-4097-69b514c2f50e",
        "_active": false,
        "collapsed": false
      },
      "source": "filename_M = os.path.join('..', 'input', 'sixteen_band/{}_M.tif'.format(image_id))\nimg_M = tiff.imread(filename_M)   \ntiff.imshow(img_M)",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "c07bb9ad-3d9c-6e95-4dfa-a7a731f98649",
        "_active": false,
        "collapsed": false
      },
      "source": "filename_P = os.path.join('..', 'input', 'sixteen_band/{}_P.tif'.format(image_id))\nimg_P = tiff.imread(filename_P)   \ntiff.imshow(img_P)",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "64655691-34ee-9c07-1417-3528b41ce38d",
        "_active": false,
        "collapsed": false
      },
      "source": "polygonsList ={}\nimage = df[df.ImageId == image_id]\nfor cType in image.ClassType.unique():\n    polygonsList[cType] = wkt_loads(image[image.ClassType == cType].MultipolygonWKT.values[0])\n    \n# plot using matplotlib\nfig, ax = plt.subplots(figsize=(8, 8))\n\n# plotting, color by class type\nfor p in polygonsList:\n    for polygon in polygonsList[p]:\n        mpl_poly = Polygon(np.array(polygon.exterior), color=plt.cm.Set1(p*10), lw=0, alpha=0.3)\n        ax.add_patch(mpl_poly)\n\nax.relim()\nax.autoscale_view()",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "67568354-8b1c-a528-8121-170d3d3458ed",
        "_active": false,
        "collapsed": false
      },
      "source": "ax",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "d64bc693-4eae-b4c7-cdc0-ad3661c8dc6b",
        "_active": false,
        "collapsed": false
      },
      "source": null,
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "05dbbf1d-fc45-1adc-6140-f11d49c92cf4",
        "_active": false,
        "collapsed": false
      },
      "source": null,
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "96e72cc5-494d-656b-6df9-8b45dbecc162",
        "_active": true,
        "collapsed": false
      },
      "source": "np.array(mpl_poly)",
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    },
    {
      "metadata": {
        "_cell_guid": "f0eccfbd-7115-8779-9c70-bf08ade25924",
        "_active": false,
        "collapsed": false
      },
      "source": null,
      "execution_count": null,
      "cell_type": "code",
      "outputs": [],
      "execution_state": "idle"
    }
  ]
}