Skip to content

Commit bfabb85

Browse files
committed
[GRASS] various import fixes
1 parent f0eeef7 commit bfabb85

File tree

10 files changed

+136
-78
lines changed

10 files changed

+136
-78
lines changed

src/core/qgsdataitem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ void QgsDataItem::setState( State state )
644644
{
645645
if ( !mPopulatingIcon )
646646
{
647+
// TODO: ensure that QgsAnimatedIcon is created on UI thread only
647648
mPopulatingIcon = new QgsAnimatedIcon( QgsApplication::iconPath( "/mIconLoading.gif" ) );
648649
}
649650
mPopulatingIcon->connectFrameChanged( this, SLOT( emitDataChanged() ) );

src/core/qgsdataitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class QgsDataItem;
3838
typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
3939

4040
/** Animated icon is keeping an animation running if there are listeners connected to frameChanged */
41-
class QgsAnimatedIcon : public QObject
41+
class CORE_EXPORT QgsAnimatedIcon : public QObject
4242
{
4343
Q_OBJECT
4444
public:

src/providers/grass/qgis.r.in.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C"
3838
#include <QDataStream>
3939
#include <QFile>
4040
#include <QIODevice>
41+
#include <QTextStream>
4142

4243
#include "qgsrectangle.h"
4344
#include "qgsrasterblock.h"
@@ -80,10 +81,13 @@ int main( int argc, char **argv )
8081
name = map->answer;
8182

8283
QFile stdinFile;
83-
stdinFile.open( 0, QIODevice::ReadOnly );
84-
84+
stdinFile.open( stdin, QIODevice::ReadOnly );
8585
QDataStream stdinStream( &stdinFile );
8686

87+
QFile stdoutFile;
88+
stdoutFile.open( stdout, QIODevice::WriteOnly );
89+
QDataStream stdoutStream( &stdoutFile );
90+
8791
QgsRectangle extent;
8892
qint32 rows, cols;
8993
stdinStream >> extent >> cols >> rows;
@@ -167,6 +171,9 @@ int main( int argc, char **argv )
167171
ptr = G_incr_void_ptr( ptr, G_raster_size( grass_type ) );
168172
}
169173
G_put_raster_row( cf, buf, grass_type );
174+
175+
stdoutStream << ( bool )true; // row written
176+
stdoutFile.flush();
170177
}
171178

172179
if ( isCanceled )

src/providers/grass/qgis.v.in.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ int main( int argc, char **argv )
105105
exit( EXIT_FAILURE );
106106

107107
QFile stdinFile;
108-
stdinFile.open( 0, QIODevice::ReadOnly );
108+
stdinFile.open( stdin, QIODevice::ReadOnly );
109109
QDataStream stdinStream( &stdinFile );
110110

111111
QFile stdoutFile;
112-
stdoutFile.open( 0, QIODevice::ReadOnly );
112+
stdoutFile.open( stdout, QIODevice::WriteOnly );
113113
QDataStream stdoutStream( &stdoutFile );
114114

115115
qint32 typeQint32;
@@ -178,6 +178,8 @@ int main( int argc, char **argv )
178178
{
179179
exitIfCanceled( stdinStream, isPolygon, tmpName, &tmpMap, finalName, &finalMap );
180180
stdinStream >> feature;
181+
stdoutStream << ( bool )true; // feature received
182+
stdoutFile.flush();
181183
if ( !feature.isValid() )
182184
{
183185
break;
@@ -278,6 +280,29 @@ int main( int argc, char **argv )
278280
break;
279281
}
280282
}
283+
// TODO: review if necessary and if there is GRASS function
284+
// remove zero length
285+
int nlines = Vect_get_num_lines( map );
286+
struct line_pnts *line = Vect_new_line_struct();
287+
for ( int i = 1; i <= nlines; i++ )
288+
{
289+
if ( !Vect_line_alive( map, i ) )
290+
{
291+
continue;
292+
}
293+
294+
int type = Vect_read_line( map, line, NULL, i );
295+
if ( !( type & GV_BOUNDARY ) )
296+
{
297+
continue;
298+
}
299+
300+
if ( Vect_line_length( line ) == 0 )
301+
{
302+
Vect_delete_line( map, i );
303+
}
304+
}
305+
281306
Vect_merge_lines( map, GV_BOUNDARY, NULL, NULL );
282307
#if GRASS_VERSION_MAJOR < 7
283308
Vect_remove_bridges( map, NULL );
@@ -310,6 +335,8 @@ int main( int argc, char **argv )
310335
{
311336
exitIfCanceled( stdinStream, isPolygon, tmpName, &tmpMap, finalName, &finalMap );
312337
stdinStream >> feature;
338+
stdoutStream << ( bool )true; // feature received
339+
stdoutFile.flush();
313340
if ( !feature.isValid() )
314341
{
315342
break;
@@ -357,6 +384,7 @@ int main( int argc, char **argv )
357384
Vect_close( &finalMap );
358385

359386
stdoutStream << ( bool )true; // to keep caller waiting until finished
387+
stdoutFile.flush();
360388
// TODO history
361389

362390
exit( EXIT_SUCCESS );

src/providers/grass/qgsgrass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ QRegExp GRASS_LIB_EXPORT QgsGrassObject::newNameRegExp( Type type )
212212
return rx;
213213
}
214214

215+
bool QgsGrassObject::operator==( const QgsGrassObject& other ) const
216+
{
217+
return mGisdbase == other.mGisdbase && mLocation == other.mLocation && mMapset == other.mMapset
218+
&& mName == other.mName && mType == other.mType;
219+
}
220+
215221
#ifdef Q_OS_WIN
216222
#include <windows.h>
217223
QString GRASS_LIB_EXPORT QgsGrass::shortPath( const QString &path )

src/providers/grass/qgsgrass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class GRASS_LIB_EXPORT QgsGrassObject
107107
bool mapsetIdentical( const QgsGrassObject &other ) const;
108108
// get regexp patter for new names, e.g. vectors should not start with number
109109
static QRegExp newNameRegExp( Type type );
110+
111+
bool operator==( const QgsGrassObject& other ) const;
110112
private:
111113
QString mGisdbase;
112114
QString mLocation;

src/providers/grass/qgsgrassimport.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ QStringList QgsGrassImport::names() const
9191
return list;
9292
}
9393

