/
qgsgrass.cpp
1661 lines (1398 loc) · 45 KB
/
qgsgrass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsgrass.cpp - Data provider for GRASS format
-------------------
begin : March, 2004
copyright : (C) 2004 by Radim Blazek
email : blazek@itc.it
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include <setjmp.h>
#include "qgsgrass.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsrectangle.h"
#include "qgsconfig.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QProcess>
#include <QSettings>
#include <QTextStream>
#include <QTemporaryFile>
#include <QHash>
#include <QTextCodec>
extern "C"
{
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <grass/version.h>
#include <grass/gprojects.h>
#if GRASS_VERSION_MAJOR < 7
#include <grass/Vect.h>
#else
#include <grass/vector.h>
#include <grass/raster.h>
#endif
}
#if !defined(GRASS_VERSION_MAJOR) || \
!defined(GRASS_VERSION_MINOR) || \
GRASS_VERSION_MAJOR<6 || \
(GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR <= 2)
#define G__setenv(name,value) G__setenv( ( char * ) (name), (char *) (value) )
#endif
#define GRASS_LOCK sMutex.lock();
#define GRASS_UNLOCK sMutex.unlock();
#ifdef Q_OS_WIN
#include <windows.h>
QString GRASS_LIB_EXPORT QgsGrass::shortPath( const QString &path )
{
TCHAR buf[MAX_PATH];
int len = GetShortPathName( path.toUtf8().constData(), buf, MAX_PATH );
if ( len == 0 || len > MAX_PATH )
{
QgsDebugMsg( QString( "GetShortPathName('%1') failed with %2: %3" )
.arg( path ).arg( len ).arg( GetLastError() ) );
return path;
}
QString res = QString::fromUtf8( buf );
// GRASS wxpyton GUI fails on paths with backslash, Windows do support forward slashesin paths
res.replace( "\\", "/" );
return res;
}
#endif
void GRASS_LIB_EXPORT QgsGrass::init( void )
{
// Warning!!!
// G_set_error_routine() once called from plugin
// is not valid in provider -> call it always
// Set error function
G_set_error_routine( &error_routine );
if ( initialized )
return;
QSettings settings;
// Is it active mode ?
if ( getenv( "GISRC" ) )
{
active = true;
// Store default values
defaultGisdbase = G_gisdbase();
defaultLocation = G_location();
defaultMapset = G_mapset();
}
else
{
active = false;
}
// Don't use GISRC file and read/write GRASS variables (from location G_VAR_GISRC) to memory only.
G_set_gisrc_mode( G_GISRC_MODE_MEMORY );
// Init GRASS libraries (required)
G_no_gisinit(); // Doesn't check write permissions for mapset compare to G_gisinit("libgrass++");
// I think that mask should not be used in QGIS as it can only confuses people,
// anyway, I don't think anybody is using MASK
// TODO7: Rast_suppress_masking (see G_suppress_masking() macro above) needs MAPSET
#if GRASS_VERSION_MAJOR < 7
G_suppress_masking();
#endif
// Set program name
G_set_program_name( "QGIS" );
// Require GISBASE to be set. This should point to the location of
// the GRASS installation. The GRASS libraries use it to know
// where to look for things.
// Look first to see if GISBASE env var is already set.
// This is set when QGIS is run from within GRASS
// or when set explicitly by the user.
// This value should always take precedence.
#ifdef Q_OS_WIN
QString gisBase = getenv( "WINGISBASE" ) ? getenv( "WINGISBASE" ) : getenv( "GISBASE" );
gisBase = shortPath( gisBase );
#else
QString gisBase = getenv( "GISBASE" );
#endif
QgsDebugMsg( QString( "GRASS gisBase from GISBASE env var is: %1" ).arg( gisBase ) );
if ( !isValidGrassBaseDir( gisBase ) )
{
// Look for gisbase in QSettings
gisBase = settings.value( "/GRASS/gisbase", "" ).toString();
QgsDebugMsg( QString( "GRASS gisBase from QSettings is: %1" ).arg( gisBase ) );
}
if ( !isValidGrassBaseDir( gisBase ) )
{
// Erase gisbase from settings because it does not exists
settings.setValue( "/GRASS/gisbase", "" );
#ifdef Q_OS_WIN
// Use the applicationDirPath()/grass
gisBase = shortPath( QCoreApplication::applicationDirPath() + "/grass" );
QgsDebugMsg( QString( "GRASS gisBase = %1" ).arg( gisBase ) );
#elif defined(Q_OS_MACX)
// check for bundled GRASS, fall back to configured path
gisBase = QCoreApplication::applicationDirPath().append( "/grass" );
if ( !isValidGrassBaseDir( gisBase ) )
{
gisBase = GRASS_BASE;
}
QgsDebugMsg( QString( "GRASS gisBase = %1" ).arg( gisBase ) );
#else
// Use the location specified --with-grass during configure
gisBase = GRASS_BASE;
QgsDebugMsg( QString( "GRASS gisBase from configure is: %1" ).arg( gisBase ) );
#endif
}
bool userGisbase = false;
bool valid = false;
while ( !( valid = isValidGrassBaseDir( gisBase ) ) )
{
// ask user if he wants to specify GISBASE
QMessageBox::StandardButton res = QMessageBox::warning( 0, QObject::tr( "GRASS plugin" ),
QObject::tr( "QGIS couldn't find your GRASS installation.\n"
"Would you like to specify path (GISBASE) to your GRASS installation?" ),
QMessageBox::Ok | QMessageBox::Cancel );
if ( res != QMessageBox::Ok )
{
userGisbase = false;
break;
}
// XXX Need to subclass this and add explantory message above to left side
userGisbase = true;
// For Mac, GISBASE folder may be inside GRASS bundle. Use Qt file dialog
// since Mac native dialog doesn't allow user to browse inside bundles.
gisBase = QFileDialog::getExistingDirectory(
0, QObject::tr( "Choose GRASS installation path (GISBASE)" ), gisBase,
QFileDialog::DontUseNativeDialog );
if ( gisBase == QString::null )
{
// User pressed cancel. No GRASS for you!
userGisbase = false;
break;
}
#ifdef Q_OS_WIN32
gisBase = shortPath( gisBase );
#endif
}
if ( !valid )
{
// warn user
QMessageBox::information( 0, QObject::tr( "GRASS plugin" ),
QObject::tr( "GRASS data won't be available if GISBASE is not specified." ) );
}
if ( userGisbase )
{
settings.setValue( "/GRASS/gisbase", gisBase );
}
QgsDebugMsg( QString( "Valid GRASS gisBase is: %1" ).arg( gisBase ) );
putEnv( "GISBASE", gisBase );
// Add path to GRASS modules
#ifdef Q_OS_WIN
QString sep = ";";
#else
QString sep = ":";
#endif
QString path = gisBase + "/bin";
path.append( sep + gisBase + "/scripts" );
path.append( sep + QgsApplication::pkgDataPath() + "/grass/scripts/" );
// On windows the GRASS libraries are in
// QgsApplication::prefixPath(), we have to add them
// to PATH to enable running of GRASS modules
// and database drivers
#ifdef Q_OS_WIN
// It seems that QgsApplication::prefixPath()
// is not initialized at this point
path.append( sep + shortPath( QCoreApplication::applicationDirPath() ) );
// Add path to MSYS bin
// Warning: MSYS sh.exe will translate this path to '/bin'
if ( QFileInfo( QCoreApplication::applicationDirPath() + "/msys/bin/" ).isDir() )
path.append( sep + shortPath( QCoreApplication::applicationDirPath() + "/msys/bin/" ) );
#endif
QString p = getenv( "PATH" );
path.append( sep + p );
QgsDebugMsg( QString( "set PATH: %1" ).arg( path ) );
putEnv( "PATH", path );
// Set PYTHONPATH
QString pythonpath = gisBase + "/etc/python";
QString pp = getenv( "PYTHONPATH" );
pythonpath.append( sep + pp );
QgsDebugMsg( QString( "set PYTHONPATH: %1" ).arg( pythonpath ) );
putEnv( "PYTHONPATH", pythonpath );
// Set GRASS_PAGER if not set, it is necessary for some
// modules printing to terminal, e.g. g.list
// We use 'cat' because 'more' is not present in MSYS (Win)
// and it doesn't work well in built in shell (Unix/Mac)
// and 'less' is not user friendly (for example user must press
// 'q' to quit which is definitely difficult for normal user)
// Also scroling can be don in scrollable window in both
// MSYS terminal and built in shell.
if ( !getenv( "GRASS_PAGER" ) )
{
QString pager;
QStringList pagers;
//pagers << "more" << "less" << "cat"; // se notes above
pagers << "cat";
for ( int i = 0; i < pagers.size(); i++ )
{
int state;
QProcess p;
p.start( pagers.at( i ) );
p.waitForStarted();
state = p.state();
p.write( "\004" ); // Ctrl-D
p.closeWriteChannel();
p.waitForFinished( 1000 );
p.kill();
if ( state == QProcess::Running )
{
pager = pagers.at( i );
break;
}
}
if ( pager.length() > 0 )
{
putEnv( "GRASS_PAGER", pager );
}
}
initialized = 1;
}
/*
* Check if given directory contains a GRASS installation
*/
bool QgsGrass::isValidGrassBaseDir( QString const gisBase )
{
QgsDebugMsg( "isValidGrassBaseDir()" );
// GRASS currently doesn't handle paths with blanks
if ( gisBase.isEmpty() || gisBase.contains( " " ) )
{
return false;
}
/* TODO: G_is_gisbase() was added to GRASS 6.1 06-05-24,
enable its use after some period (others do update) */
#if 0
if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 )
{
if ( G_is_gisbase( gisBase.toUtf8().constData() ) )
return true;
}
else
{
#endif
QFileInfo gbi( gisBase + "/etc/element_list" );
if ( gbi.exists() )
return true;
#if 0
}
#endif
return false;
}
bool QgsGrass::activeMode( void )
{
init();
return active;
}
QString QgsGrass::getDefaultGisdbase( void )
{
init();
return defaultGisdbase;
}
QString QgsGrass::getDefaultLocation( void )
{
init();
return defaultLocation;
}
QString QgsGrass::getDefaultMapset( void )
{
init();
return defaultMapset;
}
void QgsGrass::setLocation( QString gisdbase, QString location )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) );
setMapset( gisdbase, location, "PERMANENT" );
}
void QgsGrass::setMapset( QString gisdbase, QString location, QString mapset )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2 mapset = %3" ).arg( gisdbase ).arg( location ).arg( mapset ) );
init();
// Set principal GRASS variables (in memory)
#ifdef Q_OS_WIN
G__setenv( "GISDBASE", shortPath( gisdbase ).toUtf8().data() );
#else
G__setenv( "GISDBASE", gisdbase.toUtf8().data() );
#endif
G__setenv( "LOCATION_NAME", location.toUtf8().data() );
G__setenv( "MAPSET", mapset.toUtf8().data() );
// Add all available mapsets to search path
char **ms = 0;
G_TRY
{
ms = G_available_mapsets();
}
G_CATCH( QgsGrass::Exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "No available mapsets found: %1" ).arg( e.what() ) );
return;
}
for ( int i = 0; ms[i]; i++ )
G_add_mapset_to_search_path( ms[i] );
}
jmp_buf QgsGrass::jumper;
int QgsGrass::initialized = 0;
bool QgsGrass::active = 0;
QgsGrass::GERROR QgsGrass::lastError = QgsGrass::OK;
QString QgsGrass::error_message;
QString QgsGrass::defaultGisdbase;
QString QgsGrass::defaultLocation;
QString QgsGrass::defaultMapset;
QString QgsGrass::mMapsetLock;
QString QgsGrass::mGisrc;
QString QgsGrass::mTmp;
QMutex QgsGrass::sMutex;
int QgsGrass::error_routine( char *msg, int fatal )
{
return error_routine(( const char* ) msg, fatal );
}
int QgsGrass::error_routine( const char *msg, int fatal )
{
// G_fatal_error obviously is not thread safe (everything static in GRASS, especially fatal_jmp_buf)
// it means that anything which may end up with G_fatal_error must use mutex
// Unfortunately the exceptions thrown here can only be caught if GRASS libraries are compiled
// with -fexception option on Linux (works on Windows)
// GRASS developers are reluctant to add -fexception by default
// https://trac.osgeo.org/grass/ticket/869
QgsDebugMsg( QString( "error_routine (fatal = %1): %2" ).arg( fatal ).arg( msg ) );
error_message = msg;
if ( fatal )
{
QgsDebugMsg( "fatal -> longjmp" );
// Exceptions cannot be thrown from here if GRASS lib is not compiled with -fexceptions
//throw QgsGrass::Exception( QString::fromUtf8( msg ) );
lastError = FATAL;
#if (GRASS_VERSION_MAJOR < 7)
// longjump() is called by G_fatal_error in GRASS >= 7
longjmp( QgsGrass::jumper, 1 );
#endif
}
else
{
lastError = WARNING;
}
return 1;
}
void GRASS_LIB_EXPORT QgsGrass::resetError( void )
{
lastError = OK;
}
int GRASS_LIB_EXPORT QgsGrass::error( void )
{
return lastError;
}
QString GRASS_LIB_EXPORT QgsGrass::errorMessage( void )
{
return error_message;
}
QString GRASS_LIB_EXPORT QgsGrass::openMapset( QString gisdbase, QString location, QString mapset )
{
QgsDebugMsg( QString( "gisdbase = %1" ).arg( gisdbase.toUtf8().constData() ) );
QgsDebugMsg( QString( "location = %1" ).arg( location.toUtf8().constData() ) );
QgsDebugMsg( QString( "mapset = %1" ).arg( mapset.toUtf8().constData() ) );
QString mapsetPath = gisdbase + "/" + location + "/" + mapset;
// Check if the mapset is in use
QString gisBase = getenv( "GISBASE" );
if ( gisBase.isEmpty() )
return QObject::tr( "GISBASE is not set." );
QFileInfo fi( mapsetPath + "/WIND" );
if ( !fi.exists() )
{
return QObject::tr( "%1 is not a GRASS mapset." ).arg( mapsetPath );
}
QString lock = mapsetPath + "/.gislock";
#ifndef _MSC_VER
int pid = getpid();
#else
int pid = GetCurrentProcessId();
#endif
QgsDebugMsg( QString( "pid = %1" ).arg( pid ) );
#ifndef Q_OS_WIN
QFile lockFile( lock );
QProcess *process = new QProcess();
QString lockProgram( gisBase + "/etc/lock" );
process->start( lockProgram, QStringList() << lock << QString::number( pid ) );
if ( !process->waitForStarted() )
{
delete process;
return QObject::tr( "Cannot start %1/etc/lock" ).arg( gisBase );
}
process->waitForFinished( -1 );
int status = process->exitStatus();
QgsDebugMsg( QString( "status = %1" ).arg( status ) );
delete process;
if ( status > 0 )
return QObject::tr( "Mapset is already in use." );
#endif // Q_OS_WIN
// Create temporary directory
QFileInfo info( mapsetPath );
QString user = info.owner();
mTmp = QDir::tempPath() + "/grass6-" + user + "-" + QString::number( pid );
QDir dir( mTmp );
if ( dir.exists() )
{
QFileInfo dirInfo( mTmp );
if ( !dirInfo.isWritable() )
{
#ifndef Q_OS_WIN
lockFile.remove();
#endif
return QObject::tr( "Temporary directory %1 exists but is not writable" ).arg( mTmp );
}
}
else if ( !dir.mkdir( mTmp ) )
{
#ifndef Q_OS_WIN
lockFile.remove();
#endif
return QObject::tr( "Cannot create temporary directory %1" ).arg( mTmp );
}
// Create GISRC file
QString globalGisrc = QDir::home().path() + "/.grassrc6";
mGisrc = mTmp + "/gisrc";
QgsDebugMsg( QString( "globalGisrc = %1" ).arg( globalGisrc ) );
QgsDebugMsg( QString( "mGisrc = %1" ).arg( mGisrc ) );
QFile out( mGisrc );
if ( !out.open( QIODevice::WriteOnly ) )
{
#ifndef Q_OS_WIN
lockFile.remove();
#endif
return QObject::tr( "Cannot create %1" ).arg( mGisrc );
}
QTextStream stream( &out );
QFile in( globalGisrc );
QString line;
bool guiSet = false;
char buf[1000];
if ( in.open( QIODevice::ReadOnly ) )
{
while ( in.readLine( buf, 1000 ) != -1 )
{
line = buf;
if ( line.contains( "GISDBASE:" ) ||
line.contains( "LOCATION_NAME:" ) ||
line.contains( "MAPSET:" ) )
{
continue;
}
if ( line.contains( "GRASS_GUI:" ) )
guiSet = true;
stream << line;
}
in.close();
}
line = "GISDBASE: " + gisdbase + "\n";
stream << line;
line = "LOCATION_NAME: " + location + "\n";
stream << line;
line = "MAPSET: " + mapset + "\n";
stream << line;
if ( !guiSet )
{
stream << "GRASS_GUI: wxpython\n";
}
out.close();
// Set GISRC environment variable
/* _Correct_ putenv() implementation is not making copy! */
putEnv( "GISRC", mGisrc );
// Reinitialize GRASS
G__setenv( "GISRC", mGisrc.toUtf8().data() );
#ifdef Q_OS_WIN
G__setenv( "GISDBASE", shortPath( gisdbase ).toLocal8Bit().data() );
#else
G__setenv( "GISDBASE", gisdbase.toUtf8().data() );
#endif
G__setenv( "LOCATION_NAME", location.toLocal8Bit().data() );
G__setenv( "MAPSET", mapset.toLocal8Bit().data() );
defaultGisdbase = gisdbase;
defaultLocation = location;
defaultMapset = mapset;
active = true;
#ifndef Q_OS_WIN
// Close old mapset
if ( mMapsetLock.length() > 0 )
{
QFile file( mMapsetLock );
file.remove();
}
#endif
mMapsetLock = lock;
return NULL;
}
QString QgsGrass::closeMapset()
{
QgsDebugMsg( "entered." );
if ( mMapsetLock.length() > 0 )
{
#ifndef Q_OS_WIN
QFile file( mMapsetLock );
if ( !file.remove() )
{
return QObject::tr( "Cannot remove mapset lock: %1" ).arg( mMapsetLock );
}
#endif
mMapsetLock = "";
putenv(( char * ) "GISRC" );
// Reinitialize GRASS
G__setenv( "GISRC", ( char * ) "" );
// Temporarily commented because of
// http://trac.osgeo.org/qgis/ticket/1900
// http://trac.osgeo.org/gdal/ticket/3313
// it can be uncommented once GDAL with patch gets deployed (probably GDAL 1.8)
//G__setenv( "GISDBASE", ( char * ) "" );
//G__setenv( "LOCATION_NAME", ( char * ) "" );
//G__setenv( "MAPSET", ( char * ) "" );
defaultGisdbase = "";
defaultLocation = "";
defaultMapset = "";
active = 0;
// Delete temporary dir
// To be sure that we don't delete '/' for example
if ( mTmp.left( 4 ) == "/tmp" )
{
QDir dir( mTmp );
for ( unsigned int i = 0; i < dir.count(); i++ )
{
if ( dir[i] == "." || dir[i] == ".." )
continue;
dir.remove( dir[i] );
if ( dir.remove( dir[i] ) )
{
QgsDebugMsg( QString( "Cannot remove temporary file %1" ).arg( dir[i] ) );
}
}
if ( !dir.rmdir( mTmp ) )
{
QgsDebugMsg( QString( "Cannot remove temporary directory %1" ).arg( mTmp ) );
}
}
}
return NULL;
}
QStringList GRASS_LIB_EXPORT QgsGrass::locations( QString gisbase )
{
QgsDebugMsg( QString( "gisbase = %1" ).arg( gisbase ) );
QStringList list;
if ( gisbase.isEmpty() )
return list;
QDir d = QDir( gisbase );
d.setFilter( QDir::NoDotAndDotDot | QDir::Dirs );
for ( unsigned int i = 0; i < d.count(); i++ )
{
if ( QFile::exists( gisbase + "/" + d[i]
+ "/PERMANENT/DEFAULT_WIND" ) )
{
list.append( QString( d[i] ) );
}
}
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::mapsets( QString gisbase, QString locationName )
{
QgsDebugMsg( QString( "gisbase = %1 locationName = %2" ).arg( gisbase ).arg( locationName ) );
if ( gisbase.isEmpty() || locationName.isEmpty() )
return QStringList();
return QgsGrass::mapsets( gisbase + "/" + locationName );
}
QStringList GRASS_LIB_EXPORT QgsGrass::mapsets( QString locationPath )
{
QgsDebugMsg( QString( "locationPath = %1" ).arg( locationPath ) );
QStringList list;
if ( locationPath.isEmpty() )
return list;
QDir d = QDir( locationPath );
d.setFilter( QDir::NoDotAndDotDot | QDir::Dirs );
for ( unsigned int i = 0; i < d.count(); i++ )
{
if ( QFile::exists( locationPath + "/" + d[i] + "/WIND" ) )
{
list.append( d[i] );
}
}
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString gisbase, QString locationName,
QString mapsetName )
{
QgsDebugMsg( "entered." );
if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
return QStringList();
/* TODO: G_list() was added to GRASS 6.1 06-05-24,
enable its use after some period (others do update) */
/*
if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 )
{
QStringList list;
char **glist = G_list( G_ELEMENT_VECTOR,
gisbase.toUtf8().constData(),
locationName.toUtf8().constData(),
mapsetName.toUtf8().constData() );
int i = 0;
while ( glist[i] )
{
list.append( QString(glist[i]) );
i++;
}
G_free_list ( glist );
return list;
}
*/
return QgsGrass::vectors( gisbase + "/" + locationName + "/" + mapsetName );
}
QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString mapsetPath )
{
QgsDebugMsg( QString( "mapsetPath = %1" ).arg( mapsetPath ) );
QStringList list;
if ( mapsetPath.isEmpty() )
return list;
QDir d = QDir( mapsetPath + "/vector" );
d.setFilter( QDir::NoDotAndDotDot | QDir::Dirs );
for ( unsigned int i = 0; i < d.count(); i++ )
{
/*
if ( QFile::exists ( mapsetPath + "/vector/" + d[i] + "/head" ) )
{
list.append(d[i]);
}
*/
list.append( d[i] );
}
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
QString location, QString mapset, QString mapName )
{
GRASS_LOCK
QgsDebugMsg( QString( "gisdbase = %1 location = %2 mapset = %3 mapName = %4" ).arg( gisdbase ).arg( location ).arg( mapset ).arg( mapName ) );
QStringList list;
// Set location
QgsGrass::setLocation( gisdbase, location );
/* Open vector */
QgsGrass::resetError();
//Vect_set_open_level( 2 );
struct Map_info map;
int level = -1;
// Vect_open_old_head GRASS is raising fatal error if topo exists but it is in different (older) version.
// It means that even we could open it on level one, it ends with exception,
// but we need level 2 anyway to get list of layers, so it does not matter, only the error message may be misleading.
G_TRY
{
level = Vect_open_old_head( &map, ( char * ) mapName.toUtf8().data(), ( char * ) mapset.toUtf8().data() );
}
G_CATCH( QgsGrass::Exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Cannot open GRASS vector: %1" ).arg( e.what() ) );
GRASS_UNLOCK
return list;
}
if ( level == 1 )
{
QgsDebugMsg( "Cannot open vector on level 2" );
QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot open vector %1 in mapset %2 on level 2 (topology not available, try to rebuild topology using v.build module)." ).arg( mapName ).arg( mapset ) );
// Vect_close here is correct, it should work, but it seems to cause
// crash on win http://trac.osgeo.org/qgis/ticket/2003
// disabled on win test it
#ifndef Q_OS_WIN
Vect_close( &map );
#endif
GRASS_UNLOCK
return list;
}
else if ( level < 1 )
{
QgsDebugMsg( "Cannot open vector" );
QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot open vector %1 in mapset %2" ).arg( mapName ).arg( mapset ) );
GRASS_UNLOCK
return list;
}
QgsDebugMsg( "GRASS vector successfully opened" );
// Get layers
int ncidx = Vect_cidx_get_num_fields( &map );
for ( int i = 0; i < ncidx; i++ )
{
int field = Vect_cidx_get_field_number( &map, i );
QString fs;
fs.sprintf( "%d", field );
QgsDebugMsg( QString( "i = %1 layer = %2" ).arg( i ).arg( field ) );
/* Points */
int npoints = Vect_cidx_get_type_count( &map, field, GV_POINT );
if ( npoints > 0 )
{
QString l = fs + "_point";
list.append( l );
}
/* Lines */
/* Lines without category appears in layer 0, but not boundaries */
int tp;
if ( field == 0 )
tp = GV_LINE;
else
tp = GV_LINE | GV_BOUNDARY;
int nlines = Vect_cidx_get_type_count( &map, field, tp );
if ( nlines > 0 )
{
QString l = fs + "_line";
list.append( l );
}
/* Faces */
int nfaces = Vect_cidx_get_type_count( &map, field, GV_FACE );
if ( nfaces > 0 )
{
QString l = fs + "_face";
list.append( l );
}
/* Polygons */
int nareas = Vect_cidx_get_type_count( &map, field, GV_AREA );
if ( nareas > 0 )
{
QString l = fs + "_polygon";
list.append( l );
}
}
// TODO: add option in GUI to set listTopoLayers
QSettings settings;
bool listTopoLayers = settings.value( "/GRASS/listTopoLayers", false ).toBool();
if ( listTopoLayers )
{
// add topology layers
if ( Vect_get_num_primitives( &map, GV_POINTS ) > 0 )
{
#if GRASS_VERSION_MAJOR < 7 /* no more point in GRASS 7 topo */
list.append( "topo_point" );
#endif
}
if ( Vect_get_num_primitives( &map, GV_LINES ) > 0 )
{
list.append( "topo_line" );
}
if ( Vect_get_num_nodes( &map ) > 0 )
{
list.append( "topo_node" );
}
}
Vect_close( &map );
GRASS_UNLOCK
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::rasters( QString gisbase, QString locationName,
QString mapsetName )
{
QgsDebugMsg( "entered." );
if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
return QStringList();
/* TODO: G_list() was added to GRASS 6.1 06-05-24,
enable its use after some period (others do update) */
/*
if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 )
{
QStringList list;
char **glist = G_list( G_ELEMENT_RASTER,
gisbase.toUtf8().constData(),
locationName.toUtf8().constData(),
mapsetName.toUtf8().constData() );
int i = 0;
while ( glist[i] )
{
list.append( QString(glist[i]) );
i++;
}
G_free_list ( glist );
return list;
}
*/
return QgsGrass::rasters( gisbase + "/" + locationName + "/" + mapsetName );
}
QStringList GRASS_LIB_EXPORT QgsGrass::rasters( QString mapsetPath )
{
QgsDebugMsg( QString( "mapsetPath = %1" ).arg( mapsetPath ) );
QStringList list;
if ( mapsetPath.isEmpty() )
return list;
QDir d = QDir( mapsetPath + "/cellhd" );
d.setFilter( QDir::Files );
for ( unsigned int i = 0; i < d.count(); i++ )
{
list.append( d[i] );
}
return list;
}