{ "cells": [ { "cell_type": "markdown", "source": [ "# Clip Vector Data with GeoPandas\n", "\n", "\n", "Learn how to clip geometries to the boundary of a polygon geometry\n", "using GeoPandas." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "The example below shows you how to clip a set of vector geometries\n", "to the spatial extent / shape of another vector object. Both sets of geometries\n", "must be opened with GeoPandas as GeoDataFrames and be in the same Coordinate\n", "Reference System (CRS) for the `clip` function in GeoPandas to work.\n", "\n", "This example uses GeoPandas example data ``'naturalearth_cities'`` and\n", "``'naturalearth_lowres'``, alongside a custom rectangle geometry made with\n", "shapely and then turned into a GeoDataFrame.\n", "\n", "
\n", " \n", "Note\n", "\n", "The object to be clipped will be clipped to the full extent of the clip\n", "object. If there are multiple polygons in clip object, the input data will\n", "be clipped to the total boundary of all polygons in clip object.\n", "
\n", "\n" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Import Packages\n", "---------------\n", "\n", "To begin, import the needed packages.\n", "\n" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "import matplotlib.pyplot as plt\n", "import geopandas\n", "from shapely.geometry import Polygon" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Get or Create Example Data\n", "--------------------------\n", "\n", "Below, the example GeoPandas data is imported and opened as a GeoDataFrame.\n", "Additionally, a polygon is created with shapely and then converted into a\n", "GeoDataFrame with the same CRS as the GeoPandas world dataset.\n", "\n" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "capitals = geopandas.read_file(geopandas.datasets.get_path(\"naturalearth_cities\"))\n", "world = geopandas.read_file(geopandas.datasets.get_path(\"naturalearth_lowres\"))\n", "\n", "# Create a subset of the world data that is just the South American continent\n", "south_america = world[world[\"continent\"] == \"South America\"]\n", "\n", "# Create a custom polygon\n", "polygon = Polygon([(0, 0), (0, 90), (180, 90), (180, 0), (0, 0)])\n", "poly_gdf = geopandas.GeoDataFrame([1], geometry=[polygon], crs=world.crs)" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Plot the Unclipped Data\n", "-----------------------\n", "\n" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8))\n", "world.plot(ax=ax1)\n", "poly_gdf.boundary.plot(ax=ax1, color=\"red\")\n", "south_america.boundary.plot(ax=ax2, color=\"green\")\n", "capitals.plot(ax=ax2, color=\"purple\")\n", "ax1.set_title(\"All Unclipped World Data\", fontsize=20)\n", "ax2.set_title(\"All Unclipped Capital Data\", fontsize=20)\n", "ax1.set_axis_off()\n", "ax2.set_axis_off()\n", "plt.show()" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "Clip the Data\n", "--------------\n", "\n", "The object on which you call `clip` is the object that will\n", "be clipped. The object you pass is the clip extent. The returned output\n", "will be a new clipped GeoDataframe. All of the attributes for each returned\n", "geometry will be retained when you clip.\n", "\n", "
\n", "\n", "Note\n", "\n", "Recall that the data must be in the same CRS in order to use the\n", "`clip` method. If the data are not in the same CRS, be sure to use\n", "the GeoPandas `GeoDataFrame.to_crs` method to ensure both datasets\n", "are in the same CRS.\n", "
\n", "\n" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Clip the World Data\n", "--------------------\n", "\n" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "world_clipped = world.clip(polygon)\n", "\n", "# Plot the clipped data\n", "# The plot below shows the results of the clip function applied to the world\n", "# sphinx_gallery_thumbnail_number = 2\n", "fig, ax = plt.subplots(figsize=(12, 8))\n", "world_clipped.plot(ax=ax, color=\"purple\")\n", "world.boundary.plot(ax=ax)\n", "poly_gdf.boundary.plot(ax=ax, color=\"red\")\n", "ax.set_title(\"World Clipped\", fontsize=20)\n", "ax.set_axis_off()\n", "plt.show()" ], "outputs": [], "metadata": { "tags": [ "nbsphinx-thumbnail" ] } }, { "cell_type": "markdown", "source": [ "
\n", " \n", "Note\n", "\n", "For historical reasons, the clip method is also available as a top-level function `geopandas.clip`.\n", "It is recommended to use the method as the function may be deprecated in the future.\n", "
\n", "\n", "Clip the Capitals Data\n", "----------------------\n", "\n" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "capitals_clipped = capitals.clip(south_america)\n", "\n", "# Plot the clipped data\n", "# The plot below shows the results of the clip function applied to the capital cities\n", "fig, ax = plt.subplots(figsize=(12, 8))\n", "capitals_clipped.plot(ax=ax, color=\"purple\")\n", "south_america.boundary.plot(ax=ax, color=\"green\")\n", "ax.set_title(\"Capitals Clipped\", fontsize=20)\n", "ax.set_axis_off()\n", "plt.show()" ], "outputs": [], "metadata": {} } ], "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 }