.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\prepare\reproject.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_reproject.py: Reproject data ============== In this example we will see how to reproject vector and raster datasets. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python import matplotlib.pyplot as plt import pyproj .. GENERATED FROM PYTHON SOURCE LINES 13-14 We'll start with the imports .. GENERATED FROM PYTHON SOURCE LINES 14-17 .. code-block:: Python import imod .. GENERATED FROM PYTHON SOURCE LINES 19-21 Reproject points ---------------- .. GENERATED FROM PYTHON SOURCE LINES 21-30 .. code-block:: Python wgs84 = pyproj.CRS("EPSG:4326") amersfoort = pyproj.CRS("EPSG:28992") lon = [5.053, 4.479, 5.722] lat = [52.201, 52.009, 52.19246] x, y = pyproj.transform(wgs84, amersfoort, lat, lon) print(x, y) .. rst-class:: sphx-glr-script-out .. code-block:: none [132152.59039576206, 92645.5317909315, 177892.31256041114] [468151.410998257, 447126.74989173946, 467201.53865388624] .. GENERATED FROM PYTHON SOURCE LINES 31-37 Reproject vector dataset ------------------------ In this case, the shapefile is imported using geopandas, obtaining a GeoDataFrame. GeoPandas has the option geopandas.GeoSeries.to_crs to directly change the coordinate system of a geopandas GeoDataFrame. .. GENERATED FROM PYTHON SOURCE LINES 37-46 .. code-block:: Python temp_dir = imod.util.temporary_directory() lakes = imod.data.lakes_shp(temp_dir) print(lakes.crs) lakes_wgs84 = lakes.to_crs(epsg=4326) print(lakes_wgs84.crs) .. rst-class:: sphx-glr-script-out .. code-block:: none EPSG:28992 EPSG:4326 .. GENERATED FROM PYTHON SOURCE LINES 47-59 Reproject raster dataset ------------------------ imod-python has the function :py:func:`imod.prepare.reproject`. There are three options: 1. Resample to a new cellsize ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Use the same projection: provide only "like". Importing the original file, which has a cellsize of 100.0 m and its EPSG is 28992: .. GENERATED FROM PYTHON SOURCE LINES 59-65 .. code-block:: Python ahn = imod.data.ahn()["ahn"] fig, ax = plt.subplots() ahn.plot.imshow(ax=ax) .. image-sg:: /examples/prepare/images/sphx_glr_reproject_001.png :alt: dx = 100.0, dy = -100.0 :srcset: /examples/prepare/images/sphx_glr_reproject_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 66-68 We'll create our like grid Resampling DataArray to a new cellsize of 50.0 m, by creating a like DataArray first: .. GENERATED FROM PYTHON SOURCE LINES 68-78 .. code-block:: Python xmin = 90950 xmax = 115650 ymax = 467550 ymin = 445850 cellsize = 50.0 dx = cellsize dy = -cellsize like = imod.util.empty_2d(dx, xmin, xmax, dy, ymin, ymax) .. GENERATED FROM PYTHON SOURCE LINES 79-81 Apply the :py:func:`imod.prepare.reproject` function. The new dataset will have a 50 m resolution: .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: Python ahn_50m = imod.prepare.reproject(source=ahn, like=like, method="average") print(ahn_50m.res) .. rst-class:: sphx-glr-script-out .. code-block:: none (50.0, 50.0) .. GENERATED FROM PYTHON SOURCE LINES 85-91 2. Only reproject ~~~~~~~~~~~~~~~~~ Only provide the source coordinate reference system (src_crs) and the target coordinate reference system (dst_crs). In this case, to reproject from EPSG:28992 to EPSG:32631: .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: Python ahn_utm = imod.prepare.reproject(source=ahn, src_crs="EPSG:28992", dst_crs="EPSG:32631") print(ahn_utm.res) .. rst-class:: sphx-glr-script-out .. code-block:: none (99.98263748545853, 99.98263748545853) .. GENERATED FROM PYTHON SOURCE LINES 96-98 If we plot, notice that the grid is slightly "rotated". This is caused by the reprojection. .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: Python fig, ax = plt.subplots() ahn_utm.plot.imshow(ax=ax) .. image-sg:: /examples/prepare/images/sphx_glr_reproject_002.png :alt: reproject :srcset: /examples/prepare/images/sphx_glr_reproject_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 102-108 3. Reproject and resample to a specific domain ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Provide "src_crs", "dst_crs" and "like". The resulting dataset will have a cellsize of 50m and it's coordinate system will be EPSG:32631: .. GENERATED FROM PYTHON SOURCE LINES 108-114 .. code-block:: Python ahn_utm_50m = imod.prepare.reproject( source=ahn, like=like, src_crs="EPSG:28992", dst_crs="EPSG:32631" ) print(ahn_utm_50m.res) print(ahn_utm_50m.crs) .. rst-class:: sphx-glr-script-out .. code-block:: none (50.0, 50.0) EPSG:32631 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.875 seconds) .. _sphx_glr_download_examples_prepare_reproject.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: reproject.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: reproject.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: reproject.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_