94+
bool QgsGrassImport::isCanceled() const
95+
{
96+
return mCanceled;
97+
}
98+
99+
void QgsGrassImport::cancel()
100+
{
101+
QgsDebugMsg( "entered" );
102+
mCanceled = true;
103+
}
104+
94105
//------------------------------ QgsGrassRasterImport ------------------------------------
95106
QgsGrassRasterImport::QgsGrassRasterImport( QgsRasterPipe* pipe, const QgsGrassObject& grassObject,
96107
const QgsRectangle &extent, int xSize, int ySize )
@@ -214,6 +225,11 @@ bool QgsGrassRasterImport::import()
214225
// calculate reasonable block size (5MB)
215226
int maximumTileHeight = 5000000 / mXSize;
216227
maximumTileHeight = std::max( 1, maximumTileHeight );
228+
// smaller if reprojecting so that it can be canceled quickly
229+
if ( mPipe->projector() )
230+
{
231+
maximumTileHeight = std::max( 1, 100000 / mXSize );
232+
}
217233

218234
QgsRasterIterator iter( mPipe->last() );
219235
iter.setMaximumTileWidth( mXSize );
@@ -226,6 +242,7 @@ bool QgsGrassRasterImport::import()
226242
int iterCols = 0;
227243
int iterRows = 0;
228244
QgsRasterBlock* block = 0;
245+
process->setReadChannel( QProcess::StandardOutput );
229246
while ( iter.readNextRasterPart( band, iterCols, iterRows, &block, iterLeft, iterTop ) )
230247
{
231248
for ( int row = 0; row < iterRows; row++ )
@@ -246,6 +263,11 @@ bool QgsGrassRasterImport::import()
246263
}
247264
outStream << false; // not canceled
248265
outStream << byteArray;
266+
267+
// wait until the row is written to allow quick cancel (don't send data to buffer)
268+
process->waitForReadyRead();
269+
bool result;
270+
outStream >> result;
249271
}
250272
delete block;
251273
if ( isCanceled() )
@@ -450,17 +472,25 @@ bool QgsGrassVectorImport::import()
450472
}
451473
outStream << false; // not canceled
452474
outStream << feature;
475+
476+
// wait until the feature is written to allow quick cancel (don't send data to buffer)
477+
process->waitForReadyRead();
478+
bool result;
479+
outStream >> result;
453480
}
454481
feature = QgsFeature(); // indicate end by invalid feature
455482
outStream << false; // not canceled
456483
outStream << feature;
457-
QgsDebugMsg( "features sent" );
484+
485+
process->waitForReadyRead();
486+
bool result;
487+
outStream >> result;
458488
}
459489
iterator.close();
460490

461491
process->setReadChannel( QProcess::StandardOutput );
462-
463492
bool result;
493+
process->waitForReadyRead();
464494
outStream >> result;
465495
QgsDebugMsg( QString( "result = %1" ).arg( result ) );
466496

src/providers/grass/qgsgrassimport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class GRASS_LIB_EXPORT QgsGrassImport : public QObject
3939
// get error if import failed
4040
QString error();
4141
virtual QStringList names() const;
42-
bool isCanceled() const { return mCanceled; }
42+
bool isCanceled() const;
4343
public slots:
4444
void onFinished();
4545
// TODO: this is not completely kosher, because QgsGrassImport exist on the main thread
4646
// but import is running in another thread, to do it right, we should have an import object
4747
// created on another thread, send cancel signal to that object which regularly processes events
4848
// and thus recieves the signal.
4949
// Most probably however, it will work correctly, even if read/write the bool wasn't atomic
50-
void cancel() { mCanceled = true; }
50+
void cancel();
5151

5252
signals:
5353
// sent when process finished

0 commit comments

Comments
 (0)