Skip to content

Commit c7af95b

Browse files
author
jef
committed
add support for UTF8 in GDAL/OGR 1.8 (fixes #2551)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15148 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 63f69c5 commit c7af95b

File tree

13 files changed

+121
-43
lines changed

13 files changed

+121
-43
lines changed

src/analysis/raster/qgsninecellfilter.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include "cpl_string.h"
2020
#include <QProgressDialog>
2121

22+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
23+
#define TO8(x) (x).toUtf8().constData()
24+
#else
25+
#define TO8(x) (x).toLocal8Bit().constData()
26+
#endif
2227

2328
QgsNineCellFilter::QgsNineCellFilter( const QString& inputFile, const QString& outputFile, const QString& outputFormat ): \
2429
mInputFile( inputFile ), mOutputFile( outputFile ), mOutputFormat( outputFormat ), mCellSizeX( -1 ), mCellSizeY( -1 ), mInputNodataValue( -1 ), mOutputNodataValue( -1 )
@@ -191,7 +196,7 @@ int QgsNineCellFilter::processRaster( QProgressDialog* p )
191196

192197
GDALDatasetH QgsNineCellFilter::openInputFile( int& nCellsX, int& nCellsY )
193198
{
194-
GDALDatasetH inputDataset = GDALOpen( mInputFile.toLocal8Bit().data(), GA_ReadOnly );
199+
GDALDatasetH inputDataset = GDALOpen( TO8( mInputFile ), GA_ReadOnly );
195200
if ( inputDataset != NULL )
196201
{
197202
nCellsX = GDALGetRasterXSize( inputDataset );

src/analysis/raster/qgsrastercalculator.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
#include "gdalwarper.h"
2626

27+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
28+
#define TO8(x) (x).toUtf8().constData()
29+
#else
30+
#define TO8(x) (x).toLocal8Bit().constData()
31+
#endif
32+
2733
QgsRasterCalculator::QgsRasterCalculator( const QString& formulaString, const QString& outputFile, const QString& outputFormat,
2834
const QgsRectangle& outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry>& rasterEntries ): mFormulaString( formulaString ), mOutputFile( outputFile ), mOutputFormat( outputFormat ),
2935
mOutputRectangle( outputExtent ), mNumOutputColumns( nOutputColumns ), mNumOutputRows( nOutputRows ), mRasterEntries( rasterEntries )
@@ -59,7 +65,7 @@ int QgsRasterCalculator::processCalculation( QProgressDialog* p )
5965
{
6066
return 2;
6167
}
62-
GDALDatasetH inputDataset = GDALOpen( it->raster->source().toLocal8Bit().data(), GA_ReadOnly );
68+
GDALDatasetH inputDataset = GDALOpen( TO8( it->raster->source() ), GA_ReadOnly );
6369
if ( inputDataset == NULL )
6470
{
6571
return 2;

src/analysis/raster/qgsrastercalculator.h

+16-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,22 @@ class ANALYSIS_EXPORT QgsRasterCalculator
6161
GDALDatasetH openOutputFile( GDALDriverH outputDriver );
6262

6363
/**Reads raster pixels from a dataset/band
64-
@param targetGeotransformation transformation parameters of the requested raster array (not necessarily the same as the transform of the source dataset */
65-
void readRasterPart( double* targetGeotransform, int xOffset, int yOffset, int nCols, int nRows, double* sourceTransform, GDALRasterBandH sourceBand, float* rasterBuffer );
64+
@param targetGeotransform transformation parameters of the requested raster array
65+
(not necessarily the same as the transform of the source dataset)
66+
@param xOffset x offset
67+
@param yOffset y offset
68+
@param nCols number of columns
69+
@param nRows number of rows
70+
@param sourceTransform source transformation
71+
@param sourceBand source band
72+
@param rasterBuffer raster buffer
73+
*/
74+
void readRasterPart( double* targetGeotransform,
75+
int xOffset, int yOffset,
76+
int nCols, int nRows,
77+
double* sourceTransform,
78+
GDALRasterBandH sourceBand,
79+
float* rasterBuffer );
6680

6781
/**Compares two geotransformations (six parameter double arrays*/
6882
bool transformationsEqual( double* t1, double* t2 ) const;

src/analysis/vector/qgszonalstatistics.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#include "cpl_string.h"
2424
#include <QProgressDialog>
2525

26+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
27+
#define TO8(x) (x).toUtf8().constData()
28+
#else
29+
#define TO8(x) (x).toLocal8Bit().constData()
30+
#endif
31+
2632
QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand )
2733
: mRasterFilePath( rasterFile )
2834
, mRasterBand( rasterBand )
@@ -60,7 +66,7 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
6066

6167
//open the raster layer and the raster band
6268
GDALAllRegister();
63-
GDALDatasetH inputDataset = GDALOpen( mRasterFilePath.toLocal8Bit().data(), GA_ReadOnly );
69+
GDALDatasetH inputDataset = GDALOpen( TO8( mRasterFilePath ), GA_ReadOnly );
6470
if ( inputDataset == NULL )
6571
{
6672
return 3;

src/app/ogr/qgsnewogrconnection.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
#include <ogr_api.h>
3030
#include <cpl_error.h>
3131

32+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
33+
#define TO8F(x) (x).toUtf8().constData()
34+
#else
35+
#define TO8F(x) QFile::encodeName( x ).constData()
36+
#endif
3237

3338
QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString& connType, const QString& connName, Qt::WFlags fl )
3439
: QDialog( parent, fl ),
@@ -83,7 +88,7 @@ void QgsNewOgrConnection::testConnection()
8388
OGRDataSourceH poDS;
8489
OGRSFDriverH pahDriver;
8590
CPLErrorReset();
86-
poDS = OGROpen( QFile::encodeName( uri ).constData(), false, &pahDriver );
91+
poDS = OGROpen( TO8F( uri ), false, &pahDriver );
8792
if ( poDS == NULL )
8893
{
8994
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );

src/core/raster/qgsrasterlayer.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ email : tim at linfiniti.com
7474
// doubles can take for the current system. (Yes, 20 was arbitrary.)
7575
#define TINY_VALUE std::numeric_limits<double>::epsilon() * 20
7676

77+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
78+
#define TO8F(x) (x).toUtf8().constData()
79+
#else
80+
#define TO8F(x) QFile::encodeName( x ).constData()
81+
#endif
7782

7883
QgsRasterLayer::QgsRasterLayer(
7984
QString const & path,
@@ -452,7 +457,7 @@ bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
452457
CPLErrorReset();
453458

454459
//open the file using gdal making sure we have handled locale properly
455-
myDataset = GDALOpen( QFile::encodeName( theFileNameQString ).constData(), GA_ReadOnly );
460+
myDataset = GDALOpen( TO8F( theFileNameQString ), GA_ReadOnly );
456461
if ( myDataset == NULL )
457462
{
458463
if ( CPLGetLastErrorNo() != CPLE_OpenFailed )
@@ -1002,12 +1007,12 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
10021007

10031008
//close the gdal dataset and reopen it in read / write mode
10041009
GDALClose( mGdalDataset );
1005-
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_Update );
1010+
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_Update );
10061011

10071012
// if the dataset couldn't be opened in read / write mode, tell the user
10081013
if ( !mGdalBaseDataset )
10091014
{
1010-
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
1015+
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_ReadOnly );
10111016
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
10121017
mGdalDataset = mGdalBaseDataset;
10131018
return "ERROR_WRITE_FORMAT";
@@ -1077,7 +1082,7 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
10771082
//something bad happenend
10781083
//QString myString = QString (CPLGetLastError());
10791084
GDALClose( mGdalBaseDataset );
1080-
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
1085+
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_ReadOnly );
10811086
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
10821087
mGdalDataset = mGdalBaseDataset;
10831088

@@ -1104,7 +1109,7 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
11041109
{
11051110
//close the gdal dataset and reopen it in read only mode
11061111
GDALClose( mGdalBaseDataset );
1107-
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
1112+
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_ReadOnly );
11081113
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
11091114
mGdalDataset = mGdalBaseDataset;
11101115
}
@@ -5299,7 +5304,7 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
52995304
mGdalDataset = NULL;
53005305

53015306
//open the dataset making sure we handle char encoding of locale properly
5302-
mGdalBaseDataset = GDALOpen( QFile::encodeName( theFilename ).constData(), GA_ReadOnly );
5307+
mGdalBaseDataset = GDALOpen( TO8F( theFilename ), GA_ReadOnly );
53035308

53045309
if ( mGdalBaseDataset == NULL )
53055310
{

src/mapserver/qgssldparser.cpp

+12-16
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676
#include "qgsidwinterpolator.h"
7777
#include "qgstininterpolator.h"
7878

79+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
80+
#define TO8(x) (x).toUtf8().constData()
81+
#else
82+
#define TO8(x) (x).toLocal8Bit().constData()
83+
#endif
84+
7985
QgsSLDParser::QgsSLDParser( QDomDocument* doc ): QgsConfigParser(), mXMLDoc( doc )
8086
{
8187
mSLDNamespace = "http://www.opengis.net/sld";
@@ -1279,15 +1285,6 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty
12791285
GDALRasterBandH hBand;
12801286
GDALDatasetH hSrcDS;
12811287

1282-
QByteArray pszSrcData = rasterLayer->source().toLocal8Bit();
1283-
QByteArray pszDstData = tmpFileName.toLocal8Bit();
1284-
QByteArray pszElevData = propertyName.toLocal8Bit();
1285-
1286-
const char *pszSrcFilename = pszSrcData.data();
1287-
const char *pszDstFilename = pszDstData.data();
1288-
const char *pszElevAttrib = pszElevData.data();
1289-
const char *pszFormat = "ESRI Shapefile";
1290-
12911288
int numberOfLevels = 0;
12921289
double currentLevel = 0.0;
12931290

@@ -1315,7 +1312,7 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty
13151312

13161313
int b3D = FALSE, bNoDataSet = FALSE, bIgnoreNoData = FALSE;
13171314

1318-
hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly );
1315+
hSrcDS = GDALOpen( TO8( rasterLayer->source() ), GA_ReadOnly );
13191316
if ( hSrcDS == NULL )
13201317
exit( 2 );
13211318

@@ -1344,19 +1341,18 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty
13441341
/* Create the outputfile. */
13451342
/* -------------------------------------------------------------------- */
13461343
OGRDataSourceH hDS;
1347-
OGRSFDriverH hDriver = OGRGetDriverByName( pszFormat );
1344+
OGRSFDriverH hDriver = OGRGetDriverByName( "ESRI Shapefile" );
13481345
OGRFieldDefnH hFld;
13491346
OGRLayerH hLayer;
13501347
int nElevField = -1;
13511348

13521349
if ( hDriver == NULL )
13531350
{
1354-
fprintf( FCGI_stderr, "Unable to find format driver named %s.\n",
1355-
pszFormat );
1351+
fprintf( FCGI_stderr, "Unable to find format driver named 'ESRI Shapefile'.\n" );
13561352
exit( 10 );
13571353
}
13581354

1359-
hDS = OGR_Dr_CreateDataSource( hDriver, pszDstFilename, NULL );
1355+
hDS = OGR_Dr_CreateDataSource( hDriver, TO8( tmpFileName ), NULL );
13601356
if ( hDS == NULL )
13611357
exit( 1 );
13621358

@@ -1371,9 +1367,9 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty
13711367
OGR_L_CreateField( hLayer, hFld, FALSE );
13721368
OGR_Fld_Destroy( hFld );
13731369

1374-
if ( pszElevAttrib )
1370+
if ( !propertyName.isEmpty() )
13751371
{
1376-
hFld = OGR_Fld_Create( pszElevAttrib, OFTReal );
1372+
hFld = OGR_Fld_Create( TO8( propertyName ), OFTReal );
13771373
OGR_Fld_SetWidth( hFld, 12 );
13781374
OGR_Fld_SetPrecision( hFld, 3 );
13791375
OGR_L_CreateField( hLayer, hFld, FALSE );

src/plugins/georeferencer/qgsimagewarper.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
#include "qgsimagewarper.h"
3232
#include "qgsgeoreftransform.h"
3333

34+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
35+
#define TO8F(x) (x).toUtf8().constData()
36+
#else
37+
#define TO8F(x) QFile::encodeName( x ).constData()
38+
#endif
39+
3440
bool QgsImageWarper::mWarpCanceled = false;
3541

3642
QgsImageWarper::QgsImageWarper( QWidget *theParent )
@@ -44,7 +50,7 @@ bool QgsImageWarper::openSrcDSAndGetWarpOpt( const QString &input, const Resampl
4450
{
4551
// Open input file
4652
GDALAllRegister();
47-
hSrcDS = GDALOpen( QFile::encodeName( input ).constData(), GA_ReadOnly );
53+
hSrcDS = GDALOpen( TO8F( input ), GA_ReadOnly );
4854
if ( hSrcDS == NULL ) return false;
4955

5056
// Setup warp options.

src/plugins/ogr_converter/dialog.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232

3333
#include <ogr_api.h>
3434

35+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
36+
#define TO8(x) (x).toUtf8().constData()
37+
#else
38+
#define TO8(x) (x).toLocal8Bit().constData()
39+
#endif
40+
3541
Dialog::Dialog( QWidget* parent, Qt::WFlags fl )
3642
: QDialog( parent, fl )
3743
{
@@ -158,7 +164,7 @@ void Dialog::populateLayers( QString const& url )
158164
{
159165
comboSrcLayer->clear();
160166

161-
OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
167+
OGRDataSourceH ds = OGROpen( TO8( url ), 0, 0 );
162168
if ( 0 != ds )
163169
{
164170
QString lyrName;
@@ -199,7 +205,7 @@ bool Dialog::testConnection( QString const& url )
199205
{
200206
bool success = false;
201207

202-
OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
208+
OGRDataSourceH ds = OGROpen( TO8( url ), 0, 0 );
203209
if ( 0 != ds )
204210
{
205211
success = true;

src/plugins/ogr_converter/translator.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
#include <ogr_api.h>
2424
#include <cpl_error.h>
2525

26+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
27+
#define TO8(x) (x).toUtf8().constData()
28+
#define FROM8(x) QString::fromUtf8(x)
29+
#else
30+
#define TO8(x) (x).toLocal8Bit().constData()
31+
#define FROM8(x) QString::fromLocal8Bit(x)
32+
#endif
33+
2634
Translator::Translator()
2735
: mDstUpdate( false ), mDstLayerOverwrite( true )
2836
{
@@ -125,7 +133,7 @@ bool Translator::translate()
125133
// TODO: Support translation of all layers from input data source
126134
//for (int i = 0; i < OGR_DS_GetLayerCount(); ++i)
127135

128-
OGRLayerH srcLayer = OGR_DS_GetLayerByName( srcDs, mSrcLayer.toAscii().constData() );
136+
OGRLayerH srcLayer = OGR_DS_GetLayerByName( srcDs, TO8( mSrcLayer ) );
129137
if ( 0 == srcLayer )
130138
{
131139
QgsDebugMsg( "Can not find layer: " + mSrcLayer );
@@ -201,13 +209,12 @@ bool Translator::translateLayer( OGRDataSourceH srcDs, OGRLayerH srcLayer, OGRDa
201209
// TODO: Implement SRS transformation
202210
OGRSpatialReferenceH dstLayerSrs = OGR_L_GetSpatialRef( srcLayer );
203211

204-
dstLayer = OGR_DS_CreateLayer( dstDs, mDstLayer.toAscii().constData(),
205-
dstLayerSrs, geomType, 0 );
212+
dstLayer = OGR_DS_CreateLayer( dstDs, TO8( mDstLayer ), dstLayerSrs, geomType, 0 );
206213
}
207214

208215
if ( 0 == dstLayer )
209216
{
210-
qWarning() << QString("Layer %1 not found and CreateLayer failed [OGR: %2]\n" ).arg( mDstLayer ).arg( CPLGetLastErrorMsg() );
217+
qWarning() << QString( "Layer %1 not found and CreateLayer failed [OGR: %2]\n" ).arg( mDstLayer ).arg( CPLGetLastErrorMsg() );
211218
return false;
212219
}
213220

@@ -379,7 +386,8 @@ OGRLayerH Translator::findLayer( OGRDataSourceH ds, QString const& name, int& in
379386

380387
OGRDataSourceH Translator::openDataSource( QString const& url, bool readOnly )
381388
{
382-
OGRDataSourceH ds = OGROpen( url.toAscii().constData(), !readOnly, 0 );
389+
OGRDataSourceH ds = OGROpen( TO8( url ), !readOnly, 0 );
390+
383391
if ( 0 == ds )
384392
{
385393
QgsDebugMsg( "Failed to open: " + url );
@@ -410,7 +418,7 @@ OGRDataSourceH Translator::openDataTarget( QString const& url, bool update )
410418
// Create the output data source
411419
//
412420
// TODO: Add support for creation options
413-
ds = OGR_Dr_CreateDataSource( drv, url.toAscii().constData(), 0 );
421+
ds = OGR_Dr_CreateDataSource( drv, TO8( url ), 0 );
414422
if ( 0 == ds )
415423
{
416424
QgsDebugMsg( "Failed to open: " + url );

src/plugins/oracle_raster/qgsselectgeoraster_ui.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626

2727
#include "qgsvectorlayer.h"
2828

29+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
30+
#define TO8(x) (x).toUtf8().constData()
31+
#else
32+
#define TO8(x) (x).toLocal8Bit().constData()
33+
#endif
34+
2935
QgsOracleSelectGeoraster::QgsOracleSelectGeoraster( QWidget* parent,
3036
QgisInterface* iface,
3137
Qt::WFlags fl ) : QDialog( parent, fl ), mIface( iface )
@@ -187,7 +193,7 @@ void QgsOracleSelectGeoraster::showSelection( const QString & line )
187193
* Try to open georaster dataset
188194
*/
189195

190-
hDS = GDALOpenShared( identification.toAscii(), eAccess );
196+
hDS = GDALOpenShared( TO8( identification ), eAccess );
191197

192198
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
193199
if ( hDS == NULL )

0 commit comments

Comments
 (0)