Skip to content

Commit 4dc8499

Browse files
committedDec 6, 2012
GRASS Direct
1 parent bfa124f commit 4dc8499

19 files changed

+2336
-365
lines changed
 

‎src/core/raster/qgsrasterblock.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,16 @@ bool QgsRasterBlock::convert( QgsRasterBlock::DataType destDataType )
413413
}
414414
QgsFree( mData );
415415
mData = data;
416+
mDataType = destDataType;
417+
mTypeSize = typeSize( mDataType );
416418
}
417419
else if ( typeIsColor( mDataType ) && typeIsColor( destDataType ) )
418420
{
419421
QImage::Format format = imageFormat( destDataType );
420422
QImage image = mImage->convertToFormat( format );
421423
*mImage = image;
424+
mDataType = destDataType;
425+
mTypeSize = typeSize( mDataType );
422426
}
423427
else
424428
{
@@ -516,7 +520,7 @@ void * QgsRasterBlock::convert( void *srcData, QgsRasterBlock::DataType srcDataT
516520
double value = readValue( srcData, srcDataType, i );
517521
writeValue( destData, destDataType, i, value );
518522
//double newValue = readValue( destData, destDataType, i );
519-
//QgsDebugMsg( QString("convert type %1 to %2: %3 -> %4").arg(srcDataType).arg(destDataType).arg( value ).arg( newValue ) );
523+
//QgsDebugMsg( QString("convert %1 type %2 to %3: %4 -> %5").arg(i).arg(srcDataType).arg(destDataType).arg( value ).arg( newValue ) );
520524
}
521525
return destData;
522526
}

