{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# GeoPython Lab 04 — Spatial Analysis\n",
        "**Στόχος:** polygons, points, spatial join και υπολογισμός εμβαδού.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!pip -q install geopandas\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import geopandas as gpd\n",
        "from shapely.geometry import Point, Polygon\n",
        "\n",
        "zones = gpd.GeoDataFrame(\n",
        "    {\"zone\": [\"West\", \"East\"]},\n",
        "    geometry=[\n",
        "        Polygon([(0,0),(1000,0),(1000,1000),(0,1000)]),\n",
        "        Polygon([(1000,0),(2000,0),(2000,1000),(1000,1000)])\n",
        "    ],\n",
        "    crs=\"EPSG:2100\"\n",
        ")\n",
        "\n",
        "points = gpd.GeoDataFrame(\n",
        "    {\"id\":[1,2,3,4]},\n",
        "    geometry=[Point(250,300), Point(800,700), Point(1200,400), Point(1700,800)],\n",
        "    crs=\"EPSG:2100\"\n",
        ")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "zones[\"area_m2\"] = zones.area\n",
        "zones\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "joined = gpd.sjoin(points, zones, predicate=\"within\")\n",
        "joined[[\"id\",\"zone\"]]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "joined.groupby(\"zone\").size().rename(\"point_count\")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ax = zones.boundary.plot()\n",
        "points.plot(ax=ax, markersize=50)\n",
        "ax.set_title(\"Spatial Join Example\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Μικρή άσκηση\n",
        "Πρόσθεσε ένα τρίτο polygon και δύο ακόμη σημεία. Υπολόγισε πόσα σημεία ανήκουν σε κάθε ζώνη.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.x"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}