/
qgsgrassrasterprovider.cpp
220 lines (187 loc) · 6.67 KB
/
qgsgrassrasterprovider.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
/***************************************************************************
qgsgrassrasterprovider.cpp - QGIS Data provider for
GRASS rasters
-------------------
begin : 16 Jan, 2010
copyright : (C) 2010 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. *
* *
***************************************************************************/
/* $Id: qgsgrassrasterprovider.cpp 11522 2009-08-28 14:49:22Z jef $ */
#include "qgslogger.h"
#include "qgsgrass.h"
#include "qgsgrassrasterprovider.h"
#include "qgsconfig.h"
#include <math.h>
#include "qgsapplication.h"
#include "qgscoordinatetransform.h"
#include "qgsrectangle.h"
#include "qgscoordinatereferencesystem.h"
#include <QImage>
#include <QSettings>
#include <QColor>
#include <QProcess>
#include <QMessageBox>
#include <QDir>
#include <QFileInfo>
#include <QFile>
static QString PROVIDER_KEY = "grassraster";
static QString PROVIDER_DESCRIPTION = "GRASS raster provider";
QgsGrassRasterProvider::QgsGrassRasterProvider( QString const & uri )
: QgsRasterDataProvider( uri ), mValid( true )
{
QgsDebugMsg( "QgsGrassRasterProvider: constructing with uri '" + uri + "'." );
// Parse URI, it is the same like using GDAL, i.e. path to raster cellhd, i.e.
// /path/to/gisdbase/location/mapset/cellhd/map
QFileInfo fileInfo( uri );
mValid = fileInfo.exists(); // then we keep it valid forever
mMapName = fileInfo.fileName();
QDir dir = fileInfo.dir();
QString element = dir.dirName();
if ( element != "cellhd" )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Groups not yet supported" ) + " (GRASS " + uri + ")" );
mValid = false;
return;
}
dir.cdUp(); // skip cellhd
mMapset = dir.dirName();
dir.cdUp();
mLocation = dir.dirName();
dir.cdUp();
mGisdbase = dir.path();
QgsDebugMsg( QString( "gisdbase: %1" ).arg( mGisdbase ) );
QgsDebugMsg( QString( "location: %1" ).arg( mLocation ) );
QgsDebugMsg( QString( "mapset: %1" ).arg( mMapset ) );
QgsDebugMsg( QString( "mapName: %1" ).arg( mMapName ) );
mCrs = QgsGrass::crs( mGisdbase, mLocation );
}
QgsGrassRasterProvider::~QgsGrassRasterProvider()
{
QgsDebugMsg( "QgsGrassRasterProvider: deconstructing." );
}
QImage* QgsGrassRasterProvider::draw( QgsRectangle const & viewExtent, int pixelWidth, int pixelHeight )
{
QgsDebugMsg( "pixelWidth = " + QString::number( pixelWidth ) );
QgsDebugMsg( "pixelHeight = " + QString::number( pixelHeight ) );
QgsDebugMsg( "viewExtent: " + viewExtent.toString() );
QImage *image = new QImage( pixelWidth, pixelHeight, QImage::Format_ARGB32 );
image->fill( QColor( Qt::gray ).rgb() );
QStringList arguments;
arguments.append( "map=" + mMapName + "@" + mMapset );
arguments.append(( QString( "window=%1,%2,%3,%4,%5,%6" )
.arg( viewExtent.xMinimum() ).arg( viewExtent.yMinimum() )
.arg( viewExtent.xMaximum() ).arg( viewExtent.yMaximum() )
.arg( pixelWidth ).arg( pixelHeight ) ) );
QProcess process( this );
QString cmd = QgsApplication::prefixPath() + "/" QGIS_LIBEXEC_SUBDIR "/grass/modules/qgis.d.rast";
QByteArray data;
try
{
data = QgsGrass::runModule( mGisdbase, mLocation, cmd, arguments );
}
catch ( QgsGrass::Exception &e )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot draw raster" ) + "\n"
+ e.what() );
// We don't set mValid to false, because the raster can be recreated and work next time
return image;
}
QgsDebugMsg( QString( "%1 bytes read from modules stdout" ).arg( data.size() ) );
uchar * ptr = image->bits( ) ;
// byteCount() in Qt >= 4.6
//int size = image->byteCount() < data.size() ? image->byteCount() : data.size();
int size = pixelWidth * pixelHeight * 4 < data.size() ? pixelWidth * pixelHeight * 4 : data.size();
memcpy( ptr, data.data(), size );
return image;
}
QgsCoordinateReferenceSystem QgsGrassRasterProvider::crs()
{
QgsDebugMsg( "Entered" );
return mCrs;
}
QgsRectangle QgsGrassRasterProvider::extent()
{
// The extend can change of course so we get always fresh, to avoid running always the module
// we should save mExtent and mLastModified and check if the map was modified
QgsRectangle rect;
rect = QgsGrass::extent( mGisdbase, mLocation, mMapset, mMapName, QgsGrass::Raster );
return rect;
}
bool QgsGrassRasterProvider::identify( const QgsPoint& thePoint, QMap<QString, QString>& theResults )
{
QgsDebugMsg( "Entered" );
//theResults["Error"] = tr( "Out of extent" );
theResults = QgsGrass::query( mGisdbase, mLocation, mMapset, mMapName, QgsGrass::Raster, thePoint.x(), thePoint.y() );
return true;
}
int QgsGrassRasterProvider::capabilities() const
{
int capability = QgsRasterDataProvider::Identify;
return capability;
}
bool QgsGrassRasterProvider::isValid()
{
return mValid;
}
QString QgsGrassRasterProvider::identifyAsText( const QgsPoint& point )
{
return QString( "Not implemented" );
}
QString QgsGrassRasterProvider::identifyAsHtml( const QgsPoint& point )
{
return QString( "Not implemented" );
}
QString QgsGrassRasterProvider::lastErrorTitle()
{
return QString( "Not implemented" );
}
QString QgsGrassRasterProvider::lastError()
{
return QString( "Not implemented" );
}
QString QgsGrassRasterProvider::name() const
{
return PROVIDER_KEY;
}
QString QgsGrassRasterProvider::description() const
{
return PROVIDER_DESCRIPTION;
}
/**
* Class factory to return a pointer to a newly created
* QgsGrassRasterProvider object
*/
QGISEXTERN QgsGrassRasterProvider * classFactory( const QString *uri )
{
return new QgsGrassRasterProvider( *uri );
}
/** Required key function (used to map the plugin to a data store type)
*/
QGISEXTERN QString providerKey()
{
return PROVIDER_KEY;
}
/**
* Required description function
*/
QGISEXTERN QString description()
{
return PROVIDER_DESCRIPTION;
}
/**
* Required isProvider function. Used to determine if this shared library
* is a data provider plugin
*/
QGISEXTERN bool isProvider()
{
return true;
}