‎src/core/raster/qgsrasterprojector.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,10 @@ QgsRasterBlock * QgsRasterProjector::block( int bandNo, QgsRectangle const & ex
701701
QgsRasterBlock *outputBlock = new QgsRasterBlock();
702702
if ( !mInput )
703703
{
704+
QgsDebugMsg( "Input not set" );
704705
return outputBlock;
705706
}
706707

707-
708708
if ( ! mSrcCRS.isValid() || ! mDestCRS.isValid() || mSrcCRS == mDestCRS )
709709
{
710710
QgsDebugMsg( "No projection necessary" );
@@ -723,6 +723,7 @@ QgsRasterBlock * QgsRasterProjector::block( int bandNo, QgsRectangle const & ex
723723
// If we zoom out too much, projector srcRows / srcCols maybe 0, which can cause problems in providers
724724
if ( srcRows() <= 0 || srcCols() <= 0 )
725725
{
726+
QgsDebugMsg( "Zero srcRows or srcCols" );
726727
return outputBlock;
727728
}
728729

@@ -737,20 +738,23 @@ QgsRasterBlock * QgsRasterProjector::block( int bandNo, QgsRectangle const & ex
737738

738739
size_t pixelSize = QgsRasterBlock::typeSize( mInput->dataType( bandNo ) );
739740

740-
if ( !outputBlock->reset( QgsRasterBlock::ARGB32_Premultiplied, width, height ) )
741+
if ( !outputBlock->reset( mInput->dataType( bandNo ), width, height ) )
741742
{
743+
QgsDebugMsg( "Cannot reset block" );
742744
delete inputBlock;
743745
return outputBlock;
744746
}
747+
outputBlock->setNoDataValue( mInput->noDataValue( bandNo ) );
745748

746-
// TODO: fill by transparent
749+
// TODO: fill by no data or transparent
747750

748751
int srcRow, srcCol;
749752
for ( int i = 0; i < height; ++i )
750753
{
751754
for ( int j = 0; j < width; ++j )
752755
{
753756
srcRowCol( i, j, &srcRow, &srcCol );
757+
QgsDebugMsgLevel( QString( "row = %1 col = %2 srcRow = %3 srcCol = %4" ).arg( i ).arg( j ).arg( srcRow ).arg( srcCol ), 5 );
754758
size_t srcIndex = srcRow * mSrcCols + srcCol;
755759
size_t destIndex = i * width + j;
756760

‎src/plugins/grass/modules/default.qgc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
</section>
247247
<section label="Create random points">
248248
<grass name="r.random"/>
249+
<grass name="r.random.raster"/>
249250
<grass name="r.random.cells"/>
250251
</section>
251252
<section label="Generate surface">
1.19 KB

Error rendering embedded code

Invalid image source.

1.77 KB

Error rendering embedded code

Invalid image source.

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Create random raster" module="r.random" direct="1">
5+
<option key="input"/>
6+
<option key="n" answer="1" hidden="no"/>
7+
<option key="raster_output" />
8+
</qgisgrassmodule>

‎src/plugins/grass/modules/r.surf.idw.qgm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
33

4-
<qgisgrassmodule label="Inverse distance squared weighting raster interpolation" module="r.surf.idw">
4+
<qgisgrassmodule label="Inverse distance squared weighting raster interpolation" module="r.surf.idw" direct="1">
55
<option key="input" />
66
<option key="npoints" />
77
<option key="output" />

‎src/plugins/grass/qgsgrassmapcalc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ QgsGrassMapcalc::QgsGrassMapcalc(
4242
QWidget * parent, Qt::WFlags f )
4343
: QMainWindow( iface->mainWindow(), Qt::Dialog )
4444
, QgsGrassMapcalcBase( )
45-
, QgsGrassModuleOptions( tools, module, iface )
45+
, QgsGrassModuleOptions( tools, module, iface, false )
4646
, mTool( -1 )
4747
, mObject( 0 )
4848
, mConnector( 0 )

‎src/plugins/grass/qgsgrassmodule.cpp

Lines changed: 590 additions & 244 deletions
Large diffs are not rendered by default.

‎src/plugins/grass/qgsgrassmodule.h

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "qgis.h"
2222
#include "qgsfield.h"
23+
#include "qgscoordinatereferencesystem.h"
2324

2425
#include <QCheckBox>
2526
#include <QGroupBox>
@@ -40,7 +41,6 @@ class QDomElement;
4041
class QLineEdit;
4142
class QValidator;
4243

43-
4444
/*! \class QgsGrassModule
4545
* \brief Interface to GRASS modules.
4646
*
@@ -50,15 +50,30 @@ class QgsGrassModule: public QDialog, private Ui::QgsGrassModuleBase
5050
Q_OBJECT
5151

5252
public:
53+
class Description
54+
{
55+
public:
56+
QString label;
57+
// supported by GRASS Direct
58+
bool direct;
59+
Description(): direct( true ) {}
60+
Description( QString lab, bool dir = false ): label( lab ), direct( dir ) { }
61+
Description( const Description & desc ) { label = desc.label; direct = desc.direct; }
62+
Description( Description & desc ) { label = desc.label; direct = desc.direct; }
63+
};
64+
5365
//! Constructor
5466
QgsGrassModule( QgsGrassTools *tools, QString moduleName, QgisInterface *iface,
55-
QString path, QWidget *parent, Qt::WFlags f = 0 );
67+
QString path, bool direct, QWidget *parent, Qt::WFlags f = 0 );
5668

5769
//! Destructor
5870
~QgsGrassModule();
5971

6072
QString translate( QString string );
6173

74+
//! Returns module description (info from .qgs file) for module description path
75+
static Description description( QString path );
76+
6277
//! Returns module label for module description path
6378
static QString label( QString path );
6479

@@ -93,6 +108,9 @@ class QgsGrassModule: public QDialog, private Ui::QgsGrassModuleBase
93108
// Returns empty list if not found.
94109
static QStringList execArguments( QString module );
95110

111+
//! Returns true if module is direct
112+
bool isDirect() { return mDirect; }
113+
96114
signals:
97115
// ! emitted when the module started
98116
void moduleStarted();
@@ -122,6 +140,9 @@ class QgsGrassModule: public QDialog, private Ui::QgsGrassModuleBase
122140
//! Read module's standard error
123141
void readStderr();
124142

143+
//! Call on mapset change, i.e. also possible direct/indirect mode change
144+
void mapsetChanged();
145+
125146
private:
126147
//! Pointer to the QGIS interface object
127148
QgisInterface *mIface;
@@ -161,6 +182,9 @@ class QgsGrassModule: public QDialog, private Ui::QgsGrassModuleBase
161182

162183
//! True if the module successfully finished
163184
bool mSuccess;
185+
186+
//! Direct mode
187+
bool mDirect;
164188
};
165189

166190
/*! \class QgsGrassModuleOptions
@@ -170,10 +194,15 @@ class QgsGrassModule: public QDialog, private Ui::QgsGrassModuleBase
170194
class QgsGrassModuleOptions
171195
{
172196
public:
197+
enum RegionMode
198+
{
199+
RegionInput = 1, // intersection of input maps extent and highest input resolution
200+
RegionCurrent = 0 // current map canvas extent and resolution
201+
};
173202
//! Constructor
174203
QgsGrassModuleOptions(
175204
QgsGrassTools *tools, QgsGrassModule *module,
176-
QgisInterface *iface );
205+
QgisInterface *iface, bool direct );
177206

178207
//! Destructor
179208
virtual ~QgsGrassModuleOptions();
@@ -218,8 +247,8 @@ class QgsGrassModuleOptions
218247
//! Get region covering all input maps
219248
// \param all true all input maps
220249
// \param all false only the mas which were switched on
221-
virtual bool inputRegion( struct Cell_head *window, bool all )
222-
{ Q_UNUSED( window ); Q_UNUSED( all ); return false; }
250+
virtual bool inputRegion( struct Cell_head *window, QgsCoordinateReferenceSystem & crs, bool all )
251+
{ Q_UNUSED( window ); Q_UNUSED( crs ); Q_UNUSED( all ); return false; }
223252

224253
// ! Flag names
225254
virtual QStringList flagNames() { return QStringList() ; }
@@ -242,6 +271,12 @@ class QgsGrassModuleOptions
242271

243272
//! QGIS directory
244273
QString mAppDir;
274+
275+
//! Region mode select box
276+
QComboBox * mRegionModeComboBox;
277+
278+
//! Direct mode
279+
bool mDirect;
245280
};
246281

247282
/*! \class QgsGrassModuleStandardOptions
@@ -258,7 +293,7 @@ class QgsGrassModuleStandardOptions: QWidget, public QgsGrassModuleOptions
258293
QgsGrassTools *tools, QgsGrassModule *module,
259294
QgisInterface *iface,
260295
QString xname, QDomElement docElem,
261-
QWidget * parent = 0, Qt::WFlags f = 0 );
296+
bool direct, QWidget * parent = 0, Qt::WFlags f = 0 );
262297

263298
//! Destructor
264299
~QgsGrassModuleStandardOptions();
@@ -282,7 +317,7 @@ class QgsGrassModuleStandardOptions: QWidget, public QgsGrassModuleOptions
282317
QStringList checkRegion();
283318
bool usesRegion();
284319
bool requestsRegion();
285-
bool inputRegion( struct Cell_head *window, bool all );
320+
bool inputRegion( struct Cell_head *window, QgsCoordinateReferenceSystem & crs, bool all );
286321
QStringList flagNames() { return mFlagNames; }
287322

288323
public slots:
@@ -357,7 +392,7 @@ class QgsGrassModuleItem
357392
* \param gnode option node in GRASS module XML description file
358393
*/
359394
QgsGrassModuleItem( QgsGrassModule *module, QString key,
360-
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode );
395+
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode, bool direct );
361396

362397
//! Destructor
363398
virtual ~QgsGrassModuleItem();
@@ -404,6 +439,8 @@ class QgsGrassModuleItem
404439
//! Is it required
405440
bool mRequired;
406441

442+
bool mDirect;
443+
407444
private:
408445

409446
};
@@ -425,7 +462,7 @@ class QgsGrassModuleGroupBoxItem: public QGroupBox, public QgsGrassModuleItem
425462
*/
426463
QgsGrassModuleGroupBoxItem( QgsGrassModule *module, QString key,
427464
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode,
428-
QWidget * parent = 0 );
465+
bool direct, QWidget * parent = 0 );
429466

430467
//! Destructor
431468
virtual ~QgsGrassModuleGroupBoxItem();
@@ -454,7 +491,7 @@ class QgsGrassModuleOption: public QgsGrassModuleGroupBoxItem
454491
*/
455492
QgsGrassModuleOption( QgsGrassModule *module, QString key,
456493
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode,
457-
QWidget * parent = 0 );
494+
bool direct, QWidget * parent = 0 );
458495

459496
//! Destructor
460497
~QgsGrassModuleOption();
@@ -501,6 +538,9 @@ class QgsGrassModuleOption: public QgsGrassModuleGroupBoxItem
501538
// Remove one line edit for multiple options
502539
void removeLineEdit();
503540

541+
// Browse output
542+
void browse( bool checked );
543+
504544
private:
505545
//! Control type
506546
ControlType mControlType;
@@ -557,7 +597,7 @@ class QgsGrassModuleFlag: public QgsGrassModuleCheckBox, public QgsGrassModuleIt
557597
*/
558598
QgsGrassModuleFlag( QgsGrassModule *module, QString key,
559599
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode,
560-
QWidget * parent = 0 );
600+
bool direct, QWidget * parent = 0 );
561601

562602
//! Destructor
563603
~QgsGrassModuleFlag();
@@ -584,7 +624,7 @@ class QgsGrassModuleInput: public QgsGrassModuleGroupBoxItem
584624
QgsGrassModuleInput( QgsGrassModule *module,
585625
QgsGrassModuleStandardOptions *options, QString key,
586626
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode,
587-
QWidget * parent = 0 );
627+
bool direct, QWidget * parent = 0 );
588628

