Skip to content

Commit

Permalink
[GRASS][FEATURE] raster import via browser drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed May 16, 2015
1 parent d88f77f commit 06167d4
Show file tree
Hide file tree
Showing 13 changed files with 1,051 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/core/qgsrectangle.cpp
Expand Up @@ -356,3 +356,20 @@ void QgsRectangle::invert()
tmp = xmin; xmin = ymin; ymin = tmp;
tmp = xmax; xmax = ymax; ymax = tmp;
}

QDataStream& operator<<( QDataStream& out, const QgsRectangle& rectangle )
{
out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
return out;
}

QDataStream& operator>>( QDataStream& in, QgsRectangle& rectangle )
{
double xmin, ymin, xmax, ymax;
in >> xmin >> ymin >> xmax >> ymax;
rectangle.setXMinimum( xmin );
rectangle.setYMinimum( ymin );
rectangle.setXMaximum( xmax );
rectangle.setYMaximum( ymax );
return in;
}
4 changes: 4 additions & 0 deletions src/core/qgsrectangle.h
Expand Up @@ -150,6 +150,10 @@ class CORE_EXPORT QgsRectangle

};

/** Writes the list rectangle to stream out. QGIS version compatibility is not guaranteed. */
QDataStream& operator<<( QDataStream& out, const QgsRectangle& rectangle );
/** Reads a rectangle from stream in into rectangle. QGIS version compatibility is not guaranteed. */
QDataStream& operator>>( QDataStream& in, QgsRectangle& rectangle );

