Skip to content

Commit

Permalink
address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kritibirda26 committed Jul 18, 2024
1 parent 7d9ff1b commit a0be16c
Show file tree
Hide file tree
Showing 5 changed files with 909 additions and 1,082 deletions.
1 change: 1 addition & 0 deletions raster/r.report/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ int parse_layer(char *);
int match(char *, char *, int);

/* prt_report.c */
int compute_unit_format(int, int, enum OutputFormat);
int print_report(int, int);
int construct_val_str(int, CELL *, char *);
char *construct_cat_label(int, CELL);
Expand Down
69 changes: 11 additions & 58 deletions raster/r.report/prt_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ JSON_Value *make_category(int ns, int nl, JSON_Value *sub_categories)
json_object_set_number(object, "category", cats[nl]);

DCELL dLow, dHigh;
char str[500];

if (!is_fp[nl] || as_int)
json_object_set_number(object, "description", cats[nl]);
json_object_set_string(object, "label",
Rast_get_c_cat(&cats[nl], &layers[nl].labels));
else {
/* find or construct the label for floating point range to print */
if (Rast_is_c_null_value(&cats[nl]))
json_object_set_null(object, "description");
json_object_set_null(object, "label");
else if (cat_ranges) {
json_object_set_string(object, "description",
json_object_set_string(object, "label",
Rast_get_ith_d_cat(&layers[nl].labels,
cats[nl], &dLow, &dHigh));
}
Expand All @@ -82,10 +82,8 @@ JSON_Value *make_category(int ns, int nl, JSON_Value *sub_categories)
DMIN[nl];
dHigh = (DMAX[nl] - DMIN[nl]) / (double)nsteps * (double)cats[nl] +
DMIN[nl];
char *from = Rast_get_d_cat(&dLow, &layers[nl].labels);
char *to = Rast_get_d_cat(&dHigh, &layers[nl].labels);
sprintf(str, "from %s to %s", from, to);
json_object_set_string(object, "description", str);

json_object_set_string(object, "label", "from to");

JSON_Value *range_value = json_value_init_object();
JSON_Object *range_object = json_object(range_value);
Expand Down Expand Up @@ -132,53 +130,8 @@ JSON_Value *make_categories(int start, int end, int level)

void print_json()
{
for (int i = 0; i < nunits; i++) {
switch (unit[i].type) {
case CELL_COUNTS:
unit[i].label[0] = " cell";
unit[i].label[1] = "count";
break;

case PERCENT_COVER:
unit[i].label[0] = " % ";
unit[i].label[1] = "cover";
break;

case SQ_METERS:
unit[i].label[0] = "square";
unit[i].label[1] = "meters";
unit[i].factor = 1.0;
break;

case SQ_KILOMETERS:
unit[i].label[0] = " square ";
unit[i].label[1] = "kilometers";
unit[i].factor = 1.0e-6;
break;

case ACRES:
unit[i].label[0] = "";
unit[i].label[1] = "acres";
unit[i].factor = 2.47105381467165e-4; /* 640 acres in a sq mile */
break;

case HECTARES:
unit[i].label[0] = "";
unit[i].label[1] = "hectares";
unit[i].factor = 1.0e-4;
break;

case SQ_MILES:
unit[i].label[0] = "square";
unit[i].label[1] = " miles";
unit[i].factor = 3.86102158542446e-7; /* 1 / ( (0.0254m/in * 12in/ft
* 5280ft/mi)^2 ) */
break;

default:
G_fatal_error("Unit %d not yet supported", unit[i].type);
}
}
compute_unit_format(0, nunits - 1, JSON);

JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_object(root_value);

Expand All @@ -200,18 +153,18 @@ void print_json()
json_object_set_null(root_object, "mask");
}
else {
json_object_set_string(root_object, "mask", maskinfo());
json_object_set_string(root_object, "mask", mask);
}

JSON_Value *maps_value = json_value_init_array();
JSON_Array *maps_array = json_array(maps_value);
for (int i = 0; i < nlayers; i++) {
JSON_Value *map_value = json_value_init_object();
JSON_Object *map_object = json_object(map_value);
json_object_set_string(map_object, "name", layers[i].name); // fixme
json_object_set_string(map_object, "name", layers[i].name);
json_object_set_string(map_object, "description", layers[i].mapset);
json_object_set_string(map_object, "layer", layers[i].name);
json_object_set_string(map_object, "type", "raster"); // fixme
json_object_set_string(map_object, "type", "raster");
json_array_append_value(maps_array, map_value);
}
json_object_set_value(root_object, "maps", maps_value);
Expand Down
67 changes: 43 additions & 24 deletions raster/r.report/prt_report.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,49 @@
#include <grass/raster.h>
#include "global.h"

int print_report(int unit1, int unit2)
int compute_unit_format(int unit1, int unit2, enum OutputFormat format)
{
int ns, nl, nx;
int i, ns, len;
char num[100];
int len, new;
CELL *cats, *prev;
int first;
int i;
int divider_level;
int after_header;
int need_format;
int with_stats;
char *cp;
int spacing;
char dot;

/* examine units, determine output format */
for (i = unit1; i <= unit2; i++) {
need_format = 1;
if (format == PLAIN) {
need_format = 1;
}
else {
need_format = 0;
}
unit[i].label[0] = "";
unit[i].label[1] = "";

switch (unit[i].type) {
case CELL_COUNTS:
need_format = 0;
unit[i].len = 5;
unit[i].label[0] = " cell";
unit[i].label[1] = "count";
ns = 0;
sprintf(num, "%ld", count_sum(&ns, -1));
len = strlen(num);
if (len > unit[i].len)
unit[i].len = len;

if (need_format) {
need_format = 0;
unit[i].len = 5;
ns = 0;
sprintf(num, "%ld", count_sum(&ns, -1));
len = strlen(num);
if (len > unit[i].len)
unit[i].len = len;
}
break;

case PERCENT_COVER:
need_format = 0;
unit[i].dp = 2;
unit[i].len = 6;
unit[i].label[0] = " % ";
unit[i].label[1] = "cover";
unit[i].eformat = 0;

if (need_format) {
need_format = 0;
unit[i].dp = 2;
unit[i].len = 6;
unit[i].eformat = 0;
}
break;

case SQ_METERS:
Expand Down Expand Up @@ -90,6 +91,24 @@ int print_report(int unit1, int unit2)
&unit[i].dp, &(unit[i].eformat), e_format);
}
}
}

int print_report(int unit1, int unit2)
{
int ns, nl, nx;
char num[100];
int len, new;
CELL *cats, *prev;
int first;
int i;
int divider_level;
int after_header;
int with_stats;
char *cp;
int spacing;
char dot;

compute_unit_format(unit1, unit2, PLAIN);

/* figure out how big the category numbers are when printed */
for (nl = 0; nl < nlayers; nl++)
Expand Down
Loading

0 comments on commit a0be16c

Please sign in to comment.