589629
//! Destructor
590630
~QgsGrassModuleInput();
@@ -664,6 +704,9 @@ class QgsGrassModuleInput: public QgsGrassModuleGroupBoxItem
664704
//! Pointers to vector layers in combobox
665705
std::vector<QgsMapLayer*> mMapLayers;
666706

707+
//! Vector of band numbers in combobox for rasters in direct mode
708+
QList<int> mBands;
709+
667710
//! Attribute fields of layers in the combobox
668711
std::vector< std::vector<QgsField> > mVectorFields;
669712

@@ -693,7 +736,7 @@ class QgsGrassModuleGdalInput: public QgsGrassModuleGroupBoxItem
693736
*/
694737
QgsGrassModuleGdalInput( QgsGrassModule *module, int type, QString key,
695738
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode,
696-
QWidget * parent = 0 );
739+
bool direct, QWidget * parent = 0 );
697740

698741
//! Destructor
699742
~QgsGrassModuleGdalInput();
@@ -757,7 +800,7 @@ class QgsGrassModuleField: public QgsGrassModuleGroupBoxItem
757800
QgsGrassModuleStandardOptions *options,
758801
QString key,
759802
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode,
760-
QWidget * parent = 0 );
803+
bool direct, QWidget * parent = 0 );
761804

762805
//! Destructor
763806
~QgsGrassModuleField();
@@ -805,7 +848,7 @@ class QgsGrassModuleSelection: public QgsGrassModuleGroupBoxItem
805848
QString key,
806849
QDomElement &qdesc, QDomElement &gdesc,
807850
QDomNode &gnode,
808-
QWidget * parent = 0 );
851+
bool direct, QWidget * parent = 0 );
809852

810853
//! Destructor
811854
~QgsGrassModuleSelection();
@@ -855,7 +898,7 @@ class QgsGrassModuleFile: public QgsGrassModuleGroupBoxItem
855898
QString key,
856899
QDomElement &qdesc, QDomElement &gdesc,
857900
QDomNode &gnode,
858-
QWidget * parent = 0 );
901+
bool direct, QWidget * parent = 0 );
859902

860903
//! Destructor
861904
~QgsGrassModuleFile();

‎src/plugins/grass/qgsgrassplugin.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,21 @@ void QgsGrassPlugin::mapsetChanged()
221221
{
222222
if ( !QgsGrass::activeMode() )
223223
{
224-
mOpenToolsAction->setEnabled( false );
224+
mOpenToolsAction->setEnabled( true );
225225
mRegionAction->setEnabled( false );
226226
mEditRegionAction->setEnabled( false );
227227
mRegionBand->reset();
228228
mCloseMapsetAction->setEnabled( false );
229229
mNewVectorAction->setEnabled( false );
230230

231+
#if 0
231232
if ( mTools )
232233
{
233234
mTools->hide();
234235
delete mTools;
235236
mTools = 0;
236237
}
238+
#endif
237239
}
238240
else
239241
{
@@ -248,10 +250,12 @@ void QgsGrassPlugin::mapsetChanged()
248250
mRegionAction->setChecked( on );
249251
switchRegion( on );
250252

253+
#if 0
251254
if ( mTools )
252255
{
253256
mTools->mapsetChanged();
254257
}
258+
#endif
255259
QString gisdbase = QgsGrass::getDefaultGisdbase();
256260
QString location = QgsGrass::getDefaultLocation();
257261
try
@@ -268,6 +272,7 @@ void QgsGrassPlugin::mapsetChanged()
268272
setTransform();
269273
redrawRegion();
270274
}
275+
if ( mTools ) mTools->mapsetChanged();
271276
}
272277

273278
void QgsGrassPlugin::saveMapset()

‎src/plugins/grass/qgsgrasstools.cpp

Lines changed: 213 additions & 84 deletions
Large diffs are not rendered by default.

‎src/plugins/grass/qgsgrasstools.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class QgsGrassTools: public QDialog, private Ui::QgsGrassToolsBase
5050

5151
//! Recursively add sections and modules to the list view
5252
// If parent is 0, the modules are added to mModulesListView root
53-
void addModules( QTreeWidgetItem *parent, QDomElement &element );
53+
void addModules( QTreeWidgetItem *parent, QDomElement &element, QTreeWidget *modulesTreeWidget, QStandardItemModel * modulesListModel, bool direct );
5454

5555
//! Returns application directory
5656
QString appDir();
5757

5858
public slots:
5959
//! Load configuration from file
60-
bool loadConfig( QString filePath );
60+
bool loadConfig( QString filePath, QTreeWidget *modulesTreeWidget, QStandardItemModel * modulesListModel, bool direct );
6161

6262
//! Close
6363
void close( void );
@@ -73,6 +73,7 @@ class QgsGrassTools: public QDialog, private Ui::QgsGrassToolsBase
7373

7474
//! Module in list clicked
7575
void moduleClicked( QTreeWidgetItem * item, int column );
76+
void directModuleClicked( QTreeWidgetItem * item, int column );
7677

7778
//! Current mapset changed
7879
void mapsetChanged();
@@ -85,10 +86,12 @@ class QgsGrassTools: public QDialog, private Ui::QgsGrassToolsBase
8586

8687
//! Update the regex used to filter the modules list (autoconnect to ui)
8788
void on_mFilterInput_textChanged( QString theText );
89+
void on_mDirectFilterInput_textChanged( QString theText );
8890
//! Run a module when its entry is clicked in the list view
8991
void listItemClicked( const QModelIndex &theIndex );
92+
void directListItemClicked( const QModelIndex &theIndex );
9093
//! Run a module given its module name e.g. r.in.gdal
91-
void runModule( QString name );
94+
void runModule( QString name, bool direct );
9295
signals:
9396
void regionChanged();
9497

@@ -102,14 +105,19 @@ class QgsGrassTools: public QDialog, private Ui::QgsGrassToolsBase
102105
//! Browser
103106
QgsGrassBrowser *mBrowser;
104107

105-
//
106-
// For experimental model & filtered model by Tim
107-
//
108-
QStandardItemModel * mModelTools;
108+
// For model & filtered model by Tim
109+
QStandardItemModel * mModulesListModel;
109110
QSortFilterProxyModel * mModelProxy;
110-
QListView * mListView2;
111-
QDockWidget * mDockWidget;
112111

112+
// Direct modules model list
113+
QStandardItemModel * mDirectModulesListModel;
114+
QSortFilterProxyModel * mDirectModelProxy;
115+
116+
void removeEmptyItems( QTreeWidget *tree );
117+
void removeEmptyItems( QTreeWidgetItem *item );
118+
119+
// Show (fill) / hide tabs according to direct/indirect mode
120+
void showTabs();
113121
};
114122

