Skip to content

Commit

Permalink
Added extra example demonstrating use of both crs keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
pp-mo committed Jan 31, 2025
1 parent d4a7408 commit 5fdf8d3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/geovista/cache/registry.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ tests/images/examples.test__unstructured.tri_hammer.png 55a063f42722665ae3575d70
tests/images/examples.test__unstructured.tri.png 17706b23041a027bd52be9950a86703a59b8f1282a82790ee7686832d0bc4196
tests/images/examples.test__vector_data.flow_directions.png 26cea271234f57b721da235b0d1b9b6d35927003621ac3f4750cd8b52e99134e
tests/images/examples.test__vector_data.vectors.png 72f1a30dd57b8c6cc635e866aa1ffd79bdfc10dd75cbf5ea8588f74781f0c490
tests/images/examples.test__vector_data.true_winds.png b160ff21770e81514089fd1a58ed3bee137734aa78d830f1d7e3c522267f9d93
tests/images/examples.test__vector_data.wind_arrows.png e73f77f2cb804e7d629be36ef8b0fcef1d64d653db005d29d2a2188575734809
tests/images/examples.test__vector_data.winds_3d.png eb08f0d00a89d3429ab4e541d1b6c791d569d6bc6e359bf98d43f2937dff6753
tests/images/examples.test__warp.from_unstructured__fvcom.png b31dba987a706601c60df6ab9c39cf03e83ed6d07acf84d2d10a798702762f08
Expand Down
89 changes: 89 additions & 0 deletions src/geovista/examples/vector_data/true_winds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
# Copyright (c) 2021, GeoVista Contributors.
#
# This file is part of GeoVista and is distributed under the 3-Clause BSD license.
# See the LICENSE file in the package root directory for licensing details.

"""
"True" Winds on a Foreign Grid
------------------------------
This example demonstrates how to display "true" winds, when attached to data points
given in a non-standard coordinate system.
📋 Summary
^^^^^^^^^^
The example constructs some sample points on a polar stereographic grid, and assigns
synthetic vector values to them, representing wind data. These data are presented as
"true" winds, i.e. northward and eastward components, so *not* related to the sample
grid. This is a fairly typical real-world case.
In this case, the synthetic wind is a steady 4.0 ms-1 Eastward everywhere, so the
meaning is clearly seen.
We use the :meth:`geovista.bridge.Transform.from_points` method, passing the
winds to the ``vectors`` keyword, and specifying the different coordinate reference
systems of both the sample points (``crs``) and the vectors (``vectors_crs``).
Please refer to other code examples, e.g. `wind_arrows <./wind_arrows.html>`_ for more
details of the basic methods used.
""" # noqa: D205,D212,D400

from __future__ import annotations

# Use cartopy as a source for specifying Coordinate Reference Systems
# (which we can translate into PROJ via the .to_wkt() method)
import cartopy.crs as ccrs
import numpy as np

import geovista as gv


def main() -> None:
"""Demonstrate plotting vectors on different CRS from points."""
# Create a mesh of individual points, adding vectors at each point.
# NOTE: this creates a mesh with 'mesh vectors' : a specific concept in PyVista.

# Create a CRS for "ordinary latitude-longitude"
crs_latlon = ccrs.Geodetic().to_wkt()
# Create a CRS for "polar stereographic"
crs_polar = ccrs.NorthPolarStereo().to_wkt()
# Create polar grid points
xx = np.linspace(-5.0e6, 5.0e6, 20)
yy = np.linspace(-4.0e6, 2.0e6, 20)
xx, yy = np.meshgrid(xx, yy)
# Make vector component arrays matching the XX and YY arrays
xx, yy = xx.flatten(), yy.flatten()
vx, vy = 4.0 * np.ones_like(xx), np.zeros_like(yy)

# Create a mesh of location points with attached vector information
mesh = gv.Transform.from_points(
xx, yy, vectors=(vx, vy), crs=crs_polar, vectors_crs=crs_latlon
)

# Create a new mesh containing arrow glyphs, from the mesh vectors.
arrows = mesh.glyph(factor=0.02)

# Add the arrows to a Plotter with other aspects, and display
p = gv.GeoPlotter()
# Scale the base layer slightly to ensure it remains 'below' other elements.
p.add_base_layer(radius=0.99)
p.add_mesh(arrows, color="red")
p.add_coastlines()
p.add_graticule()
p.add_axes()

# Set up a nice default view
selected_view = [
(0.6037050543418041, -0.011033743353827528, 3.069575190259155),
(0.0, 0.0, 0.0028896447726441954),
(-0.9809865744261353, -0.019981215106321615, 0.19304427429621296),
]
p.camera_position = selected_view
p.show()


if __name__ == "__main__":
main()

0 comments on commit 5fdf8d3

Please sign in to comment.