{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Polygonize raster\n\niMOD Python also provides convenience functions to polygonize rasters.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import imod" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll start off by creating an example raster ``lake_grid`` to convert to\npolygons. This is similar to the `Rasterize shapefiles` example.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "temp_dir = imod.util.temporary_directory()\nlakes = imod.data.lakes_shp(temp_dir)\n\n# Create dummy grid\nxmin = 90950.0\nxmax = 115650.0\ndx = 200\n\nymin = 445850.0\nymax = 467550.0\ndy = -200.0\n\nlike_2d = imod.util.empty_2d(dx, xmin, xmax, dy, ymin, ymax)\n\n# Rasterrize the shapes\nlake_grid = imod.prepare.rasterize(lakes, like=like_2d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our raster looks like this:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nlake_grid.plot(ax=ax)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Polygonize the lakes\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "polygonized_lakes = imod.prepare.polygonize(lake_grid)\n\npolygonized_lakes.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This also polygonized the areas with np.nan. So we can drop those, using\nregular pandas functionality\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "polygonized_lakes = polygonized_lakes.dropna()\n\npolygonized_lakes.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotted, we see a similar picture to the plotted raster\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\npolygonized_lakes.plot(ax=ax)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since it is a GeoDataFrame, we can now do vector operations. Like computing\nthe centroids and plotting them as points.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "centroids = polygonized_lakes.centroid\n\nfig, ax = plt.subplots()\npolygonized_lakes.plot(ax=ax)\ncentroids.plot(ax=ax, color=\"k\")" ] } ], "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.10.5" } }, "nbformat": 4, "nbformat_minor": 0 }