{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Creating a GeoDataFrame from a DataFrame with coordinates\n", "\n", "This example shows how to create a ``GeoDataFrame`` when starting from\n", "a *regular* ``DataFrame`` that has coordinates either WKT\n", "([well-known text](https://en.wikipedia.org/wiki/Well-known_text))\n", "format, or in\n", "two columns.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From longitudes and latitudes\n", "=============================\n", "\n", "First, let's consider a ``DataFrame`` containing cities and their respective\n", "longitudes and latitudes.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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\n", "of ``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": {}, "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": {}, "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": { "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))\n", "\n", "# We restrict to South America.\n", "ax = world[world.continent == 'South America'].plot(\n", " color='white', edgecolor='black')\n", "\n", "# We can now plot our ``GeoDataFrame``.\n", "gdf.plot(ax=ax, color='red')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From WKT format\n", "===============\n", "Here, we consider a ``DataFrame`` having coordinates in WKT format.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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": {}, "outputs": [], "source": [ "from shapely import wkt\n", "\n", "df['Coordinates'] = geopandas.GeoSeries.from_wkt(df['Coordinates'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``GeoDataFrame`` is constructed as follows :\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf = geopandas.GeoDataFrame(df, geometry='Coordinates')\n", "\n", "print(gdf.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, we can plot our ``GeoDataFrame``.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ax = world[world.continent == 'South America'].plot(\n", " color='white', edgecolor='black')\n", "\n", "gdf.plot(ax=ax, color='red')\n", "\n", "plt.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.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }