Skip to content

Commit

Permalink
Plot square or cube by default for OGR/GMT files with Point/MultiPoin…
Browse files Browse the repository at this point in the history
…t types (#1438)

Set default behavior for plotting OGR/GMT files with
Point/Multipoint type geometry as points (-Sp0.2c) for `plot`
and cubes (-Su0.2c) for `plot3d`, instead of GMT's default
line style plot.

Co-authored-by: Dongdong Tian <[email protected]>
Co-authored-by: Wei Ji <[email protected]>
Co-authored-by: Michael Grund <[email protected]>
  • Loading branch information
4 people authored Aug 25, 2021
1 parent bccfb68 commit 0881d0f
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pygmt/src/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
kwargs_to_strings,
use_alias,
)
from pygmt.src.which import which


@fmt_docstring
Expand Down Expand Up @@ -200,6 +201,7 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs):
*transparency* can also be a 1d array to set varying transparency
for symbols, but this option is only valid if using x/y.
"""
# pylint: disable=too-many-locals
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access

kind = data_kind(data, x, y)
Expand All @@ -213,6 +215,18 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs):
and data.geom_type.isin(["Point", "MultiPoint"]).all()
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
kwargs["S"] = "s0.2c"
elif (
"S" not in kwargs and kind == "file"
): # checking that the data is a file path to set default style
try:
with open(which(data), mode="r", encoding="utf8") as file:
line = file.readline()
if (
"@GMULTIPOINT" in line or "@GPOINT" in line
): # if the file is gmt style and geometry is set to Point
kwargs["S"] = "s0.2c"
except FileNotFoundError:
pass
if "G" in kwargs and not isinstance(kwargs["G"], str):
if kind != "vectors":
raise GMTInvalidInput(
Expand Down
14 changes: 14 additions & 0 deletions pygmt/src/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
kwargs_to_strings,
use_alias,
)
from pygmt.src.which import which


@fmt_docstring
Expand Down Expand Up @@ -170,6 +171,7 @@ def plot3d(
*transparency* can also be a 1d array to set varying transparency
for symbols, but this option is only valid if using x/y/z.
"""
# pylint: disable=too-many-locals
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access

kind = data_kind(data, x, y, z)
Expand All @@ -183,6 +185,18 @@ def plot3d(
and data.geom_type.isin(["Point", "MultiPoint"]).all()
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
kwargs["S"] = "u0.2c"
elif (
"S" not in kwargs and kind == "file"
): # checking that the data is a file path to set default style
try:
with open(which(data), mode="r", encoding="utf8") as file:
line = file.readline()
if (
"@GMULTIPOINT" in line or "@GPOINT" in line
): # if the file is gmt style and geometry is set to Point
kwargs["S"] = "u0.2c"
except FileNotFoundError:
pass
if "G" in kwargs and not isinstance(kwargs["G"], str):
if kind != "vectors":
raise GMTInvalidInput(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 88b5844699eb5f1a977350575909a074
size: 12063
path: test_plot3d_ogrgmt_file_multipoint_default_style.png
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 91aabfe40c0121b151385c8a27e3495c
size: 12080
path: test_plot3d_ogrgmt_file_multipoint_non_default_style.png
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 6c395fb503f67d024925a2c86cae7ba8
size: 4272
path: test_plot_ogrgmt_file_multipoint_default_style.png
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 4a946506f8a48be04792fdabffc6fa07
size: 4451
path: test_plot_ogrgmt_file_multipoint_non_default_style.png
44 changes: 44 additions & 0 deletions pygmt/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import xarray as xr
from pygmt import Figure
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
Expand Down Expand Up @@ -495,3 +496,46 @@ def test_plot_deprecate_columns_to_incols(region):
)
assert len(record) == 1 # check that only one warning was raised
return fig


@pytest.mark.mpl_image_compare
def test_plot_ogrgmt_file_multipoint_default_style():
"""
Make sure that OGR/GMT files with MultiPoint geometry are plotted as
squares and not as line (default GMT style).
"""
with GMTTempFile(suffix=".gmt") as tmpfile:
gmt_file = """# @VGMT1.0 @GMULTIPOINT
# @R1/1/1/1UB
# FEATURE_DATA
1 2
"""
with open(tmpfile.name, "w", encoding="utf8") as file:
file.write(gmt_file)
fig = Figure()
fig.plot(data=tmpfile.name, region=[0, 2, 1, 3], projection="X2c", frame=True)
return fig


@pytest.mark.mpl_image_compare
def test_plot_ogrgmt_file_multipoint_non_default_style():
"""
Make sure that non-default style can be set for plotting OGR/GMT file.
"""
with GMTTempFile(suffix=".gmt") as tmpfile:
gmt_file = """# @VGMT1.0 @GPOINT
# @R1/1/1/1UB
# FEATURE_DATA
1 2
"""
with open(tmpfile.name, "w", encoding="utf8") as file:
file.write(gmt_file)
fig = Figure()
fig.plot(
data=tmpfile.name,
region=[0, 2, 1, 3],
projection="X2c",
frame=True,
style="c0.2c",
)
return fig
55 changes: 55 additions & 0 deletions pygmt/tests/test_plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
from pygmt import Figure
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
Expand Down Expand Up @@ -517,3 +518,57 @@ def test_plot3d_deprecate_columns_to_incols(data, region):
)
assert len(record) == 1 # check that only one warning was raised
return fig


@pytest.mark.mpl_image_compare
def test_plot3d_ogrgmt_file_multipoint_default_style():
"""
Make sure that OGR/GMT files with MultiPoint geometry are plotted as cubes
and not as line (default GMT style).
"""
with GMTTempFile(suffix=".gmt") as tmpfile:
gmt_file = """# @VGMT1.0 @GMULTIPOINT
# @R1/1.5/1/1.5
# FEATURE_DATA
>
1 1 2
1.5 1.5 1"""
with open(tmpfile.name, "w", encoding="utf8") as file:
file.write(gmt_file)
fig = Figure()
fig.plot3d(
data=tmpfile.name,
perspective=[315, 25],
region=[0, 2, 0, 2, 0, 2],
projection="X2c",
frame=["WsNeZ1", "xag", "yag", "zag"],
zscale=1.5,
)
return fig


@pytest.mark.mpl_image_compare
def test_plot3d_ogrgmt_file_multipoint_non_default_style():
"""
Make sure that non-default style can be set for plotting OGR/GMT file.
"""
with GMTTempFile(suffix=".gmt") as tmpfile:
gmt_file = """# @VGMT1.0 @GMULTIPOINT
# @R1/1.5/1/1.5
# FEATURE_DATA
>
1 1 2
1.5 1.5 1"""
with open(tmpfile.name, "w", encoding="utf8") as file:
file.write(gmt_file)
fig = Figure()
fig.plot3d(
data=tmpfile.name,
perspective=[315, 25],
region=[0, 2, 0, 2, 0, 2],
projection="X2c",
frame=["WsNeZ1", "xag", "yag", "zag"],
zscale=1.5,
style="c0.2c",
)
return fig

0 comments on commit 0881d0f

Please sign in to comment.