Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/arraystats/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ double AS_class_apply_algorithm(int algo, const double data[], int nrec,
{
double finfo = 0.0;

if (nrec < 1)
G_fatal_error(_("Cannot classify an empty set of values"));

switch (algo) {
case CLASS_INTERVAL:
finfo = AS_class_interval(data, nrec, *nbreaks, classbreaks);
Expand Down
42 changes: 26 additions & 16 deletions vector/v.class/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ int main(int argc, char *argv[])
int ofield;
int nrec, ctype, nbclass, nbreaks, *frequencies;
int ret, i;
double finfo;
double finfo = 0;
double *classbreaks, min, max, *data;
struct GASTATS stats;
struct GASTATS stats = {0};
char *desc, *fs;
enum OutputFormat format;
G_JSON_Value *root_value = NULL, *intervals_value = NULL,
Expand Down Expand Up @@ -174,6 +174,9 @@ int main(int argc, char *argv[])
if (nrec < 0)
G_fatal_error(_("Unable to select data from table"));

if (nrec == 0)
G_warning(_("No records selected from table <%s>"), Fi->table);

db_close_database_shutdown_driver(Driver);

ret = db_CatValArray_sort_by_value(&Cvarr);
Expand Down Expand Up @@ -203,15 +206,19 @@ int main(int argc, char *argv[])

/* Get classbreaks for given algorithm and number of classbreaks.
* finfo takes any info coming from the classification algorithms
* equ algorithm can alter number of class breaks */
finfo = AS_class_apply_algorithm(AS_option_to_algorithm(algo_opt), data,
nrec, &nbreaks, classbreaks);

if (G_strcasecmp(algo_opt->answer, "dis") == 0 && finfo < 3.84148)
G_warning(
_("The discontinuities algorithm indicates that some "
"class breaks are not statistically significant at "
"alpha=0.05. You are advised to reduce the number of classes."));
* equ algorithm can alter number of class breaks.
* With no data there is nothing to classify and the class breaks stay
* at zero. */
if (nrec > 0) {
finfo = AS_class_apply_algorithm(AS_option_to_algorithm(algo_opt), data,
nrec, &nbreaks, classbreaks);

if (G_strcasecmp(algo_opt->answer, "dis") == 0 && finfo < 3.84148)
G_warning(_("The discontinuities algorithm indicates that some "
"class breaks are not statistically significant at "
"alpha=0.05. You are advised to reduce the number of "
"classes."));
}

/*output to be piped to other modules ? */
if (breaks_flag->answer) {
Expand Down Expand Up @@ -280,12 +287,15 @@ int main(int argc, char *argv[])
for (i = 0; i < nbreaks + 1; i++)
frequencies[i] = 0;

ret =
AS_class_frequencies(data, nrec, nbreaks, classbreaks, frequencies);
AS_basic_stats(data, nrec, &stats);
min = max = 0;
if (nrec > 0) {
ret = AS_class_frequencies(data, nrec, nbreaks, classbreaks,
frequencies);
AS_basic_stats(data, nrec, &stats);

min = data[0];
max = data[nrec - 1];
min = data[0];
max = data[nrec - 1];
}

/* as equ algorithm can modify number of breaks we recalculate number of
* classes
Expand Down
6 changes: 3 additions & 3 deletions vector/v.class/tests/test_v_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ def test_v_class_where_no_matches(setup_vector_with_values):
where="value > 100", # No points satisfy this
algorithm="int",
nbclasses=5,
flags="g",
flags="b",
env=session.env,
)
breaks = [float(b) for b in output.strip().split(",")] if output.strip() else []
assert len(breaks) == 4 # For 5 classes → 4 breaks
breaks = [float(b) for b in output.strip().split(",")]
assert len(breaks) == 4 # For 5 classes, there are 4 breaks
assert all(b == 0.0 for b in breaks)


Expand Down
Loading