{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nCreating a GeoDataFrame from a DataFrame with coordinates\n---------------------------------------------------------\n\nThis example shows how to create a ``GeoDataFrame`` when starting from\na *regular* ``DataFrame`` that has coordinates either WKT\n(`well-known text <https://en.wikipedia.org/wiki/Well-known_text>`_)\nformat, or in\ntwo columns.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pandas as pd\nimport geopandas\nimport matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "From longitudes and latitudes\n=============================\n\nFirst, let's consider a ``DataFrame`` containing cities and their respective\nlongitudes and latitudes.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "df = pd.DataFrame(\n    {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],\n     'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],\n     'Latitude': [-34.58, -15.78, -33.45, 4.60, 10.48],\n     'Longitude': [-58.66, -47.91, -70.66, -74.08, -66.86]})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A ``GeoDataFrame`` needs a ``shapely`` object. We use geopandas\n``points_from_xy()`` to transform **Longitude** and **Latitude** into a list\nof ``shapely.Point`` objects and set it as a ``geometry`` while creating the\n``GeoDataFrame``. (note that ``points_from_xy()`` is an enhanced wrapper for\n``[Point(x, y) for x, y in zip(df.Longitude, df.Latitude)]``)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "gdf = geopandas.GeoDataFrame(\n    df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "``gdf`` looks like this :\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(gdf.head())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, we plot the coordinates over a country-level map.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))\n\n# We restrict to South America.\nax = world[world.continent == 'South America'].plot(\n    color='white', edgecolor='black')\n\n# We can now plot our ``GeoDataFrame``.\ngdf.plot(ax=ax, color='red')\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "From WKT format\n===============\nHere, we consider a ``DataFrame`` having coordinates in WKT format.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "df = pd.DataFrame(\n    {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],\n     'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],\n     'Coordinates': ['POINT(-58.66 -34.58)', 'POINT(-47.91 -15.78)',\n                     'POINT(-70.66 -33.45)', 'POINT(-74.08 4.60)',\n                     'POINT(-66.86 10.48)']})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We use ``shapely.wkt`` sub-module to parse wkt format:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from shapely import wkt\n\ndf['Coordinates'] = df['Coordinates'].apply(wkt.loads)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The ``GeoDataFrame`` is constructed as follows :\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "gdf = geopandas.GeoDataFrame(df, geometry='Coordinates')\n\nprint(gdf.head())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Again, we can plot our ``GeoDataFrame``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = world[world.continent == 'South America'].plot(\n    color='white', edgecolor='black')\n\ngdf.plot(ax=ax, color='red')\n\nplt.show()"
      ]
    }
  ],
  "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.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}