|
| 1 | +""" |
| 2 | +scalebar - Add a scale bar. |
| 3 | +""" |
| 4 | + |
| 5 | +from collections.abc import Sequence |
| 6 | +from typing import Literal |
| 7 | + |
| 8 | +from pygmt._typing import AnchorCode |
| 9 | +from pygmt.alias import Alias, AliasSystem |
| 10 | +from pygmt.clib import Session |
| 11 | +from pygmt.exceptions import GMTInvalidInput |
| 12 | +from pygmt.helpers import build_arg_list, fmt_docstring |
| 13 | +from pygmt.params import Box, Position |
| 14 | +from pygmt.src._common import _parse_position |
| 15 | + |
| 16 | +__doctest_skip__ = ["scalebar"] |
| 17 | + |
| 18 | + |
| 19 | +@fmt_docstring |
| 20 | +def scalebar( # noqa: PLR0913 |
| 21 | + self, |
| 22 | + position: Position | Sequence[float | str] | AnchorCode | None = None, |
| 23 | + length: float | str | None = None, |
| 24 | + height: float | str | None = None, |
| 25 | + scale_position: float | Sequence[float] | bool = False, |
| 26 | + label: str | bool = False, |
| 27 | + label_alignment: Literal["left", "right", "top", "bottom"] | None = None, |
| 28 | + unit: bool = False, |
| 29 | + fancy: bool = False, |
| 30 | + vertical: bool = False, |
| 31 | + box: Box | bool = False, |
| 32 | + verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"] |
| 33 | + | bool = False, |
| 34 | + panel: int | Sequence[int] | bool = False, |
| 35 | + perspective: float | Sequence[float] | str | bool = False, |
| 36 | + transparency: float | None = None, |
| 37 | +): |
| 38 | + """ |
| 39 | + Add a scale bar on the plot. |
| 40 | +
|
| 41 | + Parameters |
| 42 | + ---------- |
| 43 | + position |
| 44 | + Position of the scale bar on the plot. It can be specified in multiple ways: |
| 45 | +
|
| 46 | + - A :class:`pygmt.params.Position` object to fully control the reference point, |
| 47 | + anchor point, and offset. |
| 48 | + - A sequence of two values representing the x and y coordinates in plot |
| 49 | + coordinates, e.g., ``(1, 2)`` or ``("1c", "2c")``. |
| 50 | + - A :doc:`2-character justification code </techref/justification_codes>` for a |
| 51 | + position inside the plot, e.g., ``"TL"`` for Top Left corner inside the plot. |
| 52 | +
|
| 53 | + If not specified, defaults to the lower-left corner of the plot. |
| 54 | + length |
| 55 | + Length of the scale bar in km. Append a suffix to specify different units. Valid |
| 56 | + units are: **e**: meters; **f**: feet; **k**: kilometers; **M**: statute mile; |
| 57 | + **n**: nautical miles; **u**: US Survey foot. |
| 58 | + height |
| 59 | + Height of the scale bar. Only works when ``fancy=True``. [Default is ``"5p"``]. |
| 60 | + scale_position |
| 61 | + Specify the location where on a geographic map the scale applies. It can be: |
| 62 | +
|
| 63 | + - *slat*: Map scale is calculated for latitude *slat* |
| 64 | + - (*slon*, *slat*): Map scale is calculated for latitude *slat* and longitude |
| 65 | + *slon*, which is useful for oblique projections. |
| 66 | + - ``True``: Map scale is calculated for the middle of the map. |
| 67 | + - ``False``: Default to the location of the reference point. |
| 68 | + label |
| 69 | + Text string to use as the scale bar label. If ``False``, no label is drawn. If |
| 70 | + ``True``, the distance unit provided in the ``length`` parameter (default is km) |
| 71 | + is used as the label. This parameter requires ``fancy=True``. |
| 72 | + label_alignment |
| 73 | + Alignment of the scale bar label. Choose from ``"left"``, ``"right"``, |
| 74 | + ``"top"``, or ``"bottom"``. [Default is ``"top"``]. |
| 75 | + fancy |
| 76 | + If ``True``, draw a "fancy" scale bar, which is a segmented bar with alternating |
| 77 | + black and white rectangles. If ``False``, draw a plain scale bar. |
| 78 | + unit |
| 79 | + If ``True``, append the unit to all distance annotations along the scale. For a |
| 80 | + plain scale, this will instead select the unit to be appended to the distance |
| 81 | + length. The unit is determined from the suffix in the ``length`` or defaults to |
| 82 | + ``"km"``. |
| 83 | + vertical |
| 84 | + If ``True``, plot a vertical rather than a horizontal Cartesian scale. |
| 85 | + box |
| 86 | + Draw a background box behind the directional rose. If set to ``True``, a simple |
| 87 | + rectangular box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box |
| 88 | + appearance, pass a :class:`pygmt.params.Box` object to control style, fill, pen, |
| 89 | + and other box properties. |
| 90 | + $verbose |
| 91 | + $panel |
| 92 | + $perspective |
| 93 | + $transparency |
| 94 | +
|
| 95 | + Examples |
| 96 | + -------- |
| 97 | + >>> import pygmt |
| 98 | + >>> from pygmt.params import Box, Position |
| 99 | + >>> fig = pygmt.Figure() |
| 100 | + >>> fig.basemap(region=[0, 80, -30, 30], projection="M10c", frame=True) |
| 101 | + >>> fig.scalebar( |
| 102 | + ... position=Position((10, 10), cstype="mapcoords"), |
| 103 | + ... length=1000, |
| 104 | + ... fancy=True, |
| 105 | + ... label="Scale", |
| 106 | + ... unit=True, |
| 107 | + ... ) |
| 108 | + >>> fig.show() |
| 109 | + """ |
| 110 | + self._activate_figure() |
| 111 | + |
| 112 | + position = _parse_position( |
| 113 | + position, |
| 114 | + kwdict={ |
| 115 | + "length": length, |
| 116 | + "height": height, |
| 117 | + "label_alignment": label_alignment, |
| 118 | + "scale_position": scale_position, |
| 119 | + "fancy": fancy, |
| 120 | + "label": label, |
| 121 | + "unit": unit, |
| 122 | + "vertical": vertical, |
| 123 | + }, |
| 124 | + default=None, # Use the GMT default. |
| 125 | + ) |
| 126 | + |
| 127 | + if length is None: |
| 128 | + msg = "Parameter 'length' must be specified." |
| 129 | + raise GMTInvalidInput(msg) |
| 130 | + |
| 131 | + aliasdict = AliasSystem( |
| 132 | + F=Alias(box, name="box"), |
| 133 | + L=[ |
| 134 | + Alias(position, name="position"), |
| 135 | + Alias(length, name="length", prefix="+w"), |
| 136 | + Alias( |
| 137 | + label_alignment, |
| 138 | + name="label_alignment", |
| 139 | + prefix="+a", |
| 140 | + mapping={"left": "l", "right": "r", "top": "t", "bottom": "b"}, |
| 141 | + ), |
| 142 | + Alias(scale_position, name="scale_position", prefix="+c", sep="/", size=2), |
| 143 | + Alias(fancy, name="fancy", prefix="+f"), |
| 144 | + Alias(label, name="label", prefix="+l"), |
| 145 | + Alias(unit, name="unit", prefix="+u"), |
| 146 | + Alias(vertical, name="vertical", prefix="+v"), |
| 147 | + ], |
| 148 | + ).add_common( |
| 149 | + V=verbose, |
| 150 | + c=panel, |
| 151 | + p=perspective, |
| 152 | + t=transparency, |
| 153 | + ) |
| 154 | + |
| 155 | + confdict = {} |
| 156 | + if height is not None: |
| 157 | + confdict["MAP_SCALE_HEIGHT"] = height |
| 158 | + |
| 159 | + with Session() as lib: |
| 160 | + lib.call_module( |
| 161 | + module="basemap", args=build_arg_list(aliasdict, confdict=confdict) |
| 162 | + ) |
0 commit comments