Skip to content

Commit dbbc168

Browse files
authored
Figure.plot3d: Refactor to increase code readability (#3143)
1 parent 19c4f55 commit dbbc168

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

pygmt/src/plot3d.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -184,48 +184,51 @@ def plot3d( # noqa: PLR0912
184184
kwargs = self._preprocess(**kwargs)
185185

186186
kind = data_kind(data, x, y, z)
187-
188187
extra_arrays = []
189-
if kwargs.get("S") is not None and kwargs["S"][0] in "vV" and direction is not None:
190-
extra_arrays.extend(direction)
191-
elif (
192-
kwargs.get("S") is None
193-
and kind == "geojson"
194-
and data.geom_type.isin(["Point", "MultiPoint"]).all()
195-
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
196-
kwargs["S"] = "u0.2c"
197-
elif kwargs.get("S") is None and kind == "file" and str(data).endswith(".gmt"):
198-
# checking that the data is a file path to set default style
199-
try:
200-
with Path(which(data)).open(encoding="utf8") as file:
201-
line = file.readline()
202-
if "@GMULTIPOINT" in line or "@GPOINT" in line:
203-
# if the file is gmt style and geometry is set to Point
204-
kwargs["S"] = "u0.2c"
205-
except FileNotFoundError:
206-
pass
207-
if is_nonstr_iter(kwargs.get("G")):
208-
if kind != "vectors":
209-
raise GMTInvalidInput(
210-
"Can't use arrays for fill if data is matrix or file."
211-
)
212-
extra_arrays.append(kwargs["G"])
213-
del kwargs["G"]
214-
if size is not None:
215-
if kind != "vectors":
216-
raise GMTInvalidInput(
217-
"Can't use arrays for 'size' if data is a matrix or a file."
218-
)
219-
extra_arrays.append(size)
220-
221-
for flag in ["I", "t"]:
222-
if is_nonstr_iter(kwargs.get(flag)):
223-
if kind != "vectors":
224-
raise GMTInvalidInput(
225-
f"Can't use arrays for {plot3d.aliases[flag]} if data is matrix or file."
226-
)
227-
extra_arrays.append(kwargs[flag])
228-
kwargs[flag] = ""
188+
189+
if kind == "vectors": # Add more columns for vectors input
190+
# Parameters for vector styles
191+
if (
192+
kwargs.get("S") is not None
193+
and kwargs["S"][0] in "vV"
194+
and is_nonstr_iter(direction)
195+
):
196+
extra_arrays.extend(direction)
197+
# Fill
198+
if is_nonstr_iter(kwargs.get("G")):
199+
extra_arrays.append(kwargs.get("G"))
200+
del kwargs["G"]
201+
# Size
202+
if is_nonstr_iter(size):
203+
extra_arrays.append(size)
204+
# Intensity and transparency
205+
for flag in ["I", "t"]:
206+
if is_nonstr_iter(kwargs.get(flag)):
207+
extra_arrays.append(kwargs.get(flag))
208+
kwargs[flag] = ""
209+
else:
210+
for name, value in [
211+
("direction", direction),
212+
("fill", kwargs.get("G")),
213+
("size", size),
214+
("intensity", kwargs.get("I")),
215+
("transparency", kwargs.get("t")),
216+
]:
217+
if is_nonstr_iter(value):
218+
raise GMTInvalidInput(f"'{name}' can't be 1-D array if 'data' is used.")
219+
220+
# Set the default style if data has a geometry of Point or MultiPoint
221+
if kwargs.get("S") is None:
222+
if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all():
223+
kwargs["S"] = "u0.2c"
224+
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
225+
try:
226+
with Path(which(data)).open() as file:
227+
line = file.readline()
228+
if "@GMULTIPOINT" in line or "@GPOINT" in line:
229+
kwargs["S"] = "u0.2c"
230+
except FileNotFoundError:
231+
pass
229232

230233
with Session() as lib:
231234
with lib.virtualfile_in(

0 commit comments

Comments
 (0)