Skip to content

Commit

Permalink
Handle PROJ4 strings with spaces
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weiji14 committed Mar 6, 2022
1 parent b58af8b commit 3ec7727
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
16 changes: 13 additions & 3 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ def build_arg_string(kwargs):
same command line argument. For example, the kwargs entry ``'B': ['xa',
'yaf']`` will be converted to ``-Bxa -Byaf`` in the argument string.
Note that spaces ` ` in arguments are converted to the equivalent octal
code `\040`, except in the case of -J (projection) arguments where PROJ4
strings (e.g. "+proj=longlat +datum=WGS84") will have their spaces removed.
See https://github.com/GenericMappingTools/pygmt/pull/1487 for more info.
Parameters
----------
kwargs : dict
Expand All @@ -151,7 +156,7 @@ def build_arg_string(kwargs):
... A=True,
... B=False,
... E=200,
... J="X4c",
... J="+proj=longlat +datum=WGS84",
... P="",
... R="1/2/3/4",
... X=None,
Expand All @@ -160,7 +165,7 @@ def build_arg_string(kwargs):
... )
... )
... )
-A -E200 -JX4c -P -R1/2/3/4 -Z0
-A -E200 -J+proj=longlat+datum=WGS84 -P -R1/2/3/4 -Z0
>>> print(
... build_arg_string(
... dict(
Expand Down Expand Up @@ -196,7 +201,12 @@ def build_arg_string(kwargs):
elif kwargs[key] is True:
gmt_args.append(f"-{key}")
else:
_value = str(kwargs[key]).replace(" ", r"\040")
if key != "J": # non-projection parameters
_value = str(kwargs[key]).replace(" ", r"\040")
else:
# special handling if key == "J" (projection)
# remove any spaces in PROJ4 string
_value = str(kwargs[key]).replace(" ", "")
gmt_args.append(rf"-{key}{_value}")
return " ".join(sorted(gmt_args))

Expand Down
4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_basemap_utm_projection.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: e6984efed2a94673754cc7f1f1d74832
size: 9069
path: test_basemap_utm_projection.png
20 changes: 20 additions & 0 deletions pygmt/tests/test_basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ def test_basemap_winkel_tripel():
return fig


@pytest.mark.mpl_image_compare(filename="test_basemap_utm_projection.png")
@pytest.mark.parametrize(
"projection",
[
"EPSG:32723 +width=5",
"+proj=utm +zone=23 +south +datum=WGS84 +units=m +no_defs +width=5",
],
)
def test_basemap_utm_projection(projection):
"""
Create a Universal Transverse Mercator (Zone 23S) basemap plot.
Also check that providing the projection as an EPSG code or PROJ4 string
works.
"""
fig = Figure()
fig.basemap(region=[-52, -50, -12, -11], projection=projection, frame="afg")
return fig


@pytest.mark.mpl_image_compare
def test_basemap_rose():
"""
Expand Down
11 changes: 9 additions & 2 deletions pygmt/tests/test_grdproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,20 @@ def test_grdproject_file_out(grid, expected_grid):
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)


def test_grdproject_no_outgrid(grid, expected_grid):
@pytest.mark.parametrize(
"projection",
["M10c", "EPSG:3395 +width=10", "+proj=merc +ellps=WGS84 +units=m +width=10"],
)
def test_grdproject_no_outgrid(grid, projection, expected_grid):
"""
Test grdproject with no set outgrid.
Also check that providing the projection as an EPSG code or PROJ4 string
works.
"""
assert grid.gmt.gtype == 1 # Geographic grid
result = grdproject(
grid=grid, projection="M10c", spacing=3, region=[-53, -51, -20, -17]
grid=grid, projection=projection, spacing=3, region=[-53, -51, -20, -17]
)
assert result.gmt.gtype == 0 # Rectangular grid
assert result.gmt.registration == 1 # Pixel registration
Expand Down

0 comments on commit 3ec7727

Please sign in to comment.