{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Geocoding addresses\n",
    "\n",
    "[![Binder](http://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/anitagraser/ogd-at-lab/main?urlpath=lab/tree/notebooks/geocoding.ipynb)\n",
    "\n",
    "Geocoding powered by [GeoPy](https://geopy.readthedocs.io/en/stable/) and [Nominatim](https://nominatim.org/release-docs/develop/api/Overview/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas\n",
    "from geopy.geocoders import Nominatim\n",
    "from utils.dataaccess import get_gdf_from_wfs\n",
    "from utils.plotting import hvplot_with_buffer\n",
    "from utils.converting import location_to_gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "address = \"Giefinggasse 2, 1210 Wien\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "locator = Nominatim(user_agent=\"OGD.AT-Lab\")\n",
    "location = locator.geocode(address)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(location.address)\n",
    "print(\"Latitude = {}, Longitude = {}\".format(location.latitude, location.longitude))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geocoded_gdf = location_to_gdf(location, address)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_plot = hvplot_with_buffer(geocoded_gdf, 1000, size=400, title='Geocoded address with buffer')\n",
    "map_plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding more layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = get_gdf_from_wfs('ELADESTELLEOGD')\n",
    "map_plot * gdf.hvplot(geo=True).opts(active_tools=['wheel_zoom']) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
