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

Fix UnicodeDecodeError with shapefiles for plot and plot3d #1695

Merged
merged 14 commits into from
Mar 1, 2022
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