Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot square or cube by default for OGR/GMT files with Point/MultiPoint types #1438

Merged
merged 36 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
72f7e89
adding default behavior for ploting gmt file at plot.py
yohaimagen Aug 11, 2021
7498ec6
reformat
yohaimagen Aug 11, 2021
8a20e99
checks that the file is not on server
yohaimagen Aug 17, 2021
2d3e197
changing the if statmrnt to try except style
yohaimagen Aug 18, 2021
c9e525f
add the same if statment to plot3d.py
yohaimagen Aug 18, 2021
b797861
Update pygmt/src/plot.py
yohaimagen Aug 19, 2021
c0a355c
Update pygmt/src/plot.py
yohaimagen Aug 19, 2021
6e29ba6
Update pygmt/src/plot3d.py
yohaimagen Aug 19, 2021
9d62ce9
Update pygmt/src/plot3d.py
yohaimagen Aug 19, 2021
f456a69
resolve conflicts at plot.py and plot3d.py
yohaimagen Aug 19, 2021
fb120b8
changing the which import
yohaimagen Aug 19, 2021
516f960
add tests to test_plot.py
yohaimagen Aug 19, 2021
26bbc71
typo correction
yohaimagen Aug 19, 2021
9e5888d
reformat
yohaimagen Aug 19, 2021
0825d40
reformat
yohaimagen Aug 19, 2021
0d57330
importing which
yohaimagen Aug 19, 2021
029a852
adding tests to test_plot3d.py
yohaimagen Aug 19, 2021
4966424
reformat
yohaimagen Aug 19, 2021
f333f2f
corecting typo
yohaimagen Aug 19, 2021
0b28914
change the name of plot.py tests
yohaimagen Aug 23, 2021
429b34d
changing the names of the plot3d tests
yohaimagen Aug 23, 2021
1a64b98
Merge branch 'main' into gmt-shape-file-point-default-style
yohaimagen Aug 24, 2021
dce1d0c
Update pygmt/src/plot.py
yohaimagen Aug 24, 2021
e29edaf
Update pygmt/src/plot.py
yohaimagen Aug 24, 2021
393cd5c
Update pygmt/src/plot3d.py
yohaimagen Aug 24, 2021
cff97c7
add encoding
yohaimagen Aug 24, 2021
022b29e
ignors to many varibels at plot.py and plot3d.py
yohaimagen Aug 24, 2021
6999a52
Update pygmt/tests/test_plot.py
yohaimagen Aug 24, 2021
8638cdd
Update pygmt/tests/test_plot3d.py
yohaimagen Aug 24, 2021
85e2a15
Update pygmt/tests/test_plot3d.py
yohaimagen Aug 24, 2021
74bd867
Update pygmt/tests/test_plot.py
yohaimagen Aug 25, 2021
ebe4549
Update pygmt/tests/test_plot3d.py
yohaimagen Aug 25, 2021
a1e8cf5
Update pygmt/tests/test_plot.py
yohaimagen Aug 25, 2021
707dc96
Update pygmt/tests/test_plot3d.py
yohaimagen Aug 25, 2021
d68634a
reformat
yohaimagen Aug 25, 2021
065c428
Merge branch 'main' into gmt-shape-file-point-default-style
yohaimagen Aug 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 file with MultiPoint geometry are plotted as squares
yohaimagen marked this conversation as resolved.
Show resolved Hide resolved
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 defuslt style can be set.
yohaimagen marked this conversation as resolved.
Show resolved Hide resolved
"""
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 file with MultiPoint geometry are plotted as cubes
yohaimagen marked this conversation as resolved.
Show resolved Hide resolved
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.
yohaimagen marked this conversation as resolved.
Show resolved Hide resolved
"""
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