inline QgsRectangle::~QgsRectangle()
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -202,6 +202,7 @@ qgsmessagelogviewer.cpp
qgsmessageviewer.cpp
qgsnewhttpconnection.cpp
qgsnewmemorylayerdialog.cpp
qgsnewnamedialog.cpp
qgsnewvectorlayerdialog.cpp
qgsnumericsortlistviewitem.cpp
qgsoptionsdialogbase.cpp
Expand Down Expand Up @@ -309,6 +310,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsmessageviewer.h
qgsnewhttpconnection.h
qgsnewmemorylayerdialog.h
qgsnewnamedialog.h
qgsnewvectorlayerdialog.h
qgsoptionsdialogbase.h
qgsowssourceselect.h
Expand Down
38 changes: 34 additions & 4 deletions src/providers/grass/CMakeLists.txt
Expand Up @@ -5,6 +5,7 @@
INCLUDE_DIRECTORIES(
../../core
../../core/raster
../../gui
${GDAL_INCLUDE_DIR}
${PROJ_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
Expand All @@ -20,9 +21,9 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
SET(GRASS_MAJOR_VERSION ${GRASS_MAJOR_VERSION${GRASS_BUILD_VERSION}})
SET(GRASS_MINOR_VERSION ${GRASS_MINOR_VERSION${GRASS_BUILD_VERSION}})

QT4_WRAP_CPP(GRASS_LIBRARY_MOC_SRCS ../qgsgrassprovider.h)
QT4_WRAP_CPP(GRASS_LIBRARY_MOC_SRCS ../qgsgrassprovider.h ../qgsgrassimport.h)

ADD_LIBRARY(qgisgrass${GRASS_BUILD_VERSION} SHARED ../qgsgrass.cpp ../qgsgrassfeatureiterator.cpp ../qgsgrassprovider.cpp ${GRASS_LIBRARY_MOC_SRCS})
ADD_LIBRARY(qgisgrass${GRASS_BUILD_VERSION} SHARED ../qgsgrass.cpp ../qgsgrassfeatureiterator.cpp ../qgsgrassprovider.cpp ../qgsgrassimport.cpp ${GRASS_LIBRARY_MOC_SRCS})

SET_TARGET_PROPERTIES(qgisgrass${GRASS_BUILD_VERSION} PROPERTIES
CLEAN_DIRECT_OUTPUT 1
Expand Down Expand Up @@ -83,7 +84,10 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
SET_TARGET_PROPERTIES(grassprovider${GRASS_BUILD_VERSION} PROPERTIES
COMPILE_FLAGS "-DGRASS_BASE=\\\"${GRASS_PREFIX}\\\" \"-DGRASS_EXPORT=${DLLEXPORT}\" \"-DGRASS_LIB_EXPORT=${DLLIMPORT}\""
)
TARGET_LINK_LIBRARIES(grassprovider${GRASS_BUILD_VERSION} qgisgrass${GRASS_BUILD_VERSION})
TARGET_LINK_LIBRARIES(grassprovider${GRASS_BUILD_VERSION}
qgisgrass${GRASS_BUILD_VERSION}
qgis_gui
)

#
# grass raster provider
Expand Down Expand Up @@ -142,6 +146,32 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
TARGET_LINK_LIBRARIES(qgis.g.info${GRASS_BUILD_VERSION} m)
ENDIF (UNIX)

#
# grass raster import module
#
ADD_EXECUTABLE(qgis.r.in${GRASS_BUILD_VERSION} ../qgis.r.in.cpp)
SET_TARGET_PROPERTIES(qgis.r.in${GRASS_BUILD_VERSION} PROPERTIES
COMPILE_FLAGS "-DGRASS_BASE=\\\"${GRASS_PREFIX}\\\" \"-DGRASS_LIB_EXPORT=${DLLEXPORT}\" \"-DGRASS_EXPORT=${DLLIMPORT}\""
)
IF (GRASS_MAJOR_VERSION LESS 7 )
TARGET_LINK_LIBRARIES(qgis.r.in${GRASS_BUILD_VERSION}
qgisgrass${GRASS_BUILD_VERSION}
${GRASS_LIBRARY${GRASS_BUILD_VERSION}_gis}
${GRASS_LIBRARY${GRASS_BUILD_VERSION}_datetime}
${GDAL_LIBRARY}
qgis_core
)
ELSE (GRASS_MAJOR_VERSION LESS 7 )
TARGET_LINK_LIBRARIES(qgis.r.in${GRASS_BUILD_VERSION}
qgisgrass${GRASS_BUILD_VERSION}
${GRASS_LIBRARY${GRASS_BUILD_VERSION}_gis}
${GRASS_LIBRARY${GRASS_BUILD_VERSION}_datetime}
${GRASS_LIBRARY${GRASS_BUILD_VERSION}_raster}
${GDAL_LIBRARY}
qgis_core
)
ENDIF (GRASS_MAJOR_VERSION LESS 7 )

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

Expand All @@ -158,7 +188,7 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})

INSTALL(TARGETS qgis.d.rast${GRASS_BUILD_VERSION} qgis.g.info${GRASS_BUILD_VERSION}
INSTALL(TARGETS qgis.d.rast${GRASS_BUILD_VERSION} qgis.g.info${GRASS_BUILD_VERSION} qgis.r.in${GRASS_BUILD_VERSION}
RUNTIME DESTINATION ${QGIS_LIBEXEC_DIR}/grass/modules
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)
Expand Down
177 changes: 177 additions & 0 deletions src/providers/grass/qgis.r.in.cpp
@@ -0,0 +1,177 @@
/***************************************************************************
qgis.r.in.cpp
---------------------
begin : May 2015
copyright : (C) 2015 by Radim Blazek
email : radim dot blazek at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

extern "C"
{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#ifdef WIN32
#include <fcntl.h>
#include <io.h>
#endif
#include <grass/version.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/display.h>

#ifdef _MSC_VER
#include <float.h>
#endif
}

#include <QByteArray>
#include <QDataStream>
#include <QFile>
#include <QIODevice>

#include "qgsrectangle.h"
#include "qgsrasterblock.h"
#include "qgsgrass.h"

//#ifdef _MSC_VER
//#define INFINITY (DBL_MAX+DBL_MAX)
//#define NAN (INFINITY-INFINITY)
//#endif

#if GRASS_VERSION_MAJOR >= 7
#define G_allocate_raster_buf Rast_allocate_buf
#define G_close_cell Rast_close
#define G_get_raster_map_type Rast_get_map_type
#define G_get_raster_row Rast_get_row
#define G_is_null_value Rast_is_null_value
#define G_open_raster_new Rast_open_new
#define G_short_history Rast_short_history
#define G_command_history Rast_command_history
#define G_write_history Rast_write_history
#define G_set_raster_value_c Rast_set_c_value
#define G_set_raster_value_f Rast_set_f_value
#define G_set_raster_value_d Rast_set_d_value
#define G_put_raster_row Rast_put_row
#define G_raster_size Rast_cell_size
#endif

int main( int argc, char **argv )
{
char *name;
struct GModule *module;
struct Option *map;
struct Cell_head window;
int cf;

G_gisinit( argv[0] );

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

map = G_define_standard_option( G_OPT_R_OUTPUT );

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

name = map->answer;

QFile stdinFile;
stdinFile.open(0, QIODevice::ReadOnly);

QDataStream stdinStream(&stdinFile);

QgsRectangle extent;
qint32 rows, cols;
stdinStream >> extent >> cols >> rows;

//G_fatal_error("i = %d", i);
//G_fatal_error( extent.toString().toAscii().data() );

QString err = QgsGrass::setRegion( &window, extent, rows, cols);
if ( !err.isEmpty() )
{
G_fatal_error("Cannot set region: %s", err.toUtf8().data());
}

G_set_window( &window );

QGis::DataType qgis_type;
qint32 type;
//stdinStream >> grass_type;
stdinStream >>type;
qgis_type = (QGis::DataType)type;

RASTER_MAP_TYPE grass_type;
switch ( qgis_type ) {
case QGis::Int32:
grass_type = CELL_TYPE;
break;
case QGis::Float32:
grass_type = FCELL_TYPE;
break;
case QGis::Float64:
grass_type = DCELL_TYPE;
break;
default:
G_fatal_error("QGIS data type %d not supported", qgis_type);
}

cf = G_open_raster_new(name, grass_type);
if (cf < 0)
{
G_fatal_error( "Unable to create raster map <%s>", name);
}

void *buf = G_allocate_raster_buf(grass_type);

int expectedSize = cols * QgsRasterBlock::typeSize( qgis_type );
QByteArray byteArray;
for ( int row = 0; row < rows; row++ ) {
stdinStream >> byteArray;
if ( byteArray.size() != expectedSize ) {
G_fatal_error("Wrong byte array size, expected %d bytes, got %d", expectedSize, byteArray.size());
}

qint32 *cell;
float *fcell;
double *dcell;
if ( grass_type == CELL_TYPE )
cell = (qint32*) byteArray.data();
else if ( grass_type == FCELL_TYPE )
fcell = (float*) byteArray.data();
else if ( grass_type == DCELL_TYPE )
dcell = (double*) byteArray.data();

void *ptr = buf;
for ( int col = 0; col < cols; col++ )
{
if ( grass_type == CELL_TYPE )
G_set_raster_value_c(ptr, (CELL)cell[col], grass_type);
else if ( grass_type == FCELL_TYPE )
G_set_raster_value_f(ptr, (FCELL)fcell[col], grass_type);
else if ( grass_type == DCELL_TYPE )
G_set_raster_value_d(ptr, (DCELL)dcell[col], grass_type);

ptr = G_incr_void_ptr( ptr, G_raster_size(grass_type) );
}
G_put_raster_row(cf, buf, grass_type);
}

G_close_cell(cf);
struct History history;
G_short_history(name, "raster", &history);
G_command_history(&history);
G_write_history(name, &history);

exit( EXIT_SUCCESS );
}

0 comments on commit 06167d4

Please sign in to comment.