/
qgis.g.info.c
135 lines (120 loc) · 3.99 KB
/
qgis.g.info.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/****************************************************************************
*
* MODULE: qgis.g.info
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com>
* using various GRASS modules
* PURPOSE: get informations about locations,mapsets,maps
* COPYRIGHT: (C) 2010 by Radim Blazek
*
* This program is free software under the GNU General Public
* License (>=v2).
*
*****************************************************************************/
#define PACKAGE "grassmods"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/display.h>
#include <grass/glocale.h>
#include <grass/gprojects.h>
int main(int argc, char **argv)
{
struct GModule *module;
struct Option *info_opt, *rast_opt, *vect_opt, *coor_opt;
struct Cell_head window;
/* Initialize the GIS calls */
G_gisinit(argv[0]);
module = G_define_module();
module->description = _("Get info about locations,mapsets,maps");
info_opt = G_define_option();
info_opt->key = "info";
info_opt->type = TYPE_STRING;
info_opt->description = "info key";
info_opt->options = "proj,window,query";
rast_opt = G_define_standard_option(G_OPT_R_INPUT);
rast_opt->key = "rast";
rast_opt->required = NO;
vect_opt = G_define_standard_option(G_OPT_V_INPUT);
vect_opt->key = "vect";
vect_opt->required = NO;
coor_opt = G_define_option();
coor_opt->key = "coor";
coor_opt->type = TYPE_DOUBLE;
coor_opt->multiple = YES;
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
if ( strcmp("proj",info_opt->answer) == 0 )
{
G_get_window(&window);
/* code from g.proj */
if (window.proj != PROJECTION_XY) {
struct Key_Value *projinfo, *projunits;
char *wkt;
projinfo = G_get_projinfo();
projunits = G_get_projunits();
wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 );
fprintf (stdout, "%s", wkt);
}
}
else if ( strcmp("window",info_opt->answer) == 0 )
{
if ( rast_opt->answer )
{
G_get_cellhd( rast_opt->answer, "", &window);
fprintf (stdout, "%f,%f,%f,%f", window.west, window.south,window.east,window.north );
}
else if ( vect_opt->answer )
{
G_fatal_error ("Not yet supported");
}
}
else if ( strcmp("query",info_opt->answer) == 0 )
{
double x, y;
int row, col;
x = atof ( coor_opt->answers[0] );
y = atof ( coor_opt->answers[1] );
if ( rast_opt->answer )
{
int fd;
RASTER_MAP_TYPE rast_type;
DCELL *dcell;
CELL *cell;
G_get_cellhd( rast_opt->answer, "", &window);
G_set_window(&window);
fd = G_open_cell_old( rast_opt->answer, "");
col = (int) G_easting_to_col( x, &window);
row = (int) G_northing_to_row( y, &window);
if (col == window.cols) col--;
if (row == window.rows) row--;
rast_type = G_get_raster_map_type(fd);
cell = G_allocate_c_raster_buf();
dcell = G_allocate_d_raster_buf();
if (rast_type == CELL_TYPE)
{
if (G_get_c_raster_row(fd, cell, row) < 0)
{
G_fatal_error(_("Unable to read raster map <%s> row %d"),
rast_opt->answer, row);
}
fprintf (stdout, "value:%d\n", cell[col] );
}
else
{
if (G_get_d_raster_row(fd, dcell, row) < 0)
{
G_fatal_error(_("Unable to read raster map <%s> row %d"),
rast_opt->answer, row);
}
fprintf (stdout, "value:%f\n", dcell[col] );
}
}
else if ( vect_opt->answer )
{
G_fatal_error ("Not yet supported");
}
}
exit(EXIT_SUCCESS);
}