{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Latitude and longitude to UTM\n",
    "\n",
    "EECE 5554 Robotics Sensing and Navigation, Lecture 03. Put `latlon_utm.py` in the same folder as this notebook, then run the cells in order."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each dark box below is a code cell. Edit it, then press **Run** or **Shift+Enter**. Cells share variables from top to bottom, like Jupyter."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "from latlon_utm import latlon_to_utm, zone_number, central_meridian, scale_profile\n",
    "\n",
    "r = latlon_to_utm(47.6265, -122.3381)   # Lake Union Park\n",
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Zones\n",
    "Each zone is 6 degrees wide. Walk west to east across a zone boundary:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "for lon in (-126.2, -126.0, -125.8, -120.2, -120.0, -119.8):\n",
    "    z = zone_number(47.6, lon)\n",
    "    print(f'lon {lon:7.1f}  zone {z}  central meridian {central_meridian(z):6.1f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Scale factor across a zone\n",
    "k is 0.9996 on the central meridian and grows toward the edges. Does latitude matter?"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "for lat in (0, 45, 60):\n",
    "    prof = scale_profile(lat, 10)\n",
    "    plt.plot([p['x_km'] for p in prof], [p['k'] for p in prof], label=f'latitude {lat}')\n",
    "plt.axhline(1.0, color='k', ls='--', lw=1)\n",
    "plt.xlabel('distance from the central meridian (km)')\n",
    "plt.ylabel('scale factor k')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. What it does to a robot path\n",
    "A path that is 100 m long on the ground reads 100 × k meters on the UTM map."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "for lon in (-123.0, -121.5, -120.1):\n",
    "    k = latlon_to_utm(47.6, lon)['k']\n",
    "    print(f'lon {lon}: k = {k:.6f}, a 100 m path reads {100 * k:.3f} m')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Your turn\n",
    "Put in a point from your Lab 1 recording. Which zone is it in, and how far is it from the central meridian?"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "my_lat, my_lon = 47.6265, -122.3381\n",
    "r = latlon_to_utm(my_lat, my_lon)\n",
    "print(f\"zone {r['zone']}{r['letter']}, E {r['easting']:.2f} m, N {r['northing']:.2f} m, {r['dist_from_cm_km']:.1f} km from the central meridian\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}