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.proj: Add JSON support #5419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions general/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ build_program_in_subdir(
DEPENDS
grass_gis
grass_gproj
grass_parson
PROJ::proj
OPTIONAL_DEPENDS
GDAL::GDAL)
Expand Down
2 changes: 1 addition & 1 deletion general/g.proj/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MODULE_TOPDIR = ../..
PGM = g.proj

EXTRA_INC = $(PROJINC) $(GDALCFLAGS)
LIBES = $(GPROJLIB) $(GISLIB) $(GDALLIBS) $(PROJLIB)
LIBES = $(GPROJLIB) $(GISLIB) $(GDALLIBS) $(PROJLIB) $(PARSONLIB)
DEPENDENCIES = $(GPROJDEP) $(GISDEP)

include $(MODULE_TOPDIR)/include/Make/Module.make
Expand Down
52 changes: 52 additions & 0 deletions general/g.proj/g.proj.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ Print the CRS information for the current project:
g.proj -p
```

Print the CRS information for the current project in JSON format:

```sh
g.proj -p format=json
```

Print the CRS information for the current project in WKT JSON format:

```sh
g.proj -w format=json
```

Print the CRS information for the current project in PROJ.4 JSON format:

```sh
g.proj -j format=json
```

List the possible datum transformation parameters for the current
project:

Expand Down Expand Up @@ -216,6 +234,40 @@ Reproject external vector map to current GRASS project using the OGR
ogr2ogr -t_srs "`g.proj -wf`" polbnda_italy_GB_ovest.shp polbnda_italy_LL.shp
```

### Using g.proj JSON output with pandas

Using the CRS information for the current project in JSON format with pandas:

```python
import grass.script as gs
import pandas as pd

# Run g.proj to get CRS information in JSON format.
proj_data = gs.parse_command("g.proj", flags="p", format="json")

df = pd.DataFrame(proj_data, index=[0]).T
print(df)
```

```sh
0
name Lambert Conformal Conic
proj lcc
datum nad83
a 6378137.0
es 0.006694380022900787
lat_1 36.16666666666666
lat_2 34.33333333333334
lat_0 33.75
lon_0 -79
x_0 609601.22
y_0 0
no_defs defined
unit Meter
units Meters
meters 1
```

## REFERENCES

[PROJ](https://proj.org): Projection/datum support library
Expand Down
8 changes: 5 additions & 3 deletions general/g.proj/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ extern struct Key_Value *projinfo, *projunits, *projepsg;
extern char *projsrid, *projwkt;
extern struct Cell_head cellhd;

enum OutputFormat { PLAIN, JSON, SHELL };

/* input.c */
void input_currloc(void);

Expand All @@ -16,12 +18,12 @@ int input_georef(char *);
#endif

/* output.c */
void print_projinfo(int);
void print_projinfo(enum OutputFormat);
void print_datuminfo(void);
void print_proj4(int);
void print_proj4(int, enum OutputFormat);

#ifdef HAVE_OGR
void print_wkt(int, int);
void print_wkt(int, int, enum OutputFormat);
#endif

/* datumtrans.c */
Expand Down
30 changes: 26 additions & 4 deletions general/g.proj/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ int main(int argc, char *argv[])
#endif
*listcodes, /* list codes of given authority */
*datum, /* datum to add (or replace existing datum) */
*dtrans; /* index to datum transform option */
*dtrans, /* index to datum transform option */
*format; /* output format */
struct GModule *module;

int formats;
enum OutputFormat outputFormat;
const char *epsg = NULL;

/* We don't call G_gisinit() here because it validates the
Expand Down Expand Up @@ -223,11 +225,31 @@ int main(int argc, char *argv[])
location->guisection = _("Create");
location->description = _("Name of new project (location) to create");

format = G_define_standard_option(G_OPT_F_FORMAT);
format->guisection = _("Print");

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

/* Initialisation & Validation */

if (strcmp(format->answer, "json") == 0) {
outputFormat = JSON;
}
else if (shellinfo->answer) {
outputFormat = SHELL;
}
else {
outputFormat = PLAIN;
}

if (outputFormat == JSON &&
!(printinfo->answer || printproj4->answer || printwkt->answer)) {
G_fatal_error(_("The 'format=json' option can only be used with one of "
"the -%c, -%c, or -%c flags"),
printinfo->key, printproj4->key, printwkt->key);
}

/* list codes for given authority */
if (listcodes->answer) {
list_codes(listcodes->answer);
Expand Down Expand Up @@ -326,14 +348,14 @@ int main(int argc, char *argv[])
#endif
}
if (printinfo->answer || shellinfo->answer)
print_projinfo(shellinfo->answer);
print_projinfo(outputFormat);
else if (datuminfo->answer)
print_datuminfo();
else if (printproj4->answer)
print_proj4(dontprettify->answer);
print_proj4(dontprettify->answer, outputFormat);
#ifdef HAVE_OGR
else if (printwkt->answer)
print_wkt(esristyle->answer, dontprettify->answer);
print_wkt(esristyle->answer, dontprettify->answer, outputFormat);
#endif
else if (location->answer)
create_location(location->answer);
Expand Down
Loading
Loading