Skip to content

Commit

Permalink
GRASS raster data provider
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@12880 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Feb 4, 2010
1 parent 9f6a286 commit 728ecc6
Show file tree
Hide file tree
Showing 8 changed files with 927 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/plugins/grass/qgsgrassplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,9 @@ void QgsGrassPlugin::addRaster()
QString name = uri.right( uri.length() - pos - 1 );
name.replace( '/', ' ' );

qGisInterface->addRasterLayer( uri, sel->map );
//qGisInterface->addRasterLayer( uri, sel->map );
qGisInterface->addRasterLayer( uri, sel->map, "grassraster", QStringList(), QStringList(),
QString(), QString() );
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/providers/grass/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ ADD_DEFINITIONS(-DGRASS_BASE=\\\"${GRASS_PREFIX}\\\")

SET(GRASS_PROVIDER_SRCS provider.cpp)

SET(GRASS_RASTER_PROVIDER_SRCS qgsgrassrasterprovider.cpp)

SET(GRASS_LIB_SRCS qgsgrassprovider.cpp qgsgrass.cpp)

SET(QGIS_D_RAST_SRCS qgis.d.rast.c)

SET(QGIS_G_INFO_SRCS qgis.g.info.c)

########################################################
# Build

Expand Down Expand Up @@ -54,6 +60,32 @@ TARGET_LINK_LIBRARIES (grassprovider
qgisgrass
)

ADD_LIBRARY (grassrasterprovider MODULE ${GRASS_RASTER_PROVIDER_SRCS})

IF (WIN32)
SET_TARGET_PROPERTIES(grassrasterprovider PROPERTIES COMPILE_FLAGS "\"-DGRASS_EXPORT=__declspec(dllimport)\"" )
ELSE (WIN32)
SET_TARGET_PROPERTIES(grassrasterprovider PROPERTIES COMPILE_FLAGS "-DGRASS_EXPORT=" )
ENDIF (WIN32)

TARGET_LINK_LIBRARIES (grassrasterprovider
qgisgrass
qgis_core
)

ADD_EXECUTABLE(qgis.d.rast ${QGIS_D_RAST_SRCS})

TARGET_LINK_LIBRARIES (qgis.d.rast
${GRASS_LIBRARY_gis}
)

ADD_EXECUTABLE(qgis.g.info ${QGIS_G_INFO_SRCS})

TARGET_LINK_LIBRARIES (qgis.g.info
${GRASS_LIBRARY_gis}
${GRASS_LIBRARY_gproj}
)

########################################################
# Install

Expand All @@ -64,3 +96,12 @@ INSTALL(TARGETS qgisgrass
INSTALL(TARGETS grassprovider
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})

INSTALL(TARGETS grassrasterprovider
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})

INSTALL(TARGETS qgis.d.rast qgis.g.info
RUNTIME DESTINATION ${QGIS_DATA_DIR}/grass/modules
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)
171 changes: 171 additions & 0 deletions src/providers/grass/qgis.d.rast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/****************************************************************************
*
* MODULE: qgis.d.rast
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com>
* using d.rast from GRASS
* PURPOSE: display raster maps in active graphics display
* COPYRIGHT: (C) 2010 by Radim Blazek
*
* This program is free software under the GNU General Public
* License (>=v2).
*
*****************************************************************************/
#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>

int display(char *name, char *mapset, RASTER_MAP_TYPE data_type);

int main(int argc, char **argv)
{
char *mapset;
char *name;
int fp;
int cols, rows;
struct GModule *module;
struct Option *map;
struct Option *win;
struct Flag *flag_o;
struct Flag *flag_i;
struct Flag *flag_x;
struct Cell_head window;

/* Initialize the GIS calls */
G_gisinit(argv[0]);

module = G_define_module();
module->keywords = _("display, raster");
module->description = _("Output raster map layers in a format suitable for display in QGIS");

map = G_define_standard_option(G_OPT_R_MAP);
map->description = _("Raster map to be displayed");

win = G_define_option();
win->key = "window";
win->type = TYPE_DOUBLE;
win->multiple = YES;
win->description = "xmin,ymin,xmax,ymax,ncols,nrows";

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

name = map->answer;

G_get_window(&window);
window.west = atof(win->answers[0]);
window.south = atof(win->answers[1]);
window.east = atof(win->answers[2]);
window.north = atof(win->answers[3]);
window.cols = atoi(win->answers[4]);
window.rows = atoi(win->answers[5]);
G_adjust_Cell_head(&window,1,1);
G_set_window(&window);

/* Make sure map is available */
mapset = G_find_cell2(name, "");
if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), name);


fp = G_raster_map_is_fp(name, mapset);

/* use DCELL even if the map is FCELL */
if (fp)
display(name, mapset, DCELL_TYPE );
else
display(name, mapset, CELL_TYPE );

exit(EXIT_SUCCESS);
}

static int cell_draw(char *, char *, struct Colors *, RASTER_MAP_TYPE);

int display(char *name,
char *mapset,
RASTER_MAP_TYPE data_type)
{
struct Colors colors;
int r, g, b;

if (G_read_colors(name, mapset, &colors) == -1)
G_fatal_error(_("Color file for <%s> not available"), name);

//G_set_null_value_color(r, g, b, &colors);

/* Go draw the raster map */
cell_draw(name, mapset, &colors, data_type);

/* release the colors now */
G_free_colors(&colors);

return 0;
}

static int cell_draw(char *name,
char *mapset,
struct Colors *colors,
RASTER_MAP_TYPE data_type)
{
int cellfile;
void *xarray;
int row;
int t, b, l, r;
int ncols, nrows;
static unsigned char *red, *grn, *blu, *set;
int i;
void *ptr;
int big_endian;
long one= 1;

big_endian = !(*((char *)(&one)));

ncols = G_window_cols();
nrows = G_window_rows();

/* Make sure map is available */
if ((cellfile = G_open_cell_old(name, mapset)) == -1)
G_fatal_error(_("Unable to open raster map <%s>"), name);

/* Allocate space for cell buffer */
xarray = G_allocate_raster_buf(data_type);
red = G_malloc(ncols);
grn = G_malloc(ncols);
blu = G_malloc(ncols);
set = G_malloc(ncols);

/* loop for array rows */
for ( row = 0; row < nrows; row++ ) {
G_get_raster_row(cellfile, xarray, row, data_type);
ptr = xarray;

G_lookup_raster_colors(xarray, red, grn, blu, set, ncols, colors,
data_type);

for (i = 0; i < ncols; i++) {
unsigned char alpha = 255;
if ( G_is_null_value(ptr, data_type) ) {
alpha = 0;
}
ptr = G_incr_void_ptr(ptr, G_raster_size(data_type));


// We need data suitable for QImage 32-bpp
// the data are stored in QImage as QRgb which is unsigned int.
// Because it depends on byte order of the platform we have to
// consider byte order (well, middle endian ignored)
if ( big_endian ) {
// I have never tested this
fprintf(stdout, "%c%c%c%c", alpha, red[i],grn[i],blu[i]);
} else {
fprintf(stdout, "%c%c%c%c", blu[i],grn[i],red[i],alpha);
}
}
}

G_close_cell(cellfile);
return (0);
}
135 changes: 135 additions & 0 deletions src/providers/grass/qgis.g.info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/****************************************************************************
*
* 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).
*
*****************************************************************************/
#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)
{
char *mapset;
char *name;
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 = G_easting_to_col( x, &window);
row = 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);
}

Loading

0 comments on commit 728ecc6

Please sign in to comment.