Skip to content

Commit a2310cb

Browse files
committed
Track the current region at the PyGMT level, instead of relying on the GMT_Extract_Region function of GMT.
1 parent fba6e83 commit a2310cb

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

pygmt/figure.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Figure:
100100
def __init__(self) -> None:
101101
self._name = unique_name()
102102
self._preview_dir = TemporaryDirectory(prefix=f"{self._name}-preview-")
103+
self._current_region = None # Track the current region
103104
self._activate_figure()
104105

105106
def __del__(self) -> None:
@@ -124,11 +125,37 @@ def region(self) -> np.ndarray:
124125
"""
125126
The geographic WESN bounding box for the current figure.
126127
"""
128+
# First try to return the tracked current region
129+
if self._current_region is not None:
130+
return self._current_region
131+
132+
# Fall back to extracting region from GMT (for backward compatibility)
127133
self._activate_figure()
128134
with Session() as lib:
129135
wesn = lib.extract_region()
130136
return wesn
131137

138+
def _update_current_region(self, region) -> None:
139+
"""
140+
Update the current region tracking.
141+
142+
Parameters
143+
----------
144+
region : str, list, or np.ndarray
145+
The region to set as current.
146+
"""
147+
if region is not None:
148+
# Convert region to numpy array for consistency
149+
if isinstance(region, str):
150+
# For string regions like "g" or "JP", we can't convert to array
151+
# We'll set it to None and let GMT handle it
152+
self._current_region = None
153+
else:
154+
# Convert list or array to numpy array
155+
self._current_region = np.asarray(region)
156+
else:
157+
self._current_region = None
158+
132159
def savefig(
133160
self,
134161
fname: PathLike,

pygmt/src/basemap.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def basemap(
100100
"""
101101
self._activate_figure()
102102

103+
# Update the current region tracking
104+
self._update_current_region(region)
105+
103106
aliasdict = AliasSystem(
104107
Jz=Alias(zscale, name="zscale"),
105108
JZ=Alias(zsize, name="zsize"),

pygmt/src/coast.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ def coast( # noqa: PLR0913
226226
"""
227227
self._activate_figure()
228228

229+
# Update the current region tracking
230+
self._update_current_region(region)
231+
229232
if (
230233
kwargs.get("G", land) is None
231234
and kwargs.get("S", water) is None

0 commit comments

Comments
 (0)