Skip to content

Commit a660002

Browse files
feat: add qvariantToFloat function
1 parent 9435579 commit a660002

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

m2l/main/vectorLayerWrapper.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,35 @@ def dataframeToQgsLayer(
454454
feedback.pushInfo("Done.")
455455
feedback.setProgress(100)
456456
return sink, sink_id
457+
458+
from qgis.core import NULL
459+
from PyQt5.QtCore import QVariant
460+
461+
def qvariantToFloat(f, field_name):
462+
val = f.attribute(field_name) # usually returns a native Python type
463+
# null / empty values
464+
if val in (None, NULL, ''):
465+
return None
466+
# strings with decimal comma (depending on locale)
467+
if isinstance(val, str):
468+
val = val.strip()
469+
if val == '':
470+
return None
471+
val = val.replace(',', '.') # replace comma with dot if present
472+
try:
473+
return float(val)
474+
except ValueError:
475+
pass
476+
# residual QVariant
477+
if isinstance(val, QVariant):
478+
# toDouble() -> (value, ok)
479+
d, ok = val.toDouble()
480+
return float(d) if ok else None
481+
# native int/float
482+
if isinstance(val, (int, float)):
483+
return float(val)
484+
# fallback conversion attempt
485+
try:
486+
return float(val)
487+
except Exception:
488+
return None

0 commit comments

Comments
 (0)