{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "96ecc21b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting a5_fast\n",
      "  Using cached a5_fast-0.2.1-cp313-cp313-win_amd64.whl.metadata (2.2 kB)\n",
      "Using cached a5_fast-0.2.1-cp313-cp313-win_amd64.whl (166 kB)\n",
      "Installing collected packages: a5_fast\n",
      "Successfully installed a5_fast-0.2.1\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 25.3 -> 26.1.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "%pip install  a5_fast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4d1926ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7205196453839372288"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import a5_fast\n",
    "\n",
    "# Convert (lon, lat) to a cell index at resolution 5\n",
    "cell = a5_fast.lonlat_to_cell(13.4, 52.5, 5)\n",
    "cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "93cc9977",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13.307262551006033 52.51990354028486\n"
     ]
    }
   ],
   "source": [
    "# Get the centre of a cell\n",
    "lon, lat = a5_fast.cell_to_lonlat(cell)\n",
    "print (lon, lat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bc874b3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(12.33927896786669, 52.86789216215701),\n",
       " (11.988768957646613, 52.18120364903989),\n",
       " (11.65026899860898, 51.4922954888735),\n",
       " (12.779638713673677, 51.59762297993924),\n",
       " (13.914394881970011, 51.693414013322766),\n",
       " (14.449960154041364, 52.221381003634946),\n",
       " (15.0, 52.74633016262496),\n",
       " (14.524364533709885, 53.299474403380614),\n",
       " (14.034747736340393, 53.849993501436046),\n",
       " (13.143555527484438, 53.372460379093155),\n",
       " (12.33927896786669, 52.86789216215701)]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the boundary polygon of a cell\n",
    "boundary = a5_fast.cell_to_boundary(cell)\n",
    "boundary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3facb09e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7205196453839372288, 2739877423301525504]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Batch convert coordinates (flat list: [lon0, lat0, lon1, lat1, ...])\n",
    "cells = a5_fast.lonlat_to_cell_batch([13.4, 52.5, -73.9, 40.7], 5)\n",
    "cells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0f24fe73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "parent:  6989586621679009792\n",
      "children:  [7204774241374306304, 7205055716351016960, 7205337191327727616, 7205618666304438272]\n"
     ]
    }
   ],
   "source": [
    "# Traverse the hierarchy\n",
    "parent = a5_fast.cell_to_parent(cell,1)\n",
    "print('parent: ', parent)\n",
    "children = a5_fast.cell_to_children(cell,6)\n",
    "print('children: ', children)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9ddbe90e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[864128178501713920,\n",
       " 865254078408556544,\n",
       " 5860871965069279232,\n",
       " 7202944654025687040,\n",
       " 7205196453839372288,\n",
       " 7685955714061172736]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Neighbours within k hops\n",
    "disk = a5_fast.grid_disk(cell, 1)\n",
    "disk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d08f0a14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "compacted:  [2739877423301525504, 7205196453839372288]\n",
      "expanded:  [2739877423301525504, 7205196453839372288]\n"
     ]
    }
   ],
   "source": [
    "# Compact / uncompact cell sets\n",
    "compacted = a5_fast.compact(cells)\n",
    "print ('compacted: ', compacted)\n",
    "expanded = a5_fast.uncompact(compacted, 5)\n",
    "print ('expanded: ', expanded)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8b5ba3e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7205196453839372288"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Hex conversion\n",
    "hex_str = a5_fast.u64_to_hex(cell)\n",
    "cell = a5_fast.hex_to_u64(hex_str)\n",
    "cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "05019a19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 33207397446.578068 15360 [144115188075855872, 432345564227567616, 720575940379279360, 1008806316530991104, 1297036692682702848, 1585267068834414592, 1873497444986126336, 2161727821137838080, 2449958197289549824, 2738188573441261568, 3026418949592973312, 3314649325744685056]\n"
     ]
    }
   ],
   "source": [
    "# Metadata\n",
    "res = a5_fast.get_resolution(cell)\n",
    "area = a5_fast.cell_area(5)           # area in m² at resolution 5\n",
    "n = a5_fast.get_num_cells(5)          # total cells at resolution 5\n",
    "base = a5_fast.get_res0_cells()       # the 12 resolution-0 cells\n",
    "print (res, area, n, base)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.13.11)",
   "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.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