115123
#endif // QGSGRASSTOOLS_H

‎src/plugins/grass/qgsgrasstoolsbase.ui

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>350</width>
10-
<height>350</height>
9+
<width>525</width>
10+
<height>361</height>
1111
</rect>
1212
</property>
1313
<property name="minimumSize">
@@ -23,9 +23,9 @@
2323
<item row="0" column="0">
2424
<widget class="QTabWidget" name="mTabWidget">
2525
<property name="currentIndex">
26-
<number>1</number>
26+
<number>3</number>
2727
</property>
28-
<widget class="QWidget" name="modulesTree">
28+
<widget class="QWidget" name="mModulesTreeTab">
2929
<attribute name="title">
3030
<string>Modules Tree</string>
3131
</attribute>
@@ -50,7 +50,7 @@
5050
</item>
5151
</layout>
5252
</widget>
53-
<widget class="QWidget" name="modulesList">
53+
<widget class="QWidget" name="mModulesListTab">
5454
<attribute name="title">
5555
<string>Modules List</string>
5656
</attribute>
@@ -80,6 +80,61 @@
8080
</item>
8181
</layout>
8282
</widget>
83+
<widget class="QWidget" name="mDirectModulesTreeTab">
84+
<attribute name="title">
85+
<string>Direct Modules Tree</string>
86+
</attribute>
87+
<layout class="QGridLayout" name="gridLayout">
88+
<item row="0" column="0">
89+
<widget class="QTreeWidget" name="mDirectModulesTree">
90+
<property name="indentation">
91+
<number>8</number>
92+
</property>
93+
<property name="rootIsDecorated">
94+
<bool>false</bool>
95+
</property>
96+
<property name="wordWrap">
97+
<bool>true</bool>
98+
</property>
99+
<column>
100+
<property name="text">
101+
<string>1</string>
102+
</property>
103+
</column>
104+
</widget>
105+
</item>
106+
</layout>
107+
</widget>
108+
<widget class="QWidget" name="mDirectModulesListTab">
109+
<attribute name="title">
110+
<string>Direct Modules List</string>
111+
</attribute>
112+
<layout class="QGridLayout" name="gridLayout_3">
113+
<item row="0" column="0">
114+
<widget class="QLabel" name="label_2">
115+
<property name="text">
116+
<string>Filter</string>
117+
</property>
118+
</widget>
119+
</item>
120+
<item row="0" column="1">
121+
<widget class="QLineEdit" name="mDirectFilterInput"/>
122+
</item>
123+
<item row="1" column="0" colspan="2">
124+
<widget class="QListView" name="mDirectListView">
125+
<property name="horizontalScrollBarPolicy">
126+
<enum>Qt::ScrollBarAlwaysOff</enum>
127+
</property>
128+
<property name="alternatingRowColors">
129+
<bool>true</bool>
130+
</property>
131+
<property name="wordWrap">
132+
<bool>true</bool>
133+
</property>
134+
</widget>
135+
</item>
136+
</layout>
137+
</widget>
83138
</widget>
84139
</item>
85140
<item row="1" column="0">

