Skip to content

Commit

Permalink
Fix UnicodeDecodeError with shapefiles for plot and plot3d (GenericMa…
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman authored and Josh Sixsmith committed Dec 21, 2022
1 parent c0a20c6 commit 719eaf9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
4 changes: 4 additions & 0 deletions pygmt/helpers/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def download_test_data():
# Other cache files
"@EGM96_to_36.txt",
"@MaunaLoa_CO2.txt",
"@RidgeTest.shp",
"@RidgeTest.shx",
"@RidgeTest.dbf",
"@RidgeTest.prj",
"@Table_5_11.txt",
"@Table_5_11_mean.xyz",
"@fractures_06.txt",
Expand Down
10 changes: 4 additions & 6 deletions pygmt/src/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,13 @@ def plot(self, data=None, x=None, y=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
elif "S" not in kwargs and kind == "file" and data.endswith(".gmt"):
# 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
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
Expand Down
10 changes: 4 additions & 6 deletions pygmt/src/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,13 @@ 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
elif "S" not in kwargs and kind == "file" and data.endswith(".gmt"):
# 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
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
Expand Down
4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_plot_shapefile.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 75277741d098cf7a0bad7869b574afc9
size: 24178
path: test_plot_shapefile.png
11 changes: 11 additions & 0 deletions pygmt/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,17 @@ def test_plot_ogrgmt_file_multipoint_non_default_style():


@pytest.mark.mpl_image_compare
def test_plot_shapefile():
"""
Make sure that plot works for shapefile.
See https://github.com/GenericMappingTools/pygmt/issues/1616.
"""
fig = Figure()
fig.plot(data="@RidgeTest.shp", pen="1p")
return fig


def test_plot_dataframe_incols():
"""
Make sure that the incols parameter works for pandas.DataFrame.
Expand Down

0 comments on commit 719eaf9

Please sign in to comment.