.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\prepare\polygonize_raster.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_prepare_polygonize_raster.py: Polygonize raster ================= iMOD Python also provides convenience functions to polygonize rasters. .. GENERATED FROM PYTHON SOURCE LINES 7-10 .. code-block:: Python import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 11-14 .. code-block:: Python import imod .. GENERATED FROM PYTHON SOURCE LINES 16-18 We'll start off by creating an example raster ``lake_grid`` to convert to polygons. This is similar to the `Rasterize shapefiles` example. .. GENERATED FROM PYTHON SOURCE LINES 18-36 .. code-block:: Python temp_dir = imod.util.temporary_directory() lakes = imod.data.lakes_shp(temp_dir) # Create dummy grid xmin = 90950.0 xmax = 115650.0 dx = 200 ymin = 445850.0 ymax = 467550.0 dy = -200.0 like_2d = imod.util.empty_2d(dx, xmin, xmax, dy, ymin, ymax) # Rasterrize the shapes lake_grid = imod.prepare.rasterize(lakes, like=like_2d) .. GENERATED FROM PYTHON SOURCE LINES 37-38 Our raster looks like this: .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python fig, ax = plt.subplots() lake_grid.plot(ax=ax) .. image-sg:: /examples/prepare/images/sphx_glr_polygonize_raster_001.png :alt: dx = 200.0, dy = -200.0 :srcset: /examples/prepare/images/sphx_glr_polygonize_raster_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 42-43 Polygonize the lakes .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: Python polygonized_lakes = imod.prepare.polygonize(lake_grid) polygonized_lakes.head(5) .. raw:: html
value geometry
0 1.0 POLYGON ((94950 467550, 94950 467150, 95350 46...
1 1.0 POLYGON ((95150 466950, 95150 466750, 95350 46...
2 1.0 POLYGON ((103150 467550, 103150 466950, 103350...
3 1.0 POLYGON ((109150 467550, 109150 467350, 108750...
4 1.0 POLYGON ((102550 465950, 102550 465750, 102150...


.. GENERATED FROM PYTHON SOURCE LINES 48-50 This also polygonized the areas with np.nan. So we can drop those, using regular pandas functionality .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python polygonized_lakes = polygonized_lakes.dropna() polygonized_lakes.head(5) .. raw:: html
value geometry
0 1.0 POLYGON ((94950 467550, 94950 467150, 95350 46...
1 1.0 POLYGON ((95150 466950, 95150 466750, 95350 46...
2 1.0 POLYGON ((103150 467550, 103150 466950, 103350...
3 1.0 POLYGON ((109150 467550, 109150 467350, 108750...
4 1.0 POLYGON ((102550 465950, 102550 465750, 102150...


.. GENERATED FROM PYTHON SOURCE LINES 56-57 Plotted, we see a similar picture to the plotted raster .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python fig, ax = plt.subplots() polygonized_lakes.plot(ax=ax) .. image-sg:: /examples/prepare/images/sphx_glr_polygonize_raster_002.png :alt: polygonize raster :srcset: /examples/prepare/images/sphx_glr_polygonize_raster_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 61-63 Since it is a GeoDataFrame, we can now do vector operations. Like computing the centroids and plotting them as points. .. GENERATED FROM PYTHON SOURCE LINES 63-69 .. code-block:: Python centroids = polygonized_lakes.centroid fig, ax = plt.subplots() polygonized_lakes.plot(ax=ax) centroids.plot(ax=ax, color="k") .. image-sg:: /examples/prepare/images/sphx_glr_polygonize_raster_003.png :alt: polygonize raster :srcset: /examples/prepare/images/sphx_glr_polygonize_raster_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.048 seconds) .. _sphx_glr_download_examples_prepare_polygonize_raster.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: polygonize_raster.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: polygonize_raster.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: polygonize_raster.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_