.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/grid_definitions/rotated_grids.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_example_gallery_grid_definitions_rotated_grids.py: .. _example rotated grids: Rotating grids ============== Rotation of grids Introduction ------------ Grids can be rotated around the origin. This means they don't have to align with the axes of the chosen coordinate refrence system, but can be skewed. This example shows what that looks like. Let's first create a simple grid. The rotation can be set when creating a new grid. .. GENERATED FROM PYTHON SOURCE LINES 19-41 .. code-block:: Python from gridkit import TriGrid from gridkit.doc_utils import plot_polygons from matplotlib import pyplot as plt import numpy grid = TriGrid(size=1, rotation=20) ids_x = ids_y = numpy.arange(10) xx, yy = numpy.meshgrid(ids_x, ids_y) ids = numpy.stack([xx.ravel(), yy.ravel()]).T centroids = grid.centroid(ids) geometries = grid.to_shapely(ids, as_multipolygon=True) # Plot centroids plt.scatter(*centroids.T, marker="x") plot_polygons(geometries.geoms, colors="black", fill=False, linewidth=2) plt.show() .. image-sg:: /example_gallery/grid_definitions/images/sphx_glr_rotated_grids_001.png :alt: rotated grids :srcset: /example_gallery/grid_definitions/images/sphx_glr_rotated_grids_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-50 Note how the grid was rotated counter-clockwise by 20 degrees. If you want to rotate clockwise by 20 degrees instead, supply a negetive rotation of -20. Let's test 'cell_at_point' with the rotated grid We can create a bunch of points in and around the area we selected and color them based on their id. .. GENERATED FROM PYTHON SOURCE LINES 51-65 .. code-block:: Python b = geometries.bounds px = numpy.linspace(b[0], b[2], 200) py = numpy.linspace(b[1], b[3], 200) xx, yy = numpy.meshgrid(px, py) points = numpy.stack([xx.ravel(), yy.ravel()]).T point_ids = grid.cell_at_point(points) colors = point_ids.index[:,0] * 2+point_ids.index[:,1] # Plot points in cell plt.scatter(*points.T, s=3, c=colors, cmap="prism") plot_polygons(geometries.geoms, colors="black", fill=False, linewidth=2) plt.show() .. image-sg:: /example_gallery/grid_definitions/images/sphx_glr_rotated_grids_002.png :alt: rotated grids :srcset: /example_gallery/grid_definitions/images/sphx_glr_rotated_grids_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-71 Naturally, when the grid is rotated 90 degrees the horizontal lines turn to vertical lines, but this can also be achived by rotating 30 degrees. This is of course a feature of trigangular grids. For comparisson, let's show a grid for the rotations of 0, 30, 60 and 90 degrees. .. GENERATED FROM PYTHON SOURCE LINES 72-87 .. code-block:: Python ids_x = ids_y = numpy.arange(7) xx, yy = numpy.meshgrid(ids_x, ids_y) ids = numpy.stack([xx.ravel(), yy.ravel()]).T def plot_rotated_grid(rotation, ax): grid = TriGrid(size=1, rotation=rotation) geometries = grid.to_shapely(ids, as_multipolygon=True) plot_polygons(geometries.geoms, colors="black", fill=False, linewidth=2, ax=ax) fig, axes = plt.subplots(2,2) for rotation, ax in zip([0, 30, 60, 90], axes.ravel()): plot_rotated_grid(rotation, ax) plt.show() .. image-sg:: /example_gallery/grid_definitions/images/sphx_glr_rotated_grids_003.png :alt: rotated grids :srcset: /example_gallery/grid_definitions/images/sphx_glr_rotated_grids_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-101 A careful observer might notice that the x and y axes show different values for different rotation. The same ids are used for all rotations, but since the grid is rotated the ids are at different locations. This means ids from a grid with a different rotation are not compatible. .. Warning :: The operations ``cells_in_bounds`` or rotated grids, as well as rotated BoudedGrids are not supported. The ``bounds`` for these operations represent a rectangle that is aligned with the coordinate reference system. It is not obvioush how this should relate to a rotated grid, as the cells and the bounds will not align and thus the cells will be cut by the bounds. If this is to be supported, a clear definition will have to be created about what it means for a rotated grid to be aligned with 'straight' bounds. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.897 seconds) .. _sphx_glr_download_example_gallery_grid_definitions_rotated_grids.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: rotated_grids.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: rotated_grids.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_