‎src/providers/grass/CMakeLists.txt

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,153 @@ IF (APPLE)
4949
SET_TARGET_PROPERTIES(qgisgrass PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
5050
ENDIF (APPLE)
5151

52+
#
53+
# Fake GRASS gis library
54+
#
55+
56+
# Generate functions mapping for funcions used in original version
57+
# Create list of functions to be mapped
58+
SET ( FUNCTIONS
59+
"G_add_color_rule"
60+
"G_adjust_Cell_head"
61+
"G_align_window"
62+
"G_allocate_cell_buf"
63+
"G_allocate_raster_buf"
64+
"G__calloc"
65+
"G_col_to_easting"
66+
"G_define_flag"
67+
"G_define_module"
68+
"G_define_option"
69+
"G_define_standard_option"
70+
"G_free"
71+
"G_get_fp_range_min_max"
72+
"G_get_range_min_max"
73+
"G_get_window"
74+
"G_get_set_window"
75+
"G__init_null_patterns"
76+
"G_incr_void_ptr"
77+
"G_init_colors"
78+
"G_init_fp_range"
79+
"G_init_raster_cats"
80+
"G_init_range"
81+
"G_is_c_null_value"
82+
"G_is_d_null_value"
83+
"G__malloc"
84+
"G_percent"
85+
"G_program_name"
86+
"G_projection"
87+
"G_quant_add_rule"
88+
"G_quant_free"
89+
"G_quant_get_limits"
90+
"G_quant_init"
91+
"G__realloc"
92+
"G_raster_size"
93+
"G_row_to_northing"
94+
"G_set_c_null_value"
95+
"G_set_d_null_value"
96+
"G_set_d_raster_cat"
97+
"G_set_gisrc_mode"
98+
"G_set_null_value"
99+
"G_set_raster_cats_title"
100+
"G_set_raster_value_d"
101+
"G_set_window"
102+
"G_setenv"
103+
"G_strip"
104+
"G_suppress_masking"
105+
"G_trim_decimal"
106+
"G_update_fp_range"
107+
"G_update_range"
108+
"G_window_cols"
109+
"G_window_rows"
110+
"G_zero"
111+
)
112+
113+
# Read GRASS header file and create functions mapping
114+
FILE(READ "${GRASS_INCLUDE_DIR}/grass/gisdefs.h" HEADER_FILE)
115+
116+
# Function definitions in gisdefs.h may spread over more lines -> remove comments
117+
# and split by ';'
118+
# Remove comments and directives (some macros are lost)
119+
STRING(REGEX REPLACE "(/\\*([^*]|[\r\n]|(\\*+([^*/]|[\r\n])))*\\*+/)" "" HEADER_FILE "${HEADER_FILE}")
120+
STRING(REGEX REPLACE "#[^\r\n]*" "" HEADER_FILE "${HEADER_FILE}")
121+
122+
SET ( FUNCTIONS_MAP "// Auto generated by cmake, do not edit\n" )
123+
LIST ( APPEND FUNCTIONS_MAP "#include \"qgsgrassgislib.h\"\n" )
124+
LIST ( APPEND FUNCTIONS_MAP "#include \"qgslogger.h\"\n" )
125+
LIST ( APPEND FUNCTIONS_MAP "#include \"qgis.h\"\n" )
126+
FOREACH( ROW ${HEADER_FILE} )
127+
STRING(REGEX REPLACE "\n" " " ROW "${ROW}")
128+
STRING(REGEX REPLACE "__attribute__.*" "" ROW "${ROW}")
129+
#MESSAGE (STATUS, "ROW: ${ROW}")
130+
# Parse function declaration
131+
STRING( REGEX REPLACE ".*(G_[^\\(]*)\\(.*" "\\1" FUNCTION_NAME "${ROW}" )
132+
FOREACH( FN ${FUNCTIONS} )
133+
IF ( "${FN}" STREQUAL "${FUNCTION_NAME}" )
134+
LIST ( APPEND FUNCTIONS_MAP "// ${ROW}\n" )
135+
# \\*? and \\** patterns do not work, why?
136+
STRING( REGEX REPLACE "(.*)G_.*" "\\1" FUNCTION_TYPE "${ROW}" )
137+
STRING( REGEX REPLACE "\\*" "" FUNCTION_TYPE "${FUNCTION_TYPE}" )
138+
STRING( REGEX REPLACE ".*(\\*+) *G_.*" "\\1" POINTER "${ROW}" )
139+
IF ( NOT "${POINTER}" STREQUAL "*" )
140+
SET ( POINTER "" )
141+
ENDIF ( NOT "${POINTER}" STREQUAL "*" )
142+
STRING( REGEX REPLACE ".*G_[^\\(]*\\((.*)\\).*" "\\1" PARAM_TYPES "${ROW}" )
143+
SET ( PARAM_NAMES "" )
144+
SET ( PARAMS "" )
145+
IF ( NOT "${PARAM_TYPES}" STREQUAL "void" )
146+
STRING ( REGEX MATCHALL "[^,]+" PARAM_TYPE_LIST ${PARAM_TYPES} )
147+
SET ( I 0 )
148+
FOREACH( PARAM_TYPE ${PARAM_TYPE_LIST} )
149+
LIST ( APPEND PARAM_NAMES "p${I}" )
150+
LIST ( APPEND PARAMS "${PARAM_TYPE} p${I}" )
151+
MATH(EXPR I "${I} + 1")
152+
ENDFOREACH ( PARAM_TYPE )
153+
ENDIF ( NOT "${PARAM_TYPES}" STREQUAL "void" )
154+
155+
STRING( REPLACE ";" ", " PARAM_NAMES "${PARAM_NAMES}" )
156+
STRING( REPLACE ";" ", " PARAMS "${PARAMS}" )
157+
158+
# Declare function type
159+
LIST ( APPEND FUNCTIONS_MAP "typedef ${FUNCTION_TYPE} ${POINTER} ${FUNCTION_NAME}_type(${PARAM_TYPES})\;\n\n" )
160+
LIST ( APPEND FUNCTIONS_MAP "${FUNCTION_TYPE} GRASS_LIB_EXPORT ${POINTER} ${FUNCTION_NAME} ( ${PARAMS} ) {\n" )
161+
#LIST ( APPEND FUNCTIONS_MAP " QgsDebugMsg( \"Entered\" )\;\n" )
162+
163+
LIST ( APPEND FUNCTIONS_MAP " ${FUNCTION_NAME}_type* fn = (${FUNCTION_NAME}_type*) cast_to_fptr (QgsGrassGisLib::instance()->resolve( \"${FUNCTION_NAME}\" ))\;\n" )
164+
LIST ( APPEND FUNCTIONS_MAP " return fn( ${PARAM_NAMES} )\;\n")
165+
LIST ( APPEND FUNCTIONS_MAP "}\n\n" )
166+
ENDIF ( "${FN}" STREQUAL "${FUNCTION_NAME}" )
167+
ENDFOREACH ( FN )
168+
ENDFOREACH( ROW )
Code has comments. Press enter to view.
169+
170+
FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/qgsgrassgislibfunctions.cpp" ${FUNCTIONS_MAP})
171+
172+
173+
# Build fake library
174+
SET (FAKE_LIB_GRASS_GIS "grass_gis.${GRASS_VERSION}")
175+
ADD_LIBRARY( ${FAKE_LIB_GRASS_GIS} MODULE qgsgrassgislib.cpp qgsgrassgislibfunctions.cpp )
176+
177+
SET_TARGET_PROPERTIES(${FAKE_LIB_GRASS_GIS} PROPERTIES
178+
CLEAN_DIRECT_OUTPUT 1
179+
FRAMEWORK 1
180+
FRAMEWORK_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}"
181+
MACOSX_FRAMEWORK_INFO_PLIST "${CMAKE_SOURCE_DIR}/mac/framework.info.plist.in"
182+
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${COMPLETE_VERSION}
183+
MACOSX_FRAMEWORK_IDENTIFIER org.qgis.qgisgrassgislib
184+
COMPILE_FLAGS "\"-DGRASS_LIB_EXPORT=${DLLEXPORT}\" \"-DGRASS_EXPORT=${DLLIMPORT}\" -DGRASS_LIBRARY_GIS=\\\"${GRASS_LIBRARY_gis}\\\" \"-I${CMAKE_CURRENT_SOURCE_DIR}\" ")
185+
186+
SET_TARGET_PROPERTIES(${FAKE_LIB_GRASS_GIS} PROPERTIES
187+
VERSION ${COMPLETE_VERSION}
188+
SOVERSION ${COMPLETE_VERSION}
189+
)
190+
191+
TARGET_LINK_LIBRARIES(${FAKE_LIB_GRASS_GIS}
192+
qgis_core
193+
)
194+
195+
IF (APPLE)
196+
SET_TARGET_PROPERTIES(${FAKE_LIB_GRASS_GIS} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
197+
ENDIF (APPLE)
198+
52199
#
53200
# GRASS vector provider
54201
#
@@ -103,6 +250,10 @@ INSTALL(TARGETS qgisgrass
103250
RUNTIME DESTINATION ${QGIS_BIN_DIR}
104251
LIBRARY DESTINATION ${QGIS_LIB_DIR}
105252
FRAMEWORK DESTINATION ${QGIS_FW_SUBDIR})
253+
254+
INSTALL(TARGETS ${FAKE_LIB_GRASS_GIS}
255+
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
256+
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})
106257

107258
INSTALL(TARGETS grassprovider
108259
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}

‎src/providers/grass/qgsgrass.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,40 @@ void GRASS_LIB_EXPORT QgsGrass::extendRegion( struct Cell_head *source,
10711071
target->bottom = source->bottom;
10721072
}
10731073

1074+
void GRASS_LIB_EXPORT QgsGrass::initRegion( struct Cell_head *window )
1075+
{
1076+
window->format = 0;
1077+
window->rows = 0;
1078+
window->rows3 = 0;
1079+
window->cols = 0;
1080+
window->cols3 = 0;
1081+
window->depths = 1;
1082+
window->proj = -1;
1083+
window->zone = -1;
1084+
window->compressed = -1;
1085+
window->ew_res = 0.0;
1086+
window->ew_res3 = 1.0;
1087+
window->ns_res = 0.0;
1088+
window->ns_res3 = 1.0;
1089+
window->tb_res = 1.0;
1090+
window->top = 1.0;
1091+
window->bottom = 0.0;
1092+
window->west = 0;
1093+
window->south = 0;
1094+
window->east = 1;
1095+
window->north = 1;
1096+
window->rows = 1;
1097+
window->cols = 1;
1098+
}
1099+
1100+
void GRASS_LIB_EXPORT QgsGrass::setRegion( struct Cell_head *window, QgsRectangle rect )
1101+
{
1102+
window->west = rect.xMinimum();
1103+
window->south = rect.yMinimum();
1104+
window->east = rect.xMaximum();
1105+
window->north = rect.yMaximum();
1106+
}
1107+
10741108
bool GRASS_LIB_EXPORT QgsGrass::mapRegion( int type, QString gisbase,
10751109
QString location, QString mapset, QString map,
10761110
struct Cell_head *window )

‎src/providers/grass/qgsgrass.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ class QgsGrass
148148
QString mapsetName, QString element );
149149
static GRASS_LIB_EXPORT QStringList elements( QString mapsetPath, QString element );
150150

