Skip to content

Commit 13065b3

Browse files
authored
Fix UnicodeDecodeError with shapefiles for plot and plot3d (#1695)
1 parent 7f075ce commit 13065b3

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

pygmt/helpers/testing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ def download_test_data():
172172
# Other cache files
173173
"@EGM96_to_36.txt",
174174
"@MaunaLoa_CO2.txt",
175+
"@RidgeTest.shp",
176+
"@RidgeTest.shx",
177+
"@RidgeTest.dbf",
178+
"@RidgeTest.prj",
175179
"@Table_5_11.txt",
176180
"@Table_5_11_mean.xyz",
177181
"@fractures_06.txt",

pygmt/src/plot.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,13 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs):
229229
and data.geom_type.isin(["Point", "MultiPoint"]).all()
230230
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
231231
kwargs["S"] = "s0.2c"
232-
elif (
233-
"S" not in kwargs and kind == "file"
234-
): # checking that the data is a file path to set default style
232+
elif "S" not in kwargs and kind == "file" and data.endswith(".gmt"):
233+
# checking that the data is a file path to set default style
235234
try:
236235
with open(which(data), mode="r", encoding="utf8") as file:
237236
line = file.readline()
238-
if (
239-
"@GMULTIPOINT" in line or "@GPOINT" in line
240-
): # if the file is gmt style and geometry is set to Point
237+
if "@GMULTIPOINT" in line or "@GPOINT" in line:
238+
# if the file is gmt style and geometry is set to Point
241239
kwargs["S"] = "s0.2c"
242240
except FileNotFoundError:
243241
pass

pygmt/src/plot3d.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,13 @@ def plot3d(
199199
and data.geom_type.isin(["Point", "MultiPoint"]).all()
200200
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
201201
kwargs["S"] = "u0.2c"
202-
elif (
203-
"S" not in kwargs and kind == "file"
204-
): # checking that the data is a file path to set default style
202+
elif "S" not in kwargs and kind == "file" and data.endswith(".gmt"):
203+
# checking that the data is a file path to set default style
205204
try:
206205
with open(which(data), mode="r", encoding="utf8") as file:
207206
line = file.readline()
208-
if (
209-
"@GMULTIPOINT" in line or "@GPOINT" in line
210-
): # if the file is gmt style and geometry is set to Point
207+
if "@GMULTIPOINT" in line or "@GPOINT" in line:
208+
# if the file is gmt style and geometry is set to Point
211209
kwargs["S"] = "u0.2c"
212210
except FileNotFoundError:
213211
pass
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 75277741d098cf7a0bad7869b574afc9
3+
size: 24178
4+
path: test_plot_shapefile.png

pygmt/tests/test_plot.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,17 @@ def test_plot_ogrgmt_file_multipoint_non_default_style():
543543

544544

545545
@pytest.mark.mpl_image_compare
546+
def test_plot_shapefile():
547+
"""
548+
Make sure that plot works for shapefile.
549+
550+
See https://github.com/GenericMappingTools/pygmt/issues/1616.
551+
"""
552+
fig = Figure()
553+
fig.plot(data="@RidgeTest.shp", pen="1p")
554+
return fig
555+
556+
546557
def test_plot_dataframe_incols():
547558
"""
548559
Make sure that the incols parameter works for pandas.DataFrame.

0 commit comments

Comments
 (0)