Skip to content

Commit 3ec7727

Browse files
committed
Handle PROJ4 strings with spaces
Instead of converting spaces to \040 in proj4 strings, just remove them directly. Added parametrized unit tests to basemap and grdproject to check that it works.
1 parent b58af8b commit 3ec7727

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

pygmt/helpers/utils.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ def build_arg_string(kwargs):
131131
same command line argument. For example, the kwargs entry ``'B': ['xa',
132132
'yaf']`` will be converted to ``-Bxa -Byaf`` in the argument string.
133133
134+
Note that spaces ` ` in arguments are converted to the equivalent octal
135+
code `\040`, except in the case of -J (projection) arguments where PROJ4
136+
strings (e.g. "+proj=longlat +datum=WGS84") will have their spaces removed.
137+
See https://github.com/GenericMappingTools/pygmt/pull/1487 for more info.
138+
134139
Parameters
135140
----------
136141
kwargs : dict
@@ -151,7 +156,7 @@ def build_arg_string(kwargs):
151156
... A=True,
152157
... B=False,
153158
... E=200,
154-
... J="X4c",
159+
... J="+proj=longlat +datum=WGS84",
155160
... P="",
156161
... R="1/2/3/4",
157162
... X=None,
@@ -160,7 +165,7 @@ def build_arg_string(kwargs):
160165
... )
161166
... )
162167
... )
163-
-A -E200 -JX4c -P -R1/2/3/4 -Z0
168+
-A -E200 -J+proj=longlat+datum=WGS84 -P -R1/2/3/4 -Z0
164169
>>> print(
165170
... build_arg_string(
166171
... dict(
@@ -196,7 +201,12 @@ def build_arg_string(kwargs):
196201
elif kwargs[key] is True:
197202
gmt_args.append(f"-{key}")
198203
else:
199-
_value = str(kwargs[key]).replace(" ", r"\040")
204+
if key != "J": # non-projection parameters
205+
_value = str(kwargs[key]).replace(" ", r"\040")
206+
else:
207+
# special handling if key == "J" (projection)
208+
# remove any spaces in PROJ4 string
209+
_value = str(kwargs[key]).replace(" ", "")
200210
gmt_args.append(rf"-{key}{_value}")
201211
return " ".join(sorted(gmt_args))
202212

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: e6984efed2a94673754cc7f1f1d74832
3+
size: 9069
4+
path: test_basemap_utm_projection.png

pygmt/tests/test_basemap.py

+20
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ def test_basemap_winkel_tripel():
7373
return fig
7474

7575

76+
@pytest.mark.mpl_image_compare(filename="test_basemap_utm_projection.png")
77+
@pytest.mark.parametrize(
78+
"projection",
79+
[
80+
"EPSG:32723 +width=5",
81+
"+proj=utm +zone=23 +south +datum=WGS84 +units=m +no_defs +width=5",
82+
],
83+
)
84+
def test_basemap_utm_projection(projection):
85+
"""
86+
Create a Universal Transverse Mercator (Zone 23S) basemap plot.
87+
88+
Also check that providing the projection as an EPSG code or PROJ4 string
89+
works.
90+
"""
91+
fig = Figure()
92+
fig.basemap(region=[-52, -50, -12, -11], projection=projection, frame="afg")
93+
return fig
94+
95+
7696
@pytest.mark.mpl_image_compare
7797
def test_basemap_rose():
7898
"""

pygmt/tests/test_grdproject.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ def test_grdproject_file_out(grid, expected_grid):
5858
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
5959

6060

61-
def test_grdproject_no_outgrid(grid, expected_grid):
61+
@pytest.mark.parametrize(
62+
"projection",
63+
["M10c", "EPSG:3395 +width=10", "+proj=merc +ellps=WGS84 +units=m +width=10"],
64+
)
65+
def test_grdproject_no_outgrid(grid, projection, expected_grid):
6266
"""
6367
Test grdproject with no set outgrid.
68+
69+
Also check that providing the projection as an EPSG code or PROJ4 string
70+
works.
6471
"""
6572
assert grid.gmt.gtype == 1 # Geographic grid
6673
result = grdproject(
67-
grid=grid, projection="M10c", spacing=3, region=[-53, -51, -20, -17]
74+
grid=grid, projection=projection, spacing=3, region=[-53, -51, -20, -17]
6875
)
6976
assert result.gmt.gtype == 0 # Rectangular grid
7077
assert result.gmt.registration == 1 # Pixel registration

0 commit comments

Comments
 (0)