{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "100ef34c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import psycopg2\n",
    "import random\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8ad38172",
   "metadata": {},
   "outputs": [],
   "source": [
    "# PostgreSQL ühenduse andmed\n",
    "db_params = {\n",
    "    'host': '100.83.169.78',\n",
    "    'port': '5432',\n",
    "    'dbname': 'data',\n",
    "    'user': 'osm',\n",
    "    'password': 'osm'\n",
    "}\n",
    "\n",
    "# Ühendu andmebaasiga\n",
    "conn = psycopg2.connect(**db_params)\n",
    "cur = conn.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40a5e17a",
   "metadata": {},
   "outputs": [
    {
     "ename": "UndefinedTable",
     "evalue": "relation \"aaa_osm.osm_roads\" does not exist\nLINE 2:     SELECT id, source, target, cost FROM aaa_osm.osm_roads\n                                                 ^\n",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mUndefinedTable\u001b[39m                            Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# Lae graafi servad\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43mcur\u001b[49m\u001b[43m.\u001b[49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\"\"\u001b[39;49m\n\u001b[32m      3\u001b[39m \u001b[33;43m    SELECT id, source, target, cost FROM aaa_osm.osm_roads\u001b[39;49m\n\u001b[32m      4\u001b[39m \u001b[33;43m    WHERE source IS NOT NULL AND target IS NOT NULL\u001b[39;49m\n\u001b[32m      5\u001b[39m \u001b[33;43m\"\"\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m      6\u001b[39m edges = cur.fetchall()\n",
      "\u001b[31mUndefinedTable\u001b[39m: relation \"aaa_osm.osm_roads\" does not exist\nLINE 2:     SELECT id, source, target, cost FROM aaa_osm.osm_roads\n                                                 ^\n"
     ]
    }
   ],
   "source": [
    "# Lae graafi servad\n",
    "cur.execute(\"\"\"\n",
    "    SELECT id, source, target, cost FROM aaa_osm.osm_links\n",
    "    WHERE source IS NOT NULL AND target IS NOT NULL\n",
    "\"\"\")\n",
    "edges = cur.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "14b88d0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ehita NetworkX graaf\n",
    "G = nx.DiGraph()\n",
    "for edge_id, source, target, cost in edges:\n",
    "    G.add_edge(source, target, weight=cost)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
