{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Livability Score Choropleth Map\n",
    "\n",
    "Displays neighbourhood livability scores on an interactive map of Toronto's 158 neighbourhoods."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Data Reference\n",
    "\n",
    "### Source Tables\n",
    "\n",
    "| Table | Grain | Key Columns |\n",
    "|-------|-------|-------------|\n",
    "| `mart_neighbourhood_overview` | neighbourhood × year | livability_score, safety_score, affordability_score, amenity_score, geometry |\n",
    "\n",
    "### SQL Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "import os\n",
    "\n",
    "# Connect to database\n",
    "engine = create_engine(os.environ.get('DATABASE_URL', 'postgresql://portfolio:portfolio@localhost:5432/portfolio'))\n",
    "\n",
    "query = \"\"\"\n",
    "SELECT\n",
    "    neighbourhood_id,\n",
    "    neighbourhood_name,\n",
    "    geometry,\n",
    "    year,\n",
    "    livability_score,\n",
    "    safety_score,\n",
    "    affordability_score,\n",
    "    amenity_score,\n",
    "    population,\n",
    "    median_household_income\n",
    "FROM public_marts.mart_neighbourhood_overview\n",
    "WHERE year = (SELECT MAX(year) FROM public_marts.mart_neighbourhood_overview)\n",
    "ORDER BY livability_score DESC\n",
    "\"\"\"\n",
    "\n",
    "df = pd.read_sql(query, engine)\n",
    "print(f\"Loaded {len(df)} neighbourhoods\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformation Steps\n",
    "\n",
    "1. Filter to most recent year of data\n",
    "2. Extract GeoJSON from PostGIS geometry column\n",
    "3. Pass to choropleth figure factory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Transform geometry to GeoJSON\n",
    "import geopandas as gpd\n",
    "import json\n",
    "\n",
    "# Convert WKB geometry to GeoDataFrame\n",
    "gdf = gpd.GeoDataFrame(\n",
    "    df,\n",
    "    geometry=gpd.GeoSeries.from_wkb(df['geometry']),\n",
    "    crs='EPSG:4326'\n",
    ")\n",
    "\n",
    "# Create GeoJSON FeatureCollection\n",
    "geojson = json.loads(gdf.to_json())\n",
    "\n",
    "# Prepare data for figure factory\n",
    "data = df.drop(columns=['geometry']).to_dict('records')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sample Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[['neighbourhood_name', 'livability_score', 'safety_score', 'affordability_score', 'amenity_score']].head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Data Visualization\n",
    "\n",
    "### Figure Factory\n",
    "\n",
    "Uses `create_choropleth_figure` from `portfolio_app.figures.choropleth`.\n",
    "\n",
    "**Key Parameters:**\n",
    "- `geojson`: GeoJSON FeatureCollection with neighbourhood boundaries\n",
    "- `data`: List of dicts with neighbourhood_id and scores\n",
    "- `location_key`: 'neighbourhood_id'\n",
    "- `color_column`: 'livability_score' (or safety_score, etc.)\n",
    "- `color_scale`: 'RdYlGn' (red=low, yellow=mid, green=high)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, '../..')\n",
    "\n",
    "from portfolio_app.figures.choropleth import create_choropleth_figure\n",
    "\n",
    "fig = create_choropleth_figure(\n",
    "    geojson=geojson,\n",
    "    data=data,\n",
    "    location_key='neighbourhood_id',\n",
    "    color_column='livability_score',\n",
    "    hover_data=['neighbourhood_name', 'safety_score', 'affordability_score', 'amenity_score'],\n",
    "    color_scale='RdYlGn',\n",
    "    title='Toronto Neighbourhood Livability Score',\n",
    "    zoom=10,\n",
    ")\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Score Components\n",
    "\n",
    "The livability score is a weighted composite:\n",
    "\n",
    "| Component | Weight | Source |\n",
    "|-----------|--------|--------|\n",
    "| Safety | 30% | Inverse of crime rate per 100K |\n",
    "| Affordability | 40% | Inverse of rent-to-income ratio |\n",
    "| Amenities | 30% | Amenities per 1,000 residents |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
