Skip to content

Commit 0881d0f

Browse files
yohaimagenseismanweiji14michaelgrund
authored
Plot square or cube by default for OGR/GMT files with Point/MultiPoint 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]>
1 parent bccfb68 commit 0881d0f

8 files changed

+143
-0
lines changed

pygmt/src/plot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
kwargs_to_strings,
1313
use_alias,
1414
)
15+
from pygmt.src.which import which
1516

1617

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

205207
kind = data_kind(data, x, y)
@@ -213,6 +215,18 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs):
213215
and data.geom_type.isin(["Point", "MultiPoint"]).all()
214216
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
215217
kwargs["S"] = "s0.2c"
218+
elif (
219+
"S" not in kwargs and kind == "file"
220+
): # checking that the data is a file path to set default style
221+
try:
222+
with open(which(data), mode="r", encoding="utf8") as file:
223+
line = file.readline()
224+
if (
225+
"@GMULTIPOINT" in line or "@GPOINT" in line
226+
): # if the file is gmt style and geometry is set to Point
227+
kwargs["S"] = "s0.2c"
228+
except FileNotFoundError:
229+
pass
216230
if "G" in kwargs and not isinstance(kwargs["G"], str):
217231
if kind != "vectors":
218232
raise GMTInvalidInput(

pygmt/src/plot3d.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
kwargs_to_strings,
1313
use_alias,
1414
)
15+
from pygmt.src.which import which
1516

1617

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

175177
kind = data_kind(data, x, y, z)
@@ -183,6 +185,18 @@ def plot3d(
183185
and data.geom_type.isin(["Point", "MultiPoint"]).all()
184186
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
185187
kwargs["S"] = "u0.2c"
188+
elif (
189+
"S" not in kwargs and kind == "file"
190+
): # checking that the data is a file path to set default style
191+
try:
192+
with open(which(data), mode="r", encoding="utf8") as file:
193+
line = file.readline()
194+
if (
195+
"@GMULTIPOINT" in line or "@GPOINT" in line
196+
): # if the file is gmt style and geometry is set to Point
197+
kwargs["S"] = "u0.2c"
198+
except FileNotFoundError:
199+
pass
186200
if "G" in kwargs and not isinstance(kwargs["G"], str):
187201
if kind != "vectors":
188202
raise GMTInvalidInput(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 88b5844699eb5f1a977350575909a074
3+
size: 12063
4+
path: test_plot3d_ogrgmt_file_multipoint_default_style.png
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 91aabfe40c0121b151385c8a27e3495c
3+
size: 12080
4+
path: test_plot3d_ogrgmt_file_multipoint_non_default_style.png
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 6c395fb503f67d024925a2c86cae7ba8
3+
size: 4272
4+
path: test_plot_ogrgmt_file_multipoint_default_style.png
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 4a946506f8a48be04792fdabffc6fa07
3+
size: 4451
4+
path: test_plot_ogrgmt_file_multipoint_non_default_style.png

pygmt/tests/test_plot.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import xarray as xr
1212
from pygmt import Figure
1313
from pygmt.exceptions import GMTInvalidInput
14+
from pygmt.helpers import GMTTempFile
1415

1516
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
1617
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
@@ -495,3 +496,46 @@ def test_plot_deprecate_columns_to_incols(region):
495496
)
496497
assert len(record) == 1 # check that only one warning was raised
497498
return fig
499+
500+
501+
@pytest.mark.mpl_image_compare
502+
def test_plot_ogrgmt_file_multipoint_default_style():
503+
"""
504+
Make sure that OGR/GMT files with MultiPoint geometry are plotted as
505+
squares and not as line (default GMT style).
506+
"""
507+
with GMTTempFile(suffix=".gmt") as tmpfile:
508+
gmt_file = """# @VGMT1.0 @GMULTIPOINT
509+
# @R1/1/1/1UB
510+
# FEATURE_DATA
511+
1 2
512+
"""
513+
with open(tmpfile.name, "w", encoding="utf8") as file:
514+
file.write(gmt_file)
515+
fig = Figure()
516+
fig.plot(data=tmpfile.name, region=[0, 2, 1, 3], projection="X2c", frame=True)
517+
return fig
518+
519+
520+
@pytest.mark.mpl_image_compare
521+
def test_plot_ogrgmt_file_multipoint_non_default_style():
522+
"""
523+
Make sure that non-default style can be set for plotting OGR/GMT file.
524+
"""
525+
with GMTTempFile(suffix=".gmt") as tmpfile:
526+
gmt_file = """# @VGMT1.0 @GPOINT
527+
# @R1/1/1/1UB
528+
# FEATURE_DATA
529+
1 2
530+
"""
531+
with open(tmpfile.name, "w", encoding="utf8") as file:
532+
file.write(gmt_file)
533+
fig = Figure()
534+
fig.plot(
535+
data=tmpfile.name,
536+
region=[0, 2, 1, 3],
537+
projection="X2c",
538+
frame=True,
539+
style="c0.2c",
540+
)
541+
return fig

pygmt/tests/test_plot3d.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
from pygmt import Figure
1010
from pygmt.exceptions import GMTInvalidInput
11+
from pygmt.helpers import GMTTempFile
1112

1213
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
1314
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
@@ -517,3 +518,57 @@ def test_plot3d_deprecate_columns_to_incols(data, region):
517518
)
518519
assert len(record) == 1 # check that only one warning was raised
519520
return fig
521+
522+
523+
@pytest.mark.mpl_image_compare
524+
def test_plot3d_ogrgmt_file_multipoint_default_style():
525+
"""
526+
Make sure that OGR/GMT files with MultiPoint geometry are plotted as cubes
527+
and not as line (default GMT style).
528+
"""
529+
with GMTTempFile(suffix=".gmt") as tmpfile:
530+
gmt_file = """# @VGMT1.0 @GMULTIPOINT
531+
# @R1/1.5/1/1.5
532+
# FEATURE_DATA
533+
>
534+
1 1 2
535+
1.5 1.5 1"""
536+
with open(tmpfile.name, "w", encoding="utf8") as file:
537+
file.write(gmt_file)
538+
fig = Figure()
539+
fig.plot3d(
540+
data=tmpfile.name,
541+
perspective=[315, 25],
542+
region=[0, 2, 0, 2, 0, 2],
543+
projection="X2c",
544+
frame=["WsNeZ1", "xag", "yag", "zag"],
545+
zscale=1.5,
546+
)
547+
return fig
548+
549+
550+
@pytest.mark.mpl_image_compare
551+
def test_plot3d_ogrgmt_file_multipoint_non_default_style():
552+
"""
553+
Make sure that non-default style can be set for plotting OGR/GMT file.
554+
"""
555+
with GMTTempFile(suffix=".gmt") as tmpfile:
556+
gmt_file = """# @VGMT1.0 @GMULTIPOINT
557+
# @R1/1.5/1/1.5
558+
# FEATURE_DATA
559+
>
560+
1 1 2
561+
1.5 1.5 1"""
562+
with open(tmpfile.name, "w", encoding="utf8") as file:
563+
file.write(gmt_file)
564+
fig = Figure()
565+
fig.plot3d(
566+
data=tmpfile.name,
567+
perspective=[315, 25],
568+
region=[0, 2, 0, 2, 0, 2],
569+
projection="X2c",
570+
frame=["WsNeZ1", "xag", "yag", "zag"],
571+
zscale=1.5,
572+
style="c0.2c",
573+
)
574+
return fig

0 commit comments

Comments
 (0)