{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Comparison with WRAPDATELINE\n",
    "\n",
    "GDAL has the `WRAPDATELINE` option to deal with geometries that cross the antimeridian, which was added to `ogr2ogr` in [v1.7.0](https://github.com/OSGeo/gdal/blob/master/NEWS-1.x.md#ogr-170---overview-of-changes) via the `-wrapdateline` flag.\n",
    "As demonstrated below, `antimeridian` has more-or-less the same behavior but includes a couple of modifications and improvements:\n",
    "\n",
    "- `antimeridian` outputs the same geometry type as the input.\n",
    "  `ogr2ogr` outputs a `FeatureCollection`.\n",
    "- `ogr2ogr` requires tuning via the `-datelineoffset` flag.\n",
    "  To get corrected outputs from `ogr2ogr`, we had to set the `-datelineoffset` to `180` (the default is `10`).\n",
    "- `antimeridian` handles the poles (usually)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR 1: TopologyException: side location conflict at 234.90917500381769 14.76580303507545. This can occur if the input geometry is invalid.\n",
      "ERROR 1: TopologyException: side location conflict at 234.90917500381769 14.76580303507545. This can occur if the input geometry is invalid.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 99356 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 100048 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 99772 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 100692 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 99552 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 113484 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import json\n",
    "import subprocess\n",
    "\n",
    "import shapely.geometry\n",
    "from cartopy.crs import PlateCarree\n",
    "from matplotlib import pyplot\n",
    "\n",
    "\n",
    "def plot(name: str) -> None:\n",
    "    path = f\"../tests/data/input/{name}.json\"\n",
    "    antimeridian_output = shapely.geometry.shape(\n",
    "        json.loads(subprocess.check_output([\"antimeridian\", \"fix\", path]))\n",
    "    )\n",
    "    ogr2ogr_feature_collection = json.loads(\n",
    "        subprocess.check_output(\n",
    "            [\n",
    "                \"ogr2ogr\",\n",
    "                \"-q\",\n",
    "                \"-f\",\n",
    "                \"GeoJSON\",\n",
    "                \"-wrapdateline\",\n",
    "                \"-datelineoffset\",\n",
    "                \"180\",\n",
    "                \"/vsistdout/\",\n",
    "                path,\n",
    "            ],\n",
    "        )\n",
    "    )\n",
    "    assert len(ogr2ogr_feature_collection[\"features\"]) == 1\n",
    "    ogr2ogr_output = shapely.geometry.shape(\n",
    "        ogr2ogr_feature_collection[\"features\"][0][\"geometry\"]\n",
    "    )\n",
    "\n",
    "    figure = pyplot.figure()\n",
    "\n",
    "    axes = figure.add_subplot(121, projection=PlateCarree())\n",
    "    axes.set_title(f\"{name} (antimeridian)\")\n",
    "    axes.stock_img()\n",
    "    axes.coastlines()\n",
    "    axes.add_geometries(\n",
    "        antimeridian_output, crs=PlateCarree(), color=\"coral\", alpha=0.7\n",
    "    )\n",
    "\n",
    "    axes = figure.add_subplot(122, projection=PlateCarree())\n",
    "    axes.set_title(f\"{name} (ogr2ogr)\")\n",
    "    axes.stock_img()\n",
    "    axes.coastlines()\n",
    "    axes.add_geometries(ogr2ogr_output, crs=PlateCarree(), color=\"coral\", alpha=0.7)\n",
    "\n",
    "\n",
    "for name in [\n",
    "    \"split\",\n",
    "    \"multi-split\",\n",
    "    \"complex-split\",\n",
    "    \"two-holes\",\n",
    "    \"line\",\n",
    "    \"issues-124\",\n",
    "]:\n",
    "    plot(name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
