Skip to content

Commit

Permalink
[GRASS] fixed hanging import of larger polygon layers on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Sep 26, 2015
1 parent 48d16ed commit 272dd9c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/providers/grass/qgis.v.in.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -221,16 +221,21 @@ int main( int argc, char **argv )
QgsFeature feature; QgsFeature feature;
struct line_cats *cats = Vect_new_cats_struct(); struct line_cats *cats = Vect_new_cats_struct();


qint32 featureCount = 0; qint32 featureCount;
stdinStream >> featureCount;

qint32 count = 0;
while ( true ) while ( true )
{ {
exitIfCanceled( stdinStream ); exitIfCanceled( stdinStream );
stdinStream >> feature; stdinStream >> feature;
checkStream( stdinStream ); checkStream( stdinStream );
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
// cannot be used on Windows, see notes in qgis.r.in // cannot be used on Windows, see notes in qgis.r.in
//#if 0
stdoutStream << true; // feature received stdoutStream << true; // feature received
stdoutFile.flush(); stdoutFile.flush();
//#endif
#endif #endif
if ( !feature.isValid() ) if ( !feature.isValid() )
{ {
Expand Down Expand Up @@ -310,7 +315,8 @@ int main( int argc, char **argv )
G_fatal_error( "Cannot insert: %s", e.what() ); G_fatal_error( "Cannot insert: %s", e.what() );
} }
} }
featureCount++; count++;
G_percent( count, featureCount, 1 );
} }
db_commit_transaction( driver ); db_commit_transaction( driver );
db_close_database_shutdown_driver( driver ); db_close_database_shutdown_driver( driver );
Expand Down Expand Up @@ -395,16 +401,20 @@ int main( int argc, char **argv )
centroids.insert( area, feature ); centroids.insert( area, feature );
spatialIndex.insertFeature( feature ); spatialIndex.insertFeature( feature );
} }

G_message( "Attaching input polygons to cleaned areas" ); G_message( "Attaching input polygons to cleaned areas" );
// read once more to assign centroids to polygons // read once more to assign centroids to polygons
count = 0;
while ( true ) while ( true )
{ {
exitIfCanceled( stdinStream ); exitIfCanceled( stdinStream );
stdinStream >> feature; stdinStream >> feature;
checkStream( stdinStream ); checkStream( stdinStream );
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
#if 0
stdoutStream << true; // feature received stdoutStream << true; // feature received
stdoutFile.flush(); stdoutFile.flush();
#endif
#endif #endif
if ( !feature.isValid() ) if ( !feature.isValid() )
{ {
Expand All @@ -426,12 +436,17 @@ int main( int argc, char **argv )
centroid.setAttributes( attr ); centroid.setAttributes( attr );
} }
} }
count++;
G_percent( count, featureCount, 1 );
} }


G_message( "Copying lines from temporary map" );
Vect_copy_map_lines( tmpMap, finalMap ); Vect_copy_map_lines( tmpMap, finalMap );
Vect_close( tmpMap ); Vect_close( tmpMap );
Vect_delete( tmpName.toUtf8().data() ); Vect_delete( tmpName.toUtf8().data() );


int centroidsCount = centroids.size();
count = 0;
foreach ( QgsFeature centroid, centroids.values() ) foreach ( QgsFeature centroid, centroids.values() )
{ {
QgsPoint point = centroid.geometry()->asPoint(); QgsPoint point = centroid.geometry()->asPoint();
Expand All @@ -445,12 +460,16 @@ int main( int argc, char **argv )
} }
writePoint( finalMap, GV_CENTROID, point, cats ); writePoint( finalMap, GV_CENTROID, point, cats );
} }
G_percent( count, centroidsCount, 1 );
} }
} }


G_message( "Building final map topology" );
Vect_build( finalMap ); Vect_build( finalMap );
Vect_close( finalMap ); Vect_close( finalMap );


G_message( "Done" );

stdoutStream << true; // to keep caller waiting until finished stdoutStream << true; // to keep caller waiting until finished
stdoutFile.flush(); stdoutFile.flush();
// TODO history // TODO history
Expand Down
27 changes: 25 additions & 2 deletions src/providers/grass/qgsgrassimport.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -588,9 +588,11 @@ bool QgsGrassVectorImport::import()


outStream << mProvider->fields(); outStream << mProvider->fields();


qint32 featureCount = mProvider->featureCount();
outStream << featureCount;

QgsFeatureIterator iterator = mProvider->getFeatures(); QgsFeatureIterator iterator = mProvider->getFeatures();
QgsFeature feature; QgsFeature feature;
mProgress->setRange( 1, mProvider->featureCount() );
mProgress->append( tr( "Writing features" ) ); mProgress->append( tr( "Writing features" ) );
for ( int i = 0; i < ( isPolygon ? 2 : 1 ); i++ ) // two cycles with polygons for ( int i = 0; i < ( isPolygon ? 2 : 1 ); i++ ) // two cycles with polygons
{ {
Expand All @@ -600,10 +602,13 @@ bool QgsGrassVectorImport::import()
iterator = mProvider->getFeatures(); iterator = mProvider->getFeatures();
} }
QgsDebugMsg( "send features" ); QgsDebugMsg( "send features" );
// Better to get real progress from module (without buffer)
#if 0
mProgress->setRange( 1, featureCount );
#endif
int count = 0; int count = 0;
while ( iterator.nextFeature( feature ) ) while ( iterator.nextFeature( feature ) )
{ {
mProgress->setValue( count + 1 );
if ( !feature.isValid() ) if ( !feature.isValid() )
{ {
continue; continue;
Expand All @@ -625,11 +630,26 @@ bool QgsGrassVectorImport::import()


#ifndef Q_OS_WIN #ifndef Q_OS_WIN
// wait until the feature is written to allow quick cancel (don't send data to buffer) // wait until the feature is written to allow quick cancel (don't send data to buffer)

// Feedback disabled because it was sometimes hanging on Linux, for example, when importing polygons
// the features were written ok in the first run, but after cleaning of polygons, which takes some time
// it was hanging here for few seconds after each feature, but data did not arrive to the modulee anyway,
// QFSFileEnginePrivate::nativeRead() was hanging on fgetc()

// TODO: inspect what is happening in QProcess, if there is some buffer and how to disable it
#if 0
mProcess->waitForReadyRead(); mProcess->waitForReadyRead();
bool result; bool result;
outStream >> result; outStream >> result;
#endif
#endif #endif
count++; count++;
#if 0
if ( count % 100 == 0 )
{
mProgress->setValue( count );
}
#endif
// get some feedback for large datasets // get some feedback for large datasets
if ( count % 10000 == 0 ) if ( count % 10000 == 0 )
{ {
Expand All @@ -644,9 +664,12 @@ bool QgsGrassVectorImport::import()
mProcess->waitForBytesWritten( -1 ); mProcess->waitForBytesWritten( -1 );
QgsDebugMsg( "features sent" ); QgsDebugMsg( "features sent" );
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
#if 0
mProcess->waitForReadyRead(); mProcess->waitForReadyRead();
bool result; bool result;
outStream >> result; outStream >> result;
QgsDebugMsg( "got feedback" );
#endif
#endif #endif
} }
iterator.close(); iterator.close();
Expand Down

0 comments on commit 272dd9c

Please sign in to comment.