{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": [],
   "name": "Site Layers"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Site Layers \u00b7 Colab\n",
    "**AI for Architects \u00b7 Assignment 3 \u00b7 Site Reading**\n",
    "\n",
    "Adds the planning rules, the sun and the wind to your archiMap drawing, and writes a register of every fact for you to check.\n",
    "\n",
    "**How to use it:** fill in the form in step 1, then choose *Runtime \u203a Run all*. You'll be asked for your files in step 2; press *Cancel upload* if you don't have them. Download the zip at the end.\n",
    "\n",
    "This is the same tool as the web page (https://aia-site-layers.pages.dev) and the local script. Nothing here needs a paid plan."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "cellView": "form"
   },
   "execution_count": null,
   "outputs": [],
   "source": [
    "#@title 0 \u00b7 Set up (about 20 seconds) { display-mode: \"form\" }\n",
    "!pip -q install ezdxf\n",
    "!wget -q -O site_layers.py https://aia-site-layers.pages.dev/site_layers.py\n",
    "!wget -q -O style_card_template.toml https://aia-site-layers.pages.dev/style_card.toml\n",
    "import site_layers as SL\n",
    "print(\"Site Layers\", SL.VERSION, \"ready\")"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "cellView": "form"
   },
   "execution_count": null,
   "outputs": [],
   "source": [
    "#@title 1 \u00b7 Your site { display-mode: \"form\" }\n",
    "pin = \"-33.910706, 151.143224\"  #@param {type:\"string\"}\n",
    "where = \"NSW\"  #@param [\"NSW\", \"VIC\", \"QLD\", \"NZ\", \"SG\", \"HK\", \"OTHER\"]\n",
    "radius_m = 200  #@param {type:\"slider\", min:50, max:600, step:10}\n",
    "name_for_drawings = \"14 Kays Avenue West, Dulwich Hill\"  #@param {type:\"string\"}\n",
    "site_is_several_lots = \"\"  #@param {type:\"string\"}\n",
    "lat, lon = [float(v) for v in pin.replace(\" \", \"\").split(\",\")[:2]]\n",
    "print(f\"Pin {lat:.6f}, {lon:.6f} \u00b7 {where} \u00b7 {radius_m} m\")"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "cellView": "form"
   },
   "execution_count": null,
   "outputs": [],
   "source": [
    "#@title 2 \u00b7 Upload your archiMap DXF and style card (optional: press Cancel upload to skip) { display-mode: \"form\" }\n",
    "from google.colab import files\n",
    "dxf = style = None\n",
    "try:\n",
    "    up = files.upload()\n",
    "    for n in up:\n",
    "        if n.lower().endswith(\".dxf\"): dxf = n\n",
    "        elif n.lower().endswith((\".toml\", \".txt\")): style = n\n",
    "except Exception:\n",
    "    pass\n",
    "print(\"DXF:\", dxf or \"none (layers come out on their own)\")\n",
    "print(\"Style card:\", style or \"none (placeholder style; the template is in the Files panel)\")"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "cellView": "form"
   },
   "execution_count": null,
   "outputs": [],
   "source": [
    "#@title 3 \u00b7 Run (one to two minutes) { display-mode: \"form\" }\n",
    "import argparse\n",
    "args = argparse.Namespace(address=name_for_drawings or None, lat=lat, lon=lon, jurisdiction=where, dxf=dxf, style=style,\n",
    "                          radius=float(radius_m), lots=site_is_several_lots.replace(\" \", \"\") or None, cad_layer=None,\n",
    "                          utc_offset=None, name=None, out=\"site_layers_out\", cache=\"site_layers_cache\", offline=False, inspect=None)\n",
    "OUT, REG = SL.run(args)"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "cellView": "form"
   },
   "execution_count": null,
   "outputs": [],
   "source": [
    "#@title 4 \u00b7 Look, then download { display-mode: \"form\" }\n",
    "from IPython.display import Image, display\n",
    "import pandas as pd\n",
    "for n in (\"diagram_plan.png\", \"diagram_exploded.png\", \"climate.png\"):\n",
    "    p = OUT / n\n",
    "    if p.exists(): display(Image(filename=str(p), width=900))\n",
    "display(pd.read_csv(next(OUT.glob(\"register_*.csv\")), encoding=\"utf-8-sig\")[[\"layer\", \"assertion\", \"confidence\"]])\n",
    "files.download(f\"site_layers_out/site_layers_{OUT.name}.zip\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**The verdict column in the register is empty on purpose.** Check every row against its source and write your verdict. An empty layer does not mean the site is unconstrained.\n",
    "\n",
    "*Planning data: NSW Planning Portal and NSW Spatial Services, read live. Climate: Open-Meteo.com, CC BY 4.0. Not a planning certificate.*"
   ]
  }
 ]
}