Skip to content

Commit

Permalink
g.region: Add JSON support (#3941)
Browse files Browse the repository at this point in the history
* g.region: add JSON support

* add test and documentation

* add gmt, wms to output

* fix CI build issues

* separate projection code and name fields
  • Loading branch information
kritibirda26 authored Aug 11, 2024
1 parent f2c8cd9 commit 71f8a81
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 46 deletions.
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
53 changes: 53 additions & 0 deletions general/g.region/g.region.html
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,59 @@ <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>

<div class="code"><pre>
g.region -l format=json
</pre></div>
<div class="code"><pre>
{
"nw_long": -78.688888505507336,
"nw_lat": 35.743893244701788,
"ne_long": -78.669097826118957,
"ne_lat": 35.743841072010554,
"se_long": -78.669158624787542,
"se_lat": 35.728968779193615,
"sw_long": -78.688945667963168,
"sw_lat": 35.729020942542441,
"center_long": -78.679022655614958,
"center_lat": 35.736431420327719,
"rows": 165,
"cols": 179
}
</pre></div>

<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();
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

0 comments on commit 71f8a81

Please sign in to comment.