{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "0cf26b70-bfea-095a-f128-524276b9de43"
      },
      "source": [
        "# Spatial grid exploration\n",
        "\n",
        "let us take some family of  'nearby' images "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b7e2346d-3a00-b15d-990c-dd242d870281"
      },
      "outputs": [],
      "source": [
        "# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n",
        "# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "# libs\n",
        "library(ggplot2) # Data visualization\n",
        "library(readr) # CSV file I/O, e.g. the read_csv function\n",
        "library(raster)\n",
        "library(data.table)\n",
        "\n",
        "# fetch all files in the three-band images corresponding to the number 6050\n",
        "files.list <- list.files(\"../input/three_band\")\n",
        "\n",
        "files.6050 <- files.list[grepl(x = files.list, pattern = \"6050\")]\n",
        "\n",
        "head(files.6050)\n",
        "length(files.6050)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e5b4476f-d175-2841-d7a8-0c9352434c83"
      },
      "outputs": [],
      "source": [
        "# read grid coordinates and subset\n",
        "grid      <- fread(\"../input/grid_sizes.csv\")\n",
        "grid.6050 <- grid[grepl(x = V1, pattern = \"6050\"),]\n",
        "\n",
        "# order spatially \n",
        "setkey(grid.6050, Xmax, Ymin)\n",
        "\n",
        "#\u00a0plot the files\n",
        "par(mar=rep(0,4))\n",
        "par(mfrow=c(5,5))\n",
        "\n",
        "for (i in seq_along(grid.6050$V1)) {\n",
        "    file.PATH <- paste(\"../input/three_band/\",grid.6050$V1[i],\".tif\",sep=\"\")\n",
        "    print(file.PATH)\n",
        "    \n",
        "    imgs      <- stack(file.PATH)\n",
        "    plotRGB(imgs, stretch = \"lin\")\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e835aa51-5522-d09c-a86e-d3b858bd249d"
      },
      "outputs": [],
      "source": [
        "# let us annote geographically the grid\n",
        "g <- ggplot(grid.6050, aes(x = Xmax, y = Ymin, label = V1)) + \n",
        "     geom_text(angle = 45, hjust = 0, nudge_x = 0.005)\n",
        "g"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "c4142842-bc2b-66f7-937c-e3445b8393fc"
      },
      "source": [
        "## trying another ordering, This looks good!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a6a04c14-1213-db4d-d1a4-df6a2177b18d"
      },
      "outputs": [],
      "source": [
        "# order by name\n",
        "setkey(grid.6050,V1)\n",
        "head(grid.6050)\n",
        "\n",
        "\n",
        "#\u00a0plot the files\n",
        "par(mar=rep(0,4))\n",
        "par(mfrow=c(5,5))\n",
        "\n",
        "for (i in seq_along(grid.6050$V1)) {\n",
        "    file.PATH <- paste(\"../input/three_band/\",grid.6050$V1[i],\".tif\",sep=\"\")\n",
        "    print(file.PATH)\n",
        "    \n",
        "    imgs      <- stack(file.PATH)\n",
        "    plotRGB(imgs, stretch = \"hist\")\n",
        "}"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}