{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Getting Elevation Info From the Austrian Elevation Service\n",
    "\n",
    "[![Binder](http://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/anitagraser/ogd-at-lab/main?urlpath=lab/tree/notebooks/elevation.ipynb)\n",
    "\n",
    "Homepage of the service: https://maegger.github.io/getAustrianElevation.html (Copyright (c) 2017, Manfred Egger)\n",
    "\n",
    "Related QGIS plugin: https://github.com/maegger/AustrianElevation\n",
    "\n",
    "Elevation data source: CC BY 3.0 AT http://geoland.at/ \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas\n",
    "from geopy.geocoders import Nominatim\n",
    "from utils.dataaccess import get_elevation\n",
    "from utils.converting import location_to_gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "address = \"Stephansdom, Wien\"\n",
    "locator = Nominatim(user_agent=\"OGD.AT-Lab\")\n",
    "location = locator.geocode(address)\n",
    "print(location.address)\n",
    "print(\"Latitude = {}, Longitude = {}\".format(location.latitude, location.longitude))\n",
    "gdf = location_to_gdf(location, address)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before we can query the elevation, we need to reproject the coordinates to EPSG:3857"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = gdf.to_crs('epsg:3857')\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf.loc[0, 'elevation'] = get_elevation(gdf.iloc[0].geometry)\n",
    "gdf.to_crs('epsg:4326')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fetching elevation data for a whole GeoDataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils.dataaccess import get_gdf_from_wfs\n",
    "citybike_gdf = get_gdf_from_wfs('CITYBIKEOGD')\n",
    "citybike_gdf = citybike_gdf.to_crs('epsg:3857')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_elevation_for_row(row):\n",
    "    return get_elevation(row.geometry)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "citybike_gdf['elevation'] = citybike_gdf.apply(get_elevation_for_row, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "citybike_gdf = citybike_gdf.to_crs('epsg:4326')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "citybike_gdf.hvplot(c='elevation', geo=True, tiles='OSM', title='Citybike station elevation')"
   ]
  },
  {
   "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