151+
//! Initialize GRASS region
152+
static GRASS_LIB_EXPORT void initRegion( struct Cell_head *window );
153+
//! Set region extent
154+
static GRASS_LIB_EXPORT void setRegion( struct Cell_head *window, QgsRectangle rect );
155+
151156
// ! Get map region
152157
static GRASS_LIB_EXPORT bool mapRegion( int type, QString gisbase,
153158
QString location, QString mapset, QString map,

‎src/providers/grass/qgsgrassgislib.cpp

Lines changed: 1038 additions & 0 deletions
Large diffs are not rendered by default.

‎src/providers/grass/qgsgrassgislib.h

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/***************************************************************************
2+
qgsgrassgislib.h - Fake GRASS gis lib
3+
-------------------
4+
begin : Nov 2012
5+
copyright : (C) 2012 by Radim Blazek
6+
email : radim dot blazek at gmail dot com
7+
***************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSGRASSGISLIB_H
17+
#define QGSGRASSGISLIB_H
18+
19+
// GRASS header files
20+
extern "C"
21+
{
22+
#include <grass/gis.h>
23+
#include <grass/form.h>
24+
}
25+
26+
#include <stdexcept>
27+
#include <qgscoordinatereferencesystem.h>
28+
#include <qgsdistancearea.h>
29+
#include <qgsexception.h>
30+
#include <qgsproviderregistry.h>
31+
#include <qgsrectangle.h>
32+
#include <qgsrasterdataprovider.h>
33+
#include <qgsrasterprojector.h>
34+
35+
#include <QLibrary>
36+
#include <QProcess>
37+
#include <QString>
38+
#include <QMap>
39+
#include <QHash>
40+
#include <QTemporaryFile>
41+
class QgsCoordinateReferenceSystem;
42+
class QgsRectangle;
43+
44+
class GRASS_LIB_EXPORT QgsGrassGisLib
45+
{
46+
public:
47+
// Region term is used in modules (g.region), internaly it is hold in structure
48+
// Cell_head, but variables keeping that struture are usually called window
49+
/*
50+
class Region
51+
{
52+
QgsRectangle extent;
53+
double ewRes; // east-west resolution
54+
double nsRes; // north south resolution
55+
};
56+
*/
57+
58+
class Raster
59+
{
60+
public:
61+
int fd; // fake file descriptor
62+
QString name; // name passed from grass module, uri
63+
QgsRasterDataProvider *provider;
64+
QgsRasterProjector *projector;
65+
// Input points to provider or projector
66+
QgsRasterInterface *input;
67+
int band;
68+
int row; // next row to be written
69+
Raster(): provider( 0 ), projector( 0 ), input( 0 ), band( 1 ), row( 0 ) {}
70+
71+
};
72+
73+
static GRASS_LIB_EXPORT QgsGrassGisLib* instance();
74+
75+
QgsGrassGisLib();
76+
77+
int G__gisinit( const char * version, const char * programName );
78+
char *G_find_cell2( const char * name, const char * mapset );
79+
int G_open_cell_old( const char *name, const char *mapset );
80+
int G_open_raster_new( const char *name, RASTER_MAP_TYPE wr_type );
81+
int G_close_cell( int fd );
82+
RASTER_MAP_TYPE G_raster_map_type( const char *name, const char *mapset );
83+
RASTER_MAP_TYPE G_get_raster_map_type( int fd );
84+
//int G_raster_map_is_fp( const char *name, const char *mapset );
85+
int G_read_fp_range( const char *name, const char *mapset, struct FPRange *drange );
86+
87+
int readRasterRow( int fd, void * buf, int row, RASTER_MAP_TYPE data_type, bool noDataAsZero = false );
88+
int G_put_raster_row( int fd, const void *buf, RASTER_MAP_TYPE data_type );
89+
int G_get_cellhd( const char *name, const char *mapset, struct Cell_head *cellhd );
90+
91+
double G_database_units_to_meters_factor( void );
92+
double G_distance( double e1, double n1, double e2, double n2 );
93+
94+
/** Get QGIS raster type for GRASS raster type */
95+
QgsRasterBlock::DataType qgisRasterType( RASTER_MAP_TYPE grassType );
96+
97+
/** Get GRASS raster type for QGIS raster type */
98+
RASTER_MAP_TYPE grassRasterType( QgsRasterBlock::DataType qgisType );
99+
100+
/** Grass does not seem to have any function to init Cell_head,
101+
* initialisation is done in G__read_Cell_head_array */
102+
void initCellHead( struct Cell_head *cellhd );
103+
104+
/** Get raster from map of opened rasters, open it if it is not yet open */
105+
Raster raster( QString name );
106+
107+
void * resolve( const char * symbol );
108+
109+
// Print error function set to be called by GRASS lib
110+
static GRASS_LIB_EXPORT int errorRoutine( const char *msg, int fatal );
111+
112+
// Error called by fake lib
113+
void fatal( QString msg );
114+
void warning( QString msg );
115+
116+
private:
117+
/** pointer to canonical Singleton object */
118+
static QgsGrassGisLib* _instance;
119+
120+
/** Original GRASS library handle */
121+
QLibrary mLibrary;
122+
123+
/** Raster maps, key is fake file descriptor */
124+
QMap<int, Raster> mRasters;
125+
126+
/** Region to be used for data processing and output */
127+
struct Cell_head mWindow;
128+
129+
/** Current region extent */
130+
QgsRectangle mExtent;
131+
/** Current region rows */
132+
int mRows;
133+
/** Current region columns */
134+
int mColumns;
135+
/** Current coordinate reference system */
136+
QgsCoordinateReferenceSystem mCrs;
137+
QgsDistanceArea mDistanceArea;
138+
};
139+
140+
#endif // QGSGRASSGISLIB_H

0 commit comments

Comments
 (0)
Please sign in to comment.