Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g.region: add JSON support #3941

Merged
merged 11 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion general/g.region/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ MODULE_TOPDIR = ../..

PGM = g.region

LIBES = $(GPROJLIB) $(VECTORLIB) $(DIG2LIB) $(RASTER3DLIB) $(RASTERLIB) $(GISLIB) $(MATHLIB) $(PROJLIB)
LIBES = $(GPROJLIB) $(VECTORLIB) $(DIG2LIB) $(RASTER3DLIB) $(RASTERLIB) $(GISLIB) $(MATHLIB) $(PROJLIB) $(PARSONLIB)
DEPENDENCIES = $(GPROJDEP) $(VECTORDEP) $(DIG2DEP) $(RASTER3DDEP) $(RASTERDEP) $(GISDEP)
EXTRA_INC = $(VECT_INC) $(PROJINC)
EXTRA_CFLAGS = $(VECT_CFLAGS)
Expand Down
33 changes: 33 additions & 0 deletions general/g.region/g.region.html
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,39 @@ <h3>Using g.region in a shell in combination with GDAL</h3>
Here the input raster map does not have to match the project's
coordinate reference system since it is reprojected on the fly.

<h3>JSON Output</h3>
<div class="code"><pre>
g.region -p format=json
</pre></div>
<div class="code"><pre>
{
"projection": "99 (Lambert Conformal Conic)",
"zone": 0,
"datum": "nad83",
"ellipsoid": "a=6378137 es=0.006694380022900787",
"region": {
"north": 320000,
"south": 10000,
"west": 120000,
"east": 935000,
"ns-res": 500,
"ns-res3": 1000,
"ew-res": 500,
"ew-res3": 1000
},
"top": 500,
"bottom": -500,
"tbres": 100,
"rows": 620,
"rows3": 310,
"cols": 1630,
"cols3": 815,
"depths": 10,
"cells": 1010600,
"cells3": 2526500
}
</pre></div>

kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved
<h2>SEE ALSO</h2>

<em>
Expand Down
7 changes: 6 additions & 1 deletion general/g.region/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
#define PRINT_GMT 0x200
#define PRINT_WMS 0x400

#include <grass/parson.h>

enum OutputFormat { PLAIN, SHELL, JSON };

/* zoom.c */
int zoom(struct Cell_head *, const char *, const char *);

/* printwindow.c */
void print_window(struct Cell_head *, int, int);
void print_window(struct Cell_head *, int, int, enum OutputFormat,
JSON_Object *);

#endif
44 changes: 42 additions & 2 deletions general/g.region/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <grass/raster.h>
#include <grass/raster3d.h>
#include <grass/vector.h>
#include <grass/parson.h>
#include <grass/glocale.h>
#include "local_proto.h"

Expand All @@ -41,6 +42,9 @@ int main(int argc, char *argv[])
char **rast_ptr, **vect_ptr;
int pix;
bool update_file = false;
enum OutputFormat format;
JSON_Value *root_value;
JSON_Object *root_object;

struct GModule *module;
struct {
Expand All @@ -51,7 +55,7 @@ int main(int argc, char *argv[])
struct {
struct Option *north, *south, *east, *west, *top, *bottom, *res, *nsres,
*ewres, *res3, *tbres, *rows, *cols, *save, *region, *raster,
*raster3d, *align, *zoom, *vect, *grow;
*raster3d, *align, *zoom, *vect, *grow, *format;
} parm;

G_gisinit(argv[0]);
Expand Down Expand Up @@ -358,6 +362,13 @@ int main(int argc, char *argv[])
parm.save->gisprompt = "new,windows,region";
parm.save->guisection = _("Effects");

parm.format = G_define_standard_option(G_OPT_F_FORMAT);
parm.format->options = "plain,shell,json";
parm.format->descriptions = _("plain;Plain text output;"
"shell;shell script style output;"
"json;JSON (JavaScript Object Notation);");
parm.format->guisection = _("Print");

G_option_required(
flag.dflt, flag.savedefault, flag.print, flag.lprint, flag.eprint,
flag.center, flag.gmt_style, flag.wms_style, flag.dist_res, flag.nangle,
Expand Down Expand Up @@ -421,6 +432,24 @@ int main(int argc, char *argv[])
print_flag |= PRINT_REG;
}

if (strcmp(parm.format->answer, "json") == 0) {
format = JSON;
root_value = json_value_init_object();
kritibirda26 marked this conversation as resolved.
Show resolved Hide resolved
if (root_value == NULL) {
G_fatal_error(
_("Failed to initialize JSON object. Out of memory?"));
}
root_object = json_object(root_value);
}
else if (strcmp(parm.format->answer, "shell") == 0 ||
(print_flag & PRINT_SH)) {
format = SHELL;
print_flag |= PRINT_SH;
}
else {
format = PLAIN;
}

if (flag.dflt->answer)
update_file = true;
else
Expand Down Expand Up @@ -903,7 +932,18 @@ int main(int argc, char *argv[])
} /* / flag.savedefault->answer */

if (print_flag)
print_window(&window, print_flag, flat_flag);
print_window(&window, print_flag, flat_flag, format, root_object);

if (format == JSON) {
char *serialized_string = NULL;
serialized_string = json_serialize_to_string_pretty(root_value);
if (serialized_string == NULL) {
G_fatal_error(_("Failed to initialize pretty JSON string."));
}
puts(serialized_string);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
}

exit(EXIT_SUCCESS);
}
Expand Down
Loading
Loading