Skip to content

Commit 728ecc6

Browse files
author
rblazek
committed
GRASS raster data provider
git-svn-id: http://svn.osgeo.org/qgis/trunk@12880 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 9f6a286 commit 728ecc6

File tree

8 files changed

+927
-2
lines changed

8 files changed

+927
-2
lines changed

src/plugins/grass/qgsgrassplugin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,9 @@ void QgsGrassPlugin::addRaster()
398398
QString name = uri.right( uri.length() - pos - 1 );
399399
name.replace( '/', ' ' );
400400

401-
qGisInterface->addRasterLayer( uri, sel->map );
401+
//qGisInterface->addRasterLayer( uri, sel->map );
402+
qGisInterface->addRasterLayer( uri, sel->map, "grassraster", QStringList(), QStringList(),
403+
QString(), QString() );
402404
}
403405
}
404406

src/providers/grass/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ ADD_DEFINITIONS(-DGRASS_BASE=\\\"${GRASS_PREFIX}\\\")
55

66
SET(GRASS_PROVIDER_SRCS provider.cpp)
77

8+
SET(GRASS_RASTER_PROVIDER_SRCS qgsgrassrasterprovider.cpp)
9+
810
SET(GRASS_LIB_SRCS qgsgrassprovider.cpp qgsgrass.cpp)
911

12+
SET(QGIS_D_RAST_SRCS qgis.d.rast.c)
13+
14+
SET(QGIS_G_INFO_SRCS qgis.g.info.c)
15+
1016
########################################################
1117
# Build
1218

@@ -54,6 +60,32 @@ TARGET_LINK_LIBRARIES (grassprovider
5460
qgisgrass
5561
)
5662

63+
ADD_LIBRARY (grassrasterprovider MODULE ${GRASS_RASTER_PROVIDER_SRCS})
64+
65+
IF (WIN32)
66+
SET_TARGET_PROPERTIES(grassrasterprovider PROPERTIES COMPILE_FLAGS "\"-DGRASS_EXPORT=__declspec(dllimport)\"" )
67+
ELSE (WIN32)
68+
SET_TARGET_PROPERTIES(grassrasterprovider PROPERTIES COMPILE_FLAGS "-DGRASS_EXPORT=" )
69+
ENDIF (WIN32)
70+
71+
TARGET_LINK_LIBRARIES (grassrasterprovider
72+
qgisgrass
73+
qgis_core
74+
)
75+
76+
ADD_EXECUTABLE(qgis.d.rast ${QGIS_D_RAST_SRCS})
77+
78+
TARGET_LINK_LIBRARIES (qgis.d.rast
79+
${GRASS_LIBRARY_gis}
80+
)
81+
82+
ADD_EXECUTABLE(qgis.g.info ${QGIS_G_INFO_SRCS})
83+
84+
TARGET_LINK_LIBRARIES (qgis.g.info
85+
${GRASS_LIBRARY_gis}
86+
${GRASS_LIBRARY_gproj}
87+
)
88+
5789
########################################################
5890
# Install
5991

@@ -64,3 +96,12 @@ INSTALL(TARGETS qgisgrass
6496
INSTALL(TARGETS grassprovider
6597
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
6698
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})
99+
100+
INSTALL(TARGETS grassrasterprovider
101+
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
102+
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})
103+
104+
INSTALL(TARGETS qgis.d.rast qgis.g.info
105+
RUNTIME DESTINATION ${QGIS_DATA_DIR}/grass/modules
106+
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
107+
)

src/providers/grass/qgis.d.rast.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/****************************************************************************
2+
*
3+
* MODULE: qgis.d.rast
4+
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com>
5+
* using d.rast from GRASS
6+
* PURPOSE: display raster maps in active graphics display
7+
* COPYRIGHT: (C) 2010 by Radim Blazek
8+
*
9+
* This program is free software under the GNU General Public
10+
* License (>=v2).
11+
*
12+
*****************************************************************************/
13+
#include <stdlib.h>
14+
#include <stdio.h>
15+
#include <string.h>
16+
#include <grass/gis.h>
17+
#include <grass/raster.h>
18+
#include <grass/display.h>
19+
#include <grass/glocale.h>
20+
21+
int display(char *name, char *mapset, RASTER_MAP_TYPE data_type);
22+
23+
int main(int argc, char **argv)
24+
{
25+
char *mapset;
26+
char *name;
27+
int fp;
28+
int cols, rows;
29+
struct GModule *module;
30+
struct Option *map;
31+
struct Option *win;
32+
struct Flag *flag_o;
33+
struct Flag *flag_i;
34+
struct Flag *flag_x;
35+
struct Cell_head window;
36+
37+
/* Initialize the GIS calls */
38+
G_gisinit(argv[0]);
39+
40+
module = G_define_module();
41+
module->keywords = _("display, raster");
42+
module->description = _("Output raster map layers in a format suitable for display in QGIS");
43+
44+
map = G_define_standard_option(G_OPT_R_MAP);
45+
map->description = _("Raster map to be displayed");
46+
47+
win = G_define_option();
48+
win->key = "window";
49+
win->type = TYPE_DOUBLE;
50+
win->multiple = YES;
51+
win->description = "xmin,ymin,xmax,ymax,ncols,nrows";
52+
53+
if (G_parser(argc, argv))
54+
exit(EXIT_FAILURE);
55+
56+
name = map->answer;
57+
58+
G_get_window(&window);
59+
window.west = atof(win->answers[0]);
60+
window.south = atof(win->answers[1]);
61+
window.east = atof(win->answers[2]);
62+
window.north = atof(win->answers[3]);
63+
window.cols = atoi(win->answers[4]);
64+
window.rows = atoi(win->answers[5]);
65+
G_adjust_Cell_head(&window,1,1);
66+
G_set_window(&window);
67+
68+
/* Make sure map is available */
69+
mapset = G_find_cell2(name, "");
70+
if (mapset == NULL)
71+
G_fatal_error(_("Raster map <%s> not found"), name);
72+
73+
74+
fp = G_raster_map_is_fp(name, mapset);
75+
76+
/* use DCELL even if the map is FCELL */
77+
if (fp)
78+
display(name, mapset, DCELL_TYPE );
79+
else
80+
display(name, mapset, CELL_TYPE );
81+
82+
exit(EXIT_SUCCESS);
83+
}
84+
85+
static int cell_draw(char *, char *, struct Colors *, RASTER_MAP_TYPE);
86+
87+
int display(char *name,
88+
char *mapset,
89+
RASTER_MAP_TYPE data_type)
90+
{
91+
struct Colors colors;
92+
int r, g, b;
93+
94+
if (G_read_colors(name, mapset, &colors) == -1)
95+
G_fatal_error(_("Color file for <%s> not available"), name);
96+
97+
//G_set_null_value_color(r, g, b, &colors);
98+
99+
/* Go draw the raster map */
100+
cell_draw(name, mapset, &colors, data_type);
101+
102+
/* release the colors now */
103+
G_free_colors(&colors);
104+
105+
return 0;
106+
}
107+
108+
static int cell_draw(char *name,
109+
char *mapset,
110+
struct Colors *colors,
111+
RASTER_MAP_TYPE data_type)
112+
{
113+
int cellfile;
114+
void *xarray;
115+
int row;
116+
int t, b, l, r;
117+
int ncols, nrows;
118+
static unsigned char *red, *grn, *blu, *set;
119+
int i;
120+
void *ptr;
121+
int big_endian;
122+
long one= 1;
123+
124+
big_endian = !(*((char *)(&one)));
125+
126+
ncols = G_window_cols();
127+
nrows = G_window_rows();
128+
129+
/* Make sure map is available */
130+
if ((cellfile = G_open_cell_old(name, mapset)) == -1)
131+
G_fatal_error(_("Unable to open raster map <%s>"), name);
132+
133+
/* Allocate space for cell buffer */
134+
xarray = G_allocate_raster_buf(data_type);
135+
red = G_malloc(ncols);
136+
grn = G_malloc(ncols);
137+
blu = G_malloc(ncols);
138+
set = G_malloc(ncols);
139+
140+
/* loop for array rows */
141+
for ( row = 0; row < nrows; row++ ) {
142+
G_get_raster_row(cellfile, xarray, row, data_type);
143+
ptr = xarray;
144+
145+
G_lookup_raster_colors(xarray, red, grn, blu, set, ncols, colors,
146+
data_type);
147+
148+
for (i = 0; i < ncols; i++) {
149+
unsigned char alpha = 255;
150+
if ( G_is_null_value(ptr, data_type) ) {
151+
alpha = 0;
152+
}
153+
ptr = G_incr_void_ptr(ptr, G_raster_size(data_type));
154+
155+
156+
// We need data suitable for QImage 32-bpp
157+
// the data are stored in QImage as QRgb which is unsigned int.
158+
// Because it depends on byte order of the platform we have to
159+
// consider byte order (well, middle endian ignored)
160+
if ( big_endian ) {
161+
// I have never tested this
162+
fprintf(stdout, "%c%c%c%c", alpha, red[i],grn[i],blu[i]);
163+
} else {
164+
fprintf(stdout, "%c%c%c%c", blu[i],grn[i],red[i],alpha);
165+
}
166+
}
167+
}
168+
169+
G_close_cell(cellfile);
170+
return (0);
171+
}

src/providers/grass/qgis.g.info.c

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

0 commit comments

Comments